Skip to content

Commit a5b997e

Browse files
committed
floating
1 parent e2cb09d commit a5b997e

File tree

4 files changed

+118
-64
lines changed

4 files changed

+118
-64
lines changed

tests/__init__.py

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -462,51 +462,56 @@
462462
PD_LTE_23 = Version(pd.__version__) < Version("2.3.999")
463463
NUMPY20 = np.lib.NumpyVersion(np.__version__) >= "2.0.0"
464464

465-
TYPE_FLOAT_ARGS: list[tuple[FloatDtypeArg, type]] = [
465+
NATIVE_FLOAT_ARGS = {
466466
# python float
467-
(float, np.floating),
468-
("float", np.floating),
469-
# pandas Float32
470-
(pd.Float32Dtype(), np.float32),
471-
("Float32", np.float32),
472-
# pandas Float64
473-
(pd.Float64Dtype(), np.float64),
474-
("Float64", np.float64),
467+
float: np.floating,
468+
"float": np.floating,
469+
}
470+
NUMPY_FLOAT_ARGS = {
475471
# numpy float16
476-
(np.half, np.half),
477-
("half", np.half),
478-
("e", np.half),
479-
("float16", np.float16),
480-
("f2", np.float16),
472+
np.half: np.half,
473+
"half": np.half,
474+
"e": np.half,
475+
"float16": np.float16,
476+
"f2": np.float16,
481477
# numpy float32
482-
(np.single, np.single),
483-
("single", np.single),
484-
("f", np.single),
485-
("float32", np.float32),
486-
("f4", np.float32),
478+
np.single: np.single,
479+
"single": np.single,
480+
"f": np.single,
481+
"float32": np.float32,
482+
"f4": np.float32,
487483
# numpy float64
488-
(np.double, np.double),
489-
("double", np.double),
490-
("d", np.double),
491-
("float64", np.float64),
492-
("f8", np.float64),
484+
np.double: np.double,
485+
"double": np.double,
486+
"d": np.double,
487+
"float64": np.float64,
488+
"f8": np.float64,
493489
# numpy float128
494-
(np.longdouble, np.longdouble),
495-
("g", np.longdouble),
490+
np.longdouble: np.longdouble,
491+
"g": np.longdouble,
496492
# pyarrow float32
497-
("float32[pyarrow]", float),
498-
("float[pyarrow]", float),
493+
"float32[pyarrow]": float,
494+
"float[pyarrow]": float,
499495
# pyarrow float64
500-
("float64[pyarrow]", float),
501-
("double[pyarrow]", float),
502-
]
503-
ASTYPE_FLOAT_ARGS: list[tuple[FloatDtypeArg | PandasAstypeFloatDtypeArg, type]] = [
504-
*TYPE_FLOAT_ARGS,
505-
("longdouble", np.longdouble),
506-
("f16", np.longdouble),
507-
# ("float96", np.longdouble), # NOTE: unsupported
508-
("float128", np.longdouble), # NOTE: UNIX ONLY
509-
]
496+
"float64[pyarrow]": float,
497+
"double[pyarrow]": float,
498+
}
499+
PANDAS_FLOAT_ARGS = {
500+
# pandas Float32
501+
pd.Float32Dtype(): np.float32,
502+
"Float32": np.float32,
503+
# pandas Float64
504+
pd.Float64Dtype(): np.float64,
505+
"Float64": np.float64,
506+
}
507+
TYPE_FLOAT_ARGS = NATIVE_FLOAT_ARGS | NUMPY_FLOAT_ARGS | PANDAS_FLOAT_ARGS
508+
ASTYPE_FLOAT_ARGS = {
509+
**TYPE_FLOAT_ARGS,
510+
"longdouble": np.longdouble,
511+
"f16": np.longdouble,
512+
# "float96": np.longdouble, # NOTE: unsupported
513+
"float128": np.longdouble, # NOTE: UNIX ONLY
514+
}
510515

511516

