Skip to content

Commit c3a11fb

Browse files
committed
ENH: Improve Arrays
1 parent 617529a commit c3a11fb

File tree

9 files changed

+718
-31
lines changed

9 files changed

+718
-31
lines changed

pandas-stubs/_libs/tslibs/period.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ from typing import Any
22

33
class IncompatibleFrequency(ValueError): ...
44

5+
from pandas._libs.tslibs.offsets import BaseOffset
6+
57
class Period:
68
def __init__(
79
self,
810
value: Any = ...,
9-
freqstr: Any = ...,
11+
freq: str | BaseOffset | None = ...,
1012
ordinal: Any = ...,
1113
year: Any = ...,
1214
month: int = ...,

pandas-stubs/core/arrays/boolean.pyi

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import numpy as np
2+
from pandas.core.arrays import ExtensionArray
23

34
from pandas._typing import (
45
Scalar,
@@ -21,16 +22,15 @@ class BooleanDtype(ExtensionDtype):
2122
def construct_array_type(cls) -> type_t[BooleanArray]: ...
2223
def __from_arrow__(self, array): ...
2324

24-
def coerce_to_array(values, mask=..., copy: bool = ...): ...
25-
2625
class BooleanArray(BaseMaskedArray):
2726
def __init__(
2827
self, values: np.ndarray, mask: np.ndarray, copy: bool = ...
2928
) -> None: ...
29+
def __setitem__(self, key: int | np.ndarray | slice, value: object) -> None: ...
3030
@property
3131
def dtype(self): ...
32-
def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): ...
33-
def __setitem__(self, key, value) -> None: ...
34-
def astype(self, dtype, copy: bool = ...): ...
35-
def any(self, skipna: bool = ..., **kwargs): ...
36-
def all(self, skipna: bool = ..., **kwargs): ...
32+
def astype(
33+
self, dtype: str | np.dtype, copy: bool = ...
34+
) -> np.ndarray | ExtensionArray: ...
35+
def any(self, skipna: bool = ..., **kwargs) -> bool: ...
36+
def all(self, skipna: bool = ..., **kwargs) -> bool: ...

pandas-stubs/core/arrays/integer.pyi

Lines changed: 115 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
from typing import (
2+
Literal,
3+
Sequence,
4+
overload,
5+
)
6+
7+
import numpy as np
8+
import pandas as pd
9+
from pandas.arrays import (
10+
BooleanArray,
11+
DatetimeArray,
12+
StringArray,
13+
)
14+
15+
from pandas._libs.missing import NAType
16+
from pandas._typing import (
17+
Dtype,
18+
npt,
19+
)
20+
121
from pandas.core.dtypes.base import ExtensionDtype as ExtensionDtype
222

323
from .masked import BaseMaskedArray
@@ -18,15 +38,103 @@ class _IntegerDtype(ExtensionDtype):
1838
def construct_array_type(cls): ...
1939
def __from_arrow__(self, array): ...
2040

21-
def safe_cast(values, dtype, copy): ...
22-
def coerce_to_array(values, dtype, mask=..., copy: bool = ...): ...
23-
2441
class IntegerArray(BaseMaskedArray):
2542
def dtype(self): ...
26-
def __init__(self, values, mask, copy: bool = ...) -> None: ...
27-
def __array_ufunc__(self, ufunc, method, *inputs, **kwargs): ...
28-
def __setitem__(self, key, value) -> None: ...
29-
def astype(self, dtype, copy: bool = ...): ...
43+
def __init__(
44+
self,
45+
values: npt.NDArray[np.integer],
46+
mask: npt.NDArray[np.bool_] | Sequence[bool],
47+
copy: bool = ...,
48+
) -> None: ...
49+
@overload # type: ignore[override]
50+
def __setitem__(self, key: int, value: float | NAType) -> None: ...
51+
@overload
52+
def __setitem__(
53+
self,
54+
key: Sequence[int] | slice | npt.NDArray[np.integer],
55+
value: float | NAType | Sequence[float | NAType] | npt.NDArray[np.integer],
56+
) -> None: ...
57+
@overload # type: ignore[override]
58+
def __getitem__(self, item: int) -> int | NAType: ...
59+
@overload
60+
def __getitem__(
61+
self, item: slice | list[int] | npt.NDArray[np.integer]
62+
) -> IntegerArray: ...
63+
# Note: the ignores are needed below due to types being subclasses,
64+
# e.g., float32 and float64 or bool, int, float, complex
65+
@overload
66+
def astype( # type: ignore[misc]
67+
self, dtype: Literal["str"] | type[str] | np.str_
68+
) -> npt.NDArray[np.str_]: ...
69+
@overload
70+
def astype( # type: ignore[misc]
71+
self, dtype: type[bool] | Literal["bool"] | type[np.bool_], copy: bool = ...
72+
) -> npt.NDArray[np.bool_]: ...
73+
@overload
74+
def astype(
75+
self,
76+
dtype: type[int]
77+
| Literal["i1", "i2", "i4", "i8", "int8", "int16", "int32", "int64"]
78+
| type[np.int8]
79+
| type[np.int16]
80+
| type[np.int32]
81+
| type[np.int64],
82+
copy: bool = ...,
83+
) -> npt.NDArray[np.signedinteger]: ...
84+
@overload
85+
def astype(
86+
self,
87+
dtype: Literal["u1", "u2", "u4", "u8", "uint8", "uint16", "uint32", "uint64"]
88+
| type[np.uint8]
89+
| type[np.uint16]
90+
| type[np.uint32]
91+
| type[np.uint64],
92+
copy: bool = ...,
93+
) -> npt.NDArray[np.unsignedinteger]: ...
94+
@overload
95+
def astype( # type: ignore[misc]
96+
self, dtype: Literal["f4", "float32"] | type[np.float32]
97+
) -> npt.NDArray[np.float32]: ...
98+
@overload
99+
def astype(
100+
self, dtype: type[float] | Literal["float", "float64", "f8"] | type[np.float64]
101+
) -> npt.NDArray[np.float64]: ...
102+
@overload
103+
def astype( # type: ignore[misc]
104+
self, dtype: Literal["complex64", "c8"] | type[np.complex64]
105+
) -> npt.NDArray[np.complex64]: ...
106+
@overload
107+
def astype(
108+
self,
109+
dtype: type[complex]
110+
| Literal["complex", "complex128", "c16"]
111+
| type[np.complex128],
112+
) -> npt.NDArray[np.complex128]: ...
113+
@overload
114+
def astype(self, dtype: Literal["boolean"] | pd.BooleanDtype) -> BooleanArray: ...
115+
@overload
116+
def astype(
117+
self,
118+
dtype: Literal[
119+
"Int8", "Int16", "Int32", "Int64", "UInt8", "UInt16", "UInt32", "UInt64"
120+
]
121+
| pd.Int8Dtype
122+
| pd.Int16Dtype
123+
| pd.Int32Dtype
124+
| pd.Int64Dtype
125+
| pd.UInt8Dtype
126+
| pd.UInt16Dtype
127+
| pd.UInt32Dtype
128+
| pd.UInt64Dtype,
129+
) -> IntegerArray: ...
130+
@overload
131+
def astype(self, dtype: Literal["string"] | pd.StringDtype) -> StringArray: ...
132+
@overload
133+
def astype(
134+
self, dtype: type[np.datetime64] | Literal["M8[ns]"]
135+
) -> npt.NDArray[np.datetime64]: ...
136+
@overload
137+
def astype(self, dtype: pd.DatetimeTZDtype) -> DatetimeArray: ...
30138

31139
class Int8Dtype(_IntegerDtype): ...
32140
class Int16Dtype(_IntegerDtype): ...

pandas-stubs/core/arrays/interval.pyi

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@ class IntervalArray(IntervalMixin, ExtensionArray):
1717
cls, data, closed=..., dtype=..., copy: bool = ..., verify_integrity: bool = ...
1818
): ...
1919
@classmethod
20-
def from_breaks(cls, breaks, closed: str = ..., copy: bool = ..., dtype=...): ...
20+
def from_breaks(
21+
cls, breaks, closed: str = ..., copy: bool = ..., dtype=...
22+
) -> IntervalArray: ...
2123
@classmethod
2224
def from_arrays(
2325
cls, left, right, closed: str = ..., copy: bool = ..., dtype=...
24-
): ...
26+
) -> IntervalArray: ...
2527
@classmethod
26-
def from_tuples(cls, data, closed: str = ..., copy: bool = ..., dtype=...): ...
28+
def from_tuples(
29+
cls, data, closed: str = ..., copy: bool = ..., dtype=...
30+
) -> IntervalArray: ...
2731
def __iter__(self): ...
2832
def __len__(self) -> int: ...
2933
def __getitem__(self, value): ...

