Skip to content

Conversation

@cmp0xff
Copy link
Contributor

@cmp0xff cmp0xff commented Nov 18, 2025

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 Pandas dtypes are relevant for making a FloatingArray. Native and Numpy dtypes lead to a NumpyExtensionArray, and PyArrow dtypes lead to an ArrowExtensionArray, if not ArrowStringArray

  • Pandas supports more dtypes in astype than in constructors

  • Index does not support np.float16

  • Closes FloatingArray documented but missing from stubs #1428

  • Tests added

@cmp0xff cmp0xff requested a review from Dr-Irv November 18, 2025 14:18
Comment on lines -2367 to -2410
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),
]
Copy link
Contributor Author

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)
Copy link
Contributor Author

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

Comment on lines +102 to +103
# 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")
Copy link
Contributor Author

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.

Comment on lines +158 to +162
# if TYPE_CHECKING_INVALID_USAGE:
# # numpy float16
# s.astype(np.half)
# s.astype("half")
# s.astype("float16")
# s.astype("e")
# s.astype("f2")
Copy link
Contributor Author

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[
Copy link
Contributor Author

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.

Comment on lines +365 to +368
| 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"]
Copy link
Contributor Author

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.

Comment on lines +82 to +83
# 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"]
Copy link
Contributor Author

@cmp0xff cmp0xff Nov 18, 2025

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.

@cmp0xff cmp0xff force-pushed the feature/floating-array branch from b5454cb to ee8615a Compare November 20, 2025 21:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FloatingArray documented but missing from stubs

1 participant