-
-
Notifications
You must be signed in to change notification settings - Fork 154
feat: FloatingArray, Series.__new__ + astype and also for Index
#1493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
| ASTYPE_FLOAT_ARGS: list[tuple[FloatDtypeArg, type]] = [ | ||
| # python float | ||
| (float, np.floating), | ||
| ("float", np.floating), | ||
| # pandas Float32 | ||
| (pd.Float32Dtype(), np.float32), | ||
| ("Float32", np.float32), | ||
| # pandas Float64 | ||
| (pd.Float64Dtype(), np.float64), | ||
| ("Float64", np.float64), | ||
| # numpy float16 | ||
| (np.half, np.half), | ||
| ("half", np.half), | ||
| ("e", np.half), | ||
| ("float16", np.float16), | ||
| ("f2", np.float16), | ||
| # numpy float32 | ||
| (np.single, np.single), | ||
| ("single", np.single), | ||
| ("f", np.single), | ||
| ("float32", np.float32), | ||
| ("f4", np.float32), | ||
| # numpy float64 | ||
| (np.double, np.double), | ||
| ("double", np.double), | ||
| ("d", np.double), | ||
| ("float64", np.float64), | ||
| ("f8", np.float64), | ||
| # numpy float128 | ||
| (np.longdouble, np.longdouble), | ||
| ("longdouble", np.longdouble), | ||
| ("g", np.longdouble), | ||
| ("f16", np.longdouble), | ||
| # ("float96", np.longdouble), # NOTE: unsupported | ||
| ("float128", np.longdouble), # NOTE: UNIX ONLY | ||
| # pyarrow float32 | ||
| ("float32[pyarrow]", float), | ||
| ("float[pyarrow]", float), | ||
| # pyarrow float64 | ||
| ("float64[pyarrow]", float), | ||
| ("double[pyarrow]", float), | ||
| ] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved to tests/__init__.py
| assert_type(s.astype("uint64[pyarrow]"), "pd.Series[int]") | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("cast_arg, target_type", ASTYPE_FLOAT_ARGS, ids=repr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved to test_series_float.py
| # if TYPE_CHECKING_INVALID_USAGE: | ||
| # # numpy float16 | ||
| # pd.Index([1.0], dtype=np.half) | ||
| # pd.Index([1.0], dtype="half") | ||
| # pd.Index([1.0], dtype="float16") | ||
| # pd.Index([1.0], dtype="e") | ||
| # pd.Index([1.0], dtype="f2") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At the moment, our __new__ overloads in Index cover everything including these invalid ones. Maybe we will clean them up in the future and recognise them as invalid.
| # if TYPE_CHECKING_INVALID_USAGE: | ||
| # # numpy float16 | ||
| # s.astype(np.half) | ||
| # s.astype("half") | ||
| # s.astype("float16") | ||
| # s.astype("e") | ||
| # s.astype("f2") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At the moment, our __new__ overloads in Index cover everything including these invalid ones. Maybe we will clean them up in the future and recognise them as invalid.
| # Refer to https://numpy.org/doc/stable/reference/arrays.datetime.html#datetime-units | ||
| TimedeltaDtypeArg: TypeAlias = Literal[ | ||
| ComplexDtypeArg: TypeAlias = BuiltinComplexDtypeArg | NumpyComplexDtypeArg | ||
| PandasAstypeTimedeltaDtypeArg: TypeAlias = Literal[ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import pandas as pd
pd.Series(["2025-01-01"], dtype="datetime64[D]") # TypeError: dtype=datetime64[D] is not supported. Supported resolutions are 's', 'ms', 'us', and 'ns'These only work upon astype.
| | Literal["F", "c8", "complex64", "csingle"] | ||
| # https://numpy.org/doc/stable/reference/arrays.scalars.html#numpy.cdouble | ||
| | type[np.cdouble] | ||
| | Literal["D", "c16", "complex128", "cdouble", "cfloat", "complex_"] | ||
| | Literal["D", "c16", "complex128", "cdouble"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The removed values do not work for me in pytests at runtime. I think they are NON_NUMPY20_ALIASES.
| # Builtin bool type and its string alias | ||
| BuiltinBooleanDtypeArg: TypeAlias = type[bool] | Literal["bool"] | ||
| # Pandas nullable boolean type and its string alias | ||
| PandasBooleanDtypeArg: TypeAlias = pd.BooleanDtype | Literal["boolean"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a bit annoying that all these has to be duplicated in tests/__init__.py.
b5454cb to
ee8615a
Compare
This is a PR simpler than #1469 which focuses more on adding
FloatingArray.I have made many more categories for the
DtypeArgs, because of the following reasons:Only
Pandasdtypes are relevant for making aFloatingArray. Native andNumpydtypes lead to aNumpyExtensionArray, andPyArrowdtypes lead to anArrowExtensionArray, if notArrowStringArrayPandassupports more dtypes inastypethan in constructorsIndexdoes not supportnp.float16Closes
FloatingArraydocumented but missing from stubs #1428Tests added