pandas-stubs/core/arrays/period.pyi

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ from pandas.core.arrays.datetimelike import (
66
DatetimeLikeArrayMixin,
77
)
88

9-
from pandas._libs.tslibs import Timestamp
10-
from pandas._libs.tslibs.period import Period as Period
9+
from pandas._libs.tslibs import Period
10+
from pandas._libs.tslibs.timestamps import Timestamp
11+
from pandas._typing import npt as npt
1112

12-
from pandas.tseries.offsets import Tick as Tick
13+
from pandas.tseries.offsets import Tick
1314

1415
class PeriodArray(DatetimeLikeArrayMixin, DatelikeOps):
1516
__array_priority__: int = ...
@@ -43,11 +44,8 @@ class PeriodArray(DatetimeLikeArrayMixin, DatelikeOps):
4344
def asfreq(self, freq: str | None = ..., how: str = ...) -> Period: ...
4445
def astype(self, dtype, copy: bool = ...): ...
4546

46-
def raise_on_incompatible(left, right): ...
4747
def period_array(
4848
data: Sequence[Period | None],
4949
freq: str | Tick | None = ...,
5050
copy: bool = ...,
5151
) -> PeriodArray: ...
52-
def validate_dtype_freq(dtype, freq): ...
53-
def dt64arr_to_periodarr(data, freq, tz=...): ...