512517
def check(
@@ -657,20 +662,15 @@ def pytest_warns_bounded(
657662

658663

659664
def skip_platform(
660-
type_error: Callable[[], Any], dtype: type | str | ExtensionDtype
665+
raise_type_error: Callable[[], Any], dtype: type | str | ExtensionDtype
661666
) -> None:
662-
if platform.system() == "Windows" and dtype in {"f16", "float128"}:
663-
with pytest.raises(TypeError):
664-
type_error()
665-
pytest.skip(f"Windows does not support {dtype}")
667+
system = platform.system()
666668
if (
667-
platform.system() == "Darwin"
668-
and platform.processor() == "arm"
669-
and dtype in {"f16", "float128"}
670-
):
669+
system == "Windows" or system == "Darwin" and platform.processor() == "arm"
670+
) and dtype in {"f16", "float128"}:
671671
with pytest.raises(TypeError):
672-
type_error()
673-
pytest.skip(f"MacOS arm does not support {dtype}")
672+
raise_type_error()
673+
pytest.skip(f"{system} does not support {dtype}")
674674

675675

676676
def get_dtype(dtype: object) -> Generator[type | str, None, None]:

tests/arrays/test_floating_array.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
from typing import TYPE_CHECKING
2+
13
import numpy as np
24
import pandas as pd
35
from pandas.core.arrays.floating import FloatingArray
46
import pytest
57
from typing_extensions import assert_type
68

79
from tests import (
10+
PANDAS_FLOAT_ARGS,
811
PandasFloatDtypeArg,
912
check,
10-
get_dtype,
1113
skip_platform,
1214
)
1315

@@ -28,8 +30,19 @@ def test_constructor() -> None:
2830
check(assert_type(pd.array(pd.array([1.0])), FloatingArray), FloatingArray)
2931

3032

31-
@pytest.mark.parametrize("dtype", get_dtype(PandasFloatDtypeArg))
32-
def test_constructor_dtype(dtype: PandasFloatDtypeArg) -> None:
33-
skip_platform(lambda: pd.array([1.0], dtype=dtype), dtype)
33+
@pytest.mark.parametrize(("dtype", "target_dtype"), PANDAS_FLOAT_ARGS.items(), ids=repr)
34+
def test_constructor_dtype(dtype: PandasFloatDtypeArg, target_dtype: type) -> None:
35+
def maker() -> FloatingArray:
36+
return assert_type(pd.array([1.0], dtype=dtype), FloatingArray)
37+
38+
skip_platform(maker, dtype)
39+
40+
check(maker(), FloatingArray, target_dtype)
3441

35-
check(assert_type(pd.array([True], dtype=dtype), FloatingArray), FloatingArray)
42+
if TYPE_CHECKING:
43+
# pandas Float32
44+
assert_type(pd.array([1.0], dtype=pd.Float32Dtype()), FloatingArray)
45+
assert_type(pd.array([1.0], dtype="Float32"), FloatingArray)
46+
# pandas Float64
47+
assert_type(pd.array([1.0], dtype=pd.Float64Dtype()), FloatingArray)
48+
assert_type(pd.array([1.0], dtype="Float64"), FloatingArray)

tests/series/test_series.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3016,7 +3016,7 @@ def test_all_astype_args_tested() -> None:
30163016
ASTYPE_BOOL_ARGS
30173017
+ ASTYPE_INT_ARGS
30183018
+ ASTYPE_UINT_ARGS
3019-
+ ASTYPE_FLOAT_ARGS
3019+
+ list(ASTYPE_FLOAT_ARGS.items())
30203020
+ ASTYPE_COMPLEX_ARGS
30213021
+ ASTYPE_TIMEDELTA_ARGS
30223022
+ ASTYPE_TIMESTAMP_ARGS

tests/series/test_series_float.py

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,59 @@ def test_constructor() -> None:
4343
)
4444

4545

46-
@pytest.mark.parametrize(("dtype", "np_dtype"), TYPE_FLOAT_ARGS)
47-
def test_constructor_dtype(dtype: FloatDtypeArg, np_dtype: type) -> None:
48-
skip_platform(lambda: pd.Series([1.0], dtype=dtype), dtype)
46+
@pytest.mark.parametrize(("dtype", "target_dtype"), TYPE_FLOAT_ARGS.items())
47+
def test_constructor_dtype(dtype: FloatDtypeArg, target_dtype: type) -> None:
48+
def maker() -> "pd.Series[float]":
49+
return assert_type(pd.Series([1.0], dtype=dtype), "pd.Series[float]")
4950

