Skip to content

Commit 1ee17e8

Browse files
committed
fix numpy
1 parent 3b2e23d commit 1ee17e8

File tree

5 files changed

+58
-37
lines changed

5 files changed

+58
-37
lines changed

pandas-stubs/core/base.pyi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ from typing import (
1010
Literal,
1111
Protocol,
1212
TypeAlias,
13+
TypeVar,
1314
final,
1415
overload,
1516
type_check_only,
@@ -53,6 +54,8 @@ from pandas._typing import (
5354
)
5455
from pandas.util._decorators import cache_readonly
5556

57+
T_INTERVAL_NP = TypeVar("T_INTERVAL_NP", bound=np.bytes_ | np.str_)
58+
5659
class NoNewAttributesMixin:
5760
def __setattr__(self, key: str, value: Any) -> None: ...
5861

pandas-stubs/core/indexes/base.pyi

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import numpy as np
3232
from pandas.core.arrays.boolean import BooleanArray
3333
from pandas.core.arrays.floating import FloatingArray
3434
from pandas.core.base import (
35+
T_INTERVAL_NP,
3536
ArrayIndexTimedeltaNoSeq,
3637
ElementOpsMixin,
3738
IndexComplex,
@@ -1168,6 +1169,14 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
11681169

11691170
@type_check_only
11701171
class _IndexSubclassBase(Index[S1], Generic[S1, GenericT_co]):
1172+
@overload
1173+
def to_numpy(
1174+
self: _IndexSubclassBase[Interval],
1175+
dtype: type[T_INTERVAL_NP],
1176+
copy: bool = False,
1177+
na_value: Scalar = ...,
1178+
**kwargs: Any,
1179+
) -> np_1darray: ...
11711180
@overload
11721181
def to_numpy(
11731182
self,

pandas-stubs/core/series.pyi

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ from typing import (
2929
NoReturn,
3030
Protocol,
3131
TypeAlias,
32-
TypeVar,
3332
final,
3433
overload,
3534
type_check_only,
@@ -71,6 +70,7 @@ from pandas.core.arrays.datetimes import DatetimeArray
7170
from pandas.core.arrays.floating import FloatingArray
7271
from pandas.core.arrays.timedeltas import TimedeltaArray
7372
from pandas.core.base import (
73+
T_INTERVAL_NP,
7474
ArrayIndexSeriesTimedeltaNoSeq,
7575
ArrayIndexTimedeltaNoSeq,
7676
ElementOpsMixin,
@@ -189,6 +189,7 @@ from pandas._typing import (
189189
MaskType,
190190
NaPosition,
191191
NsmallestNlargestKeep,
192+
NumpyStrDtypeArg,
192193
ObjectDtypeArg,
193194
PandasAstypeComplexDtypeArg,
194195
PandasAstypeFloatDtypeArg,
@@ -251,8 +252,6 @@ from pandas.core.dtypes.dtypes import CategoricalDtype
251252

252253
from pandas.plotting import PlotAccessor
253254

254-
_T_INTERVAL_NP = TypeVar("_T_INTERVAL_NP", bound=np.bytes_ | np.str_)
255-
256255
@type_check_only
257256
class _SupportsAdd(Protocol[_T_co]):
258257
def __add__(self, value: Self, /) -> _T_co: ...
@@ -4503,7 +4502,7 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
45034502
copy: bool = False,
45044503
na_value: Scalar = ...,
45054504
**kwargs: Any,
4506-
) -> np_1darray_bytes: ...
4505+
) -> np_1darray: ...
45074506
@overload
45084507
def to_numpy(
45094508
self: Series[Interval],
@@ -4515,11 +4514,11 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
45154514
@overload
45164515
def to_numpy(
45174516
self: Series[Interval],
4518-
dtype: type[_T_INTERVAL_NP],
4517+
dtype: type[T_INTERVAL_NP],
45194518
copy: bool = False,
45204519
na_value: Scalar = ...,
45214520
**kwargs: Any,
4522-
) -> np_1darray[_T_INTERVAL_NP]: ...
4521+
) -> np_1darray: ...
45234522
@overload
45244523
def to_numpy(
45254524
self: Series[int],
@@ -4555,12 +4554,28 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
45554554
@overload
45564555
def to_numpy(
45574556
self: Series[_str],
4558-
dtype: DTypeLike | None = None,
4557+
dtype: NumpyStrDtypeArg,
45594558
copy: bool = False,
45604559
na_value: Scalar = ...,
45614560
**kwargs: Any,
45624561
) -> np_1darray_str: ...
45634562
@overload
4563+
def to_numpy(
4564+
self: Series[_str],
4565+
dtype: DTypeLike,
4566+
copy: bool = False,
4567+
na_value: Scalar = ...,
4568+
**kwargs: Any,
4569+
) -> np_1darray: ...
4570+
@overload
4571+
def to_numpy(
4572+
self: Series[_str],
4573+
dtype: None = None,
4574+
copy: bool = False,
4575+
na_value: Scalar = ...,
4576+
**kwargs: Any,
4577+
) -> np_1darray_object: ...
4578+
@overload
45644579
def to_numpy(
45654580
self: Series[bytes],
45664581
dtype: DTypeLike | None = None,

tests/series/test_series.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@
100100
np_1darray_dt,
101101
np_1darray_float,
102102
np_1darray_object,
103-
np_1darray_str,
104103
np_1darray_td,
105104
np_ndarray_num,
106105
)
@@ -128,7 +127,6 @@
128127
np_1darray_dt,
129128
np_1darray_float,
130129
np_1darray_object,
131-
np_1darray_str,
132130
np_1darray_td,
133131
np_ndarray_num,
134132
)
@@ -2032,18 +2030,22 @@ def test_dtype_type() -> None:
20322030

20332031
def test_types_to_numpy() -> None:
20342032
s = pd.Series(["a", "b", "c"], dtype=str)
2035-
check(assert_type(s.to_numpy(), np_1darray_str), np_1darray_str)
2033+
check(assert_type(s.to_numpy(), np_1darray_object), np_1darray_object)
2034+
check( # <U1, not str_
2035+
assert_type(s.to_numpy(dtype="str", copy=True), np_1darray), np_1darray
2036+
)
2037+
check(assert_type(s.to_numpy(na_value=0), np_1darray_object), np_1darray_object)
20362038
check(
2037-
assert_type(s.to_numpy(dtype="str", copy=True), np_1darray_str), np_1darray_str
2039+
assert_type(s.to_numpy(na_value=np.int32(4)), np_1darray_object),
2040+
np_1darray_object,
20382041
)
2039-
check(assert_type(s.to_numpy(na_value=0), np_1darray_str), np_1darray_str)
2040-
check(assert_type(s.to_numpy(na_value=np.int32(4)), np_1darray_str), np_1darray_str)
20412042
check(
2042-
assert_type(s.to_numpy(na_value=np.float16(4)), np_1darray_str), np_1darray_str
2043+
assert_type(s.to_numpy(na_value=np.float16(4)), np_1darray_object),
2044+
np_1darray_object,
20432045
)
20442046
check(
2045-
assert_type(s.to_numpy(na_value=np.complex128(4, 7)), np_1darray_str),
2046-
np_1darray_str,
2047+
assert_type(s.to_numpy(na_value=np.complex128(4, 7)), np_1darray_object),
2048+
np_1darray_object,
20472049
)
20482050

20492051
check(assert_type(pd.Series().to_numpy(), np_1darray), np_1darray)
@@ -2052,7 +2054,7 @@ def test_types_to_numpy() -> None:
20522054
def test_to_numpy() -> None:
20532055
"""Test Series.to_numpy for different types."""
20542056
s_str = pd.Series(["a", "b", "c"], dtype=str)
2055-
check(assert_type(s_str.to_numpy(), np_1darray_str), np_1darray_str)
2057+
check(assert_type(s_str.to_numpy(), np_1darray_object), np_1darray_object)
20562058

20572059
s_bytes = pd.Series(["a", "b", "c"]).astype(bytes)
20582060
check(assert_type(s_bytes.to_numpy(), np_1darray_bytes), np_1darray, np.bytes_)

tests/test_timefuncs.py

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,18 @@
5050
from pandas._typing import (
5151
np_1darray,
5252
np_1darray_bool,
53-
np_1darray_bytes,
5453
np_1darray_dt,
5554
np_1darray_int64,
5655
np_1darray_object,
57-
np_1darray_str,
5856
np_1darray_td,
5957
)
6058
else:
6159
from tests._typing import (
6260
np_1darray,
6361
np_1darray_bool,
64-
np_1darray_bytes,
6562
np_1darray_dt,
6663
np_1darray_int64,
6764
np_1darray_object,
68-
np_1darray_str,
6965
np_1darray_td,
7066
)
7167

@@ -976,11 +972,11 @@ def test_series_types_to_numpy() -> None:
976972
)
977973
check(
978974
assert_type(o_s.to_numpy(dtype="bytes", copy=True), np_1darray),
979-
np_1darray_bytes,
975+
np_1darray,
980976
)
981977
check(
982978
assert_type(i_s.to_numpy(dtype="bytes", copy=True), np_1darray),
983-
np_1darray_bytes,
979+
np_1darray,
984980
)
985981

986982
# passed dtype-like with statically known generic
@@ -1009,13 +1005,12 @@ def test_series_types_to_numpy() -> None:
10091005
np_1darray,
10101006
np.int64,
10111007
)
1012-
check(
1013-
assert_type(o_s.to_numpy(dtype=np.bytes_), np_1darray_bytes), np_1darray_bytes
1014-
)
1015-
check(
1016-
assert_type(i_s.to_numpy(dtype=np.bytes_), np_1darray_bytes), np_1darray_bytes
1017-
)
1018-
check(assert_type(i_s.to_numpy(dtype=np.str_), np_1darray_str), np_1darray_str)
1008+
# |S20, not bytes_
1009+
check(assert_type(o_s.to_numpy(dtype=np.bytes_), np_1darray), np_1darray)
1010+
# |S6, not bytes_
1011+
check(assert_type(i_s.to_numpy(dtype=np.bytes_), np_1darray), np_1darray)
1012+
# <U6, not str_
1013+
check(assert_type(i_s.to_numpy(dtype=np.str_), np_1darray), np_1darray)
10191014

10201015

10211016
def test_index_types_to_numpy() -> None:
@@ -1074,10 +1069,8 @@ def test_index_types_to_numpy() -> None:
10741069
np_1darray,
10751070
dtype=np.integer,
10761071
)
1077-
check(
1078-
assert_type(i_i.to_numpy(dtype="bytes", copy=True), np_1darray),
1079-
np_1darray_bytes,
1080-
)
1072+
# |S6, not bytes_
1073+
check(assert_type(i_i.to_numpy(dtype="bytes", copy=True), np_1darray), np_1darray)
10811074

10821075
# passed dtype-like with statically known generic
10831076
check(
@@ -1095,9 +1088,8 @@ def test_index_types_to_numpy() -> None:
10951088
np_1darray,
10961089
np.int64,
10971090
)
1098-
check(
1099-
assert_type(i_i.to_numpy(dtype=np.bytes_), np_1darray_bytes), np_1darray_bytes
1100-
)
1091+
# |S6, not bytes_
1092+
check(assert_type(i_i.to_numpy(dtype=np.bytes_), np_1darray), np_1darray)
11011093

11021094

11031095
def test_to_timedelta_units() -> None:

0 commit comments

Comments
 (0)