pandas-stubs/core/arrays/sparse/array.pyi

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ from pandas.core.arrays import (
55
)
66
from pandas.core.base import PandasObject
77

8+
from pandas._libs.sparse import SparseIndex
9+
810
class SparseArray(PandasObject, ExtensionArray, ExtensionOpsMixin):
911
def __init__(
1012
self,
@@ -20,9 +22,9 @@ class SparseArray(PandasObject, ExtensionArray, ExtensionOpsMixin):
2022
def __array__(self, dtype=..., copy=...) -> np.ndarray: ...
2123
def __setitem__(self, key, value) -> None: ...
2224
@property
23-
def sp_index(self): ...
25+
def sp_index(self) -> SparseIndex: ...
2426
@property
25-
def sp_values(self): ...
27+
def sp_values(self) -> np.ndarray: ...
2628
@property
2729
def dtype(self): ...
2830
@property

pandas-stubs/core/arrays/string_.pyi

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1+
from typing import Sequence
2+
3+
import numpy as np
4+
import pandas as pd
15
from pandas.core.arrays import PandasArray
26

3-
from pandas._typing import type_t
7+
from pandas._typing import (
8+
AnyArrayLike,
9+
type_t,
10+
)
411

512
from pandas.core.dtypes.base import ExtensionDtype
613

@@ -14,7 +21,12 @@ class StringDtype(ExtensionDtype):
1421
def __from_arrow__(self, array): ...
1522

1623
class StringArray(PandasArray):
17-
def __init__(self, values, copy: bool = ...) -> None: ...
24+
def __init__(
25+
self,
26+
# Also pd.NA and np.nan but not possible it seems
27+
values: AnyArrayLike | Sequence[str | None],
28+
copy: bool = ...,
29+
) -> None: ...
1830
def __arrow_array__(self, type=...): ...
1931
def __setitem__(self, key, value) -> None: ...
2032
def fillna(self, value=..., method=..., limit=...): ...

pandas-stubs/core/construction.pyi

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
1-
from typing import Sequence
1+
from typing import (
2+
Sequence,
3+
Union,
4+
)
25

36
import numpy as np
7+
import pandas as pd
48
from pandas.core.indexes.api import Index
59
from pandas.core.series import Series
610

711
from pandas._typing import (
812
ArrayLike,
913
Dtype,
14+
npt,
1015
)
1116

1217
from pandas.core.dtypes.dtypes import ExtensionDtype
1318
from pandas.core.dtypes.generic import ABCExtensionArray
1419

1520
def array(
16-
data: Sequence[object],
17-
dtype: str | np.dtype | ExtensionDtype | None = ...,
21+
# str is forbidden even though Sequence[object] allows "abc"
22+
data: npt.NDArray | Sequence[object] | pd.Index | pd.Series,
23+
dtype: str
24+
| np.dtype[np.generic]
25+
| ExtensionDtype
26+
| type[Union[str, bool, float, int]]
27+
| None = ...,
1828
copy: bool = ...,
1929
) -> ABCExtensionArray: ...
2030
def extract_array(obj, extract_numpy: bool = ...): ...

0 commit comments

Comments
 (0)