50-
check(
51-
assert_type(pd.Series([1.0], dtype=dtype), "pd.Series[float]"),
52-
pd.Series,
53-
np_dtype,
54-
)
51+
skip_platform(maker, dtype)
52+
53+
check(maker(), pd.Series, target_dtype)
54+
if TYPE_CHECKING:
55+
# python float
56+
assert_type(pd.Series([1.0], dtype=float), "pd.Series[float]")
57+
assert_type(pd.Series([1.0], dtype="float"), "pd.Series[float]")
58+
# pandas Float32
59+
assert_type(pd.Series([1.0], dtype=pd.Float32Dtype()), "pd.Series[float]")
60+
assert_type(pd.Series([1.0], dtype="Float32"), "pd.Series[float]")
61+
# pandas Float64
62+
assert_type(pd.Series([1.0], dtype=pd.Float64Dtype()), "pd.Series[float]")
63+
assert_type(pd.Series([1.0], dtype="Float64"), "pd.Series[float]")
64+
# numpy float16
65+
assert_type(pd.Series([1.0], dtype=np.half), "pd.Series[float]")
66+
assert_type(pd.Series([1.0], dtype="half"), "pd.Series[float]")
67+
assert_type(pd.Series([1.0], dtype="float16"), "pd.Series[float]")
68+
assert_type(pd.Series([1.0], dtype="e"), "pd.Series[float]")
69+
assert_type(pd.Series([1.0], dtype="f2"), "pd.Series[float]")
70+
# numpy float32
71+
assert_type(pd.Series([1.0], dtype=np.single), "pd.Series[float]")
72+
assert_type(pd.Series([1.0], dtype="single"), "pd.Series[float]")
73+
assert_type(pd.Series([1.0], dtype="float32"), "pd.Series[float]")
74+
assert_type(pd.Series([1.0], dtype="f"), "pd.Series[float]")
75+
assert_type(pd.Series([1.0], dtype="f4"), "pd.Series[float]")
76+
# numpy float64
77+
assert_type(pd.Series([1.0], dtype=np.double), "pd.Series[float]")
78+
assert_type(pd.Series([1.0], dtype="double"), "pd.Series[float]")
79+
assert_type(pd.Series([1.0], dtype="float64"), "pd.Series[float]")
80+
assert_type(pd.Series([1.0], dtype="d"), "pd.Series[float]")
81+
assert_type(pd.Series([1.0], dtype="f8"), "pd.Series[float]")
82+
# numpy float128
83+
assert_type(pd.Series([1.0], dtype=np.longdouble), "pd.Series[float]")
84+
assert_type(pd.Series([1.0], dtype="longdouble"), "pd.Series[float]")
85+
assert_type(pd.Series([1.0], dtype="float128"), "pd.Series[float]")
86+
assert_type(pd.Series([1.0], dtype="g"), "pd.Series[float]")
87+
assert_type(pd.Series([1.0], dtype="f16"), "pd.Series[float]")
88+
# pyarrow float32
89+
assert_type(pd.Series([1.0], dtype="float32[pyarrow]"), "pd.Series[float]")
90+
assert_type(pd.Series([1.0], dtype="float[pyarrow]"), "pd.Series[float]")
91+
# pyarrow float64
92+
assert_type(pd.Series([1.0], dtype="float64[pyarrow]"), "pd.Series[float]")
93+
assert_type(pd.Series([1.0], dtype="double[pyarrow]"), "pd.Series[float]")
5594

5695

57-
@pytest.mark.parametrize("cast_arg, target_type", ASTYPE_FLOAT_ARGS, ids=repr)
96+
@pytest.mark.parametrize(
97+
("cast_arg", "target_type"), ASTYPE_FLOAT_ARGS.items(), ids=repr
98+
)
5899
def test_astype_float(cast_arg: FloatDtypeArg, target_type: type) -> None:
59100
s = pd.Series([1, 2, 3])
60101

0 commit comments

Comments
 (0)