Skip to content

Commit 28f16e9

Browse files
authored
feat: FloatingArray, Series.__new__ + astype and also for Index (#1493)
* floating array * simplify import * simplify skip_platform * fix: mac_arm * exception on platform * attempt to drop arm for mac * bi-directional type matching #1493 (comment) * #1501
1 parent dc3304b commit 28f16e9

File tree

11 files changed

+1148
-340
lines changed

11 files changed

+1148
-340
lines changed

pandas-stubs/_typing.pyi

Lines changed: 205 additions & 133 deletions
Large diffs are not rendered by default.
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
from pandas.core.arrays.numeric import NumericDtype
1+
from pandas.core.arrays.numeric import (
2+
NumericArray,
3+
NumericDtype,
4+
)
25

36
class Float32Dtype(NumericDtype): ...
47
class Float64Dtype(NumericDtype): ...
8+
class FloatingArray(NumericArray): ...
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
from pandas.core.arrays.masked import BaseMaskedArray
2+
3+
from pandas._libs.properties import cache_readonly
4+
15
from pandas.core.dtypes.dtypes import BaseMaskedDtype
26

37
class NumericDtype(BaseMaskedDtype): ...
8+
9+
class NumericArray(BaseMaskedArray):
10+
@cache_readonly
11+
def dtype(self) -> NumericDtype: ...

pandas-stubs/core/construction.pyi

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,43 @@ from typing import overload
44
import numpy as np
55
from pandas.core.arrays.base import ExtensionArray
66
from pandas.core.arrays.boolean import BooleanArray
7+
from pandas.core.arrays.floating import FloatingArray
78
from pandas.core.arrays.integer import IntegerArray
89

910
from pandas._libs.missing import NAType
1011
from pandas._typing import (
11-
BooleanDtypeArg,
12-
IntDtypeArg,
13-
UIntDtypeArg,
12+
PandasBooleanDtypeArg,
13+
PandasFloatDtypeArg,
14+
PandasIntDtypeArg,
15+
PandasUIntDtypeArg,
16+
np_ndarray_anyint,
17+
np_ndarray_bool,
18+
np_ndarray_float,
1419
)
1520

1621
from pandas.core.dtypes.dtypes import ExtensionDtype
1722

1823
@overload
1924
def array( # type: ignore[overload-overlap] # pyright: ignore[reportOverlappingOverload]
20-
data: Sequence[bool | NAType | None],
21-
dtype: BooleanDtypeArg | None = None,
25+
data: Sequence[bool | np.bool | NAType | None] | np_ndarray_bool | BooleanArray,
26+
dtype: PandasBooleanDtypeArg | None = None,
2227
copy: bool = True,
2328
) -> BooleanArray: ...
2429
@overload
25-
def array(
26-
data: Sequence[int | NAType | None],
27-
dtype: IntDtypeArg | UIntDtypeArg | None = None,
30+
def array( # type: ignore[overload-overlap]
31+
data: Sequence[int | np.integer | NAType | None] | np_ndarray_anyint | IntegerArray,
32+
dtype: PandasIntDtypeArg | PandasUIntDtypeArg | None = None,
2833
copy: bool = True,
2934
) -> IntegerArray: ...
3035
@overload
36+
def array(
37+
data: (
38+
Sequence[float | np.floating | NAType | None] | np_ndarray_float | FloatingArray
39+
),
40+
dtype: PandasFloatDtypeArg | None = None,
41+
copy: bool = True,
42+
) -> FloatingArray: ...
43+
@overload
3144
def array(
3245
data: Sequence[object],
3346
dtype: str | np.dtype | ExtensionDtype | None = None,

pandas-stubs/core/indexes/base.pyi

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ from typing import (
1515
ClassVar,
1616
Generic,
1717
Literal,
18+
TypeAlias,
1819
final,
1920
overload,
2021
type_check_only,
@@ -29,6 +30,7 @@ from _typeshed import (
2930
)
3031
import numpy as np
3132
from pandas.core.arrays.boolean import BooleanArray
33+
from pandas.core.arrays.floating import FloatingArray
3234
from pandas.core.base import (
3335
ArrayIndexTimedeltaNoSeq,
3436
ElementOpsMixin,
@@ -82,6 +84,7 @@ from pandas._typing import (
8284
ArrayLike,
8385
AxesData,
8486
Axis,
87+
BuiltinFloatDtypeArg,
8588
CategoryDtypeArg,
8689
DropKeep,
8790
Dtype,
@@ -98,6 +101,10 @@ from pandas._typing import (
98101
Level,
99102
MaskType,
100103
NaPosition,
104+
NumpyFloatNot16DtypeArg,
105+
PandasAstypeFloatDtypeArg,
106+
PandasFloatDtypeArg,
107+
PyArrowFloatDtypeArg,
101108
ReindexMethod,
102109
S2_contra,
103110
Scalar,
@@ -123,6 +130,13 @@ from pandas._typing import (
123130

124131
from pandas.core.dtypes.dtypes import PeriodDtype
125132

133+
FloatNotNumpy16DtypeArg: TypeAlias = (
134+
BuiltinFloatDtypeArg
135+
| PandasFloatDtypeArg
136+
| NumpyFloatNot16DtypeArg
137+
| PyArrowFloatDtypeArg
138+
)
139+
126140
class InvalidIndexError(Exception): ...
127141

128142
class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
@@ -161,9 +175,8 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
161175
@overload
162176
def __new__(
163177
cls,
164-
data: Sequence[float | np.floating] | IndexOpsMixin[float] | np_ndarray_float,
165-
*,
166-
dtype: Literal["float"] | type_t[float | np.floating] = ...,
178+
data: Sequence[float | np.floating] | np_ndarray_float | FloatingArray,
179+
dtype: None = None,
167180
copy: bool = ...,
168181
name: Hashable = ...,
169182
tupleize_cols: bool = ...,
@@ -172,8 +185,7 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
172185
def __new__(
173186
cls,
174187
data: AxesData,
175-
*,
176-
dtype: Literal["float"] | type_t[float | np.floating],
188+
dtype: FloatNotNumpy16DtypeArg,
177189
copy: bool = ...,
178190
name: Hashable = ...,
179191
tupleize_cols: bool = ...,
@@ -361,6 +373,13 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
361373
@final
362374
def ravel(self, order: _str = "C") -> Self: ...
363375
def view(self, cls=...): ...
376+
@overload
377+
def astype(
378+
self,
379+
dtype: FloatNotNumpy16DtypeArg | PandasAstypeFloatDtypeArg,
380+
copy: bool = True,
381+
) -> Index[float]: ...
382+
@overload
364383
def astype(self, dtype: DtypeArg, copy: bool = True) -> Index: ...
365384
def take(
366385
self,

0 commit comments

Comments
 (0)