Skip to content

Commit 034f12b

Browse files
committed
fix: #1508 numpy arrays in tests
1 parent b82986c commit 034f12b

File tree

6 files changed

+64
-46
lines changed

6 files changed

+64
-46
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: 10 additions & 1 deletion
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,
@@ -1189,6 +1190,14 @@ class Index(IndexOpsMixin[S1], ElementOpsMixin[S1]):
11891190

11901191
@type_check_only
11911192
class _IndexSubclassBase(Index[S1], Generic[S1, GenericT_co]):
1193+
@overload
1194+
def to_numpy(
1195+
self: _IndexSubclassBase[Interval],
1196+
dtype: type[T_INTERVAL_NP],
1197+
copy: bool = False,
1198+
na_value: Scalar = ...,
1199+
**kwargs: Any,
1200+
) -> np_1darray: ...
11921201
@overload
11931202
def to_numpy(
11941203
self,
@@ -1206,7 +1215,7 @@ class _IndexSubclassBase(Index[S1], Generic[S1, GenericT_co]):
12061215
**kwargs: Any,
12071216
) -> np_1darray[GenericT]: ...
12081217
@overload
1209-
def to_numpy(
1218+
def to_numpy( # pyright: ignore[reportIncompatibleMethodOverride]
12101219
self,
12111220
dtype: DTypeLike,
12121221
copy: bool = False,

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/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -439,11 +439,11 @@
439439
_S = TypeVar("_S", bound=tuple[int, ...])
440440
# Separately define here so pytest works
441441
np_1darray: TypeAlias = np.ndarray[tuple[int], np.dtype[_G]]
442-
np_1darray_bool: TypeAlias = np.ndarray[tuple[int], np.bool_]
443-
np_1darray_str: TypeAlias = np.ndarray[tuple[int], np.str_]
444-
np_1darray_bytes: TypeAlias = np.ndarray[tuple[int], np.bytes_]
445-
np_1darray_complex: TypeAlias = np.ndarray[tuple[int], np.complexfloating]
446-
np_1darray_object: TypeAlias = np.ndarray[tuple[int], np.object_]
442+
np_1darray_bool: TypeAlias = np_1darray[np.bool_]
443+
np_1darray_str: TypeAlias = np_1darray[np.str_]
444+
np_1darray_bytes: TypeAlias = np_1darray[np.bytes_]
445+
np_1darray_complex: TypeAlias = np_1darray[np.complexfloating]
446+
np_1darray_object: TypeAlias = np_1darray[np.object_]
447447
np_1darray_intp: TypeAlias = np_1darray[np.intp]
448448
np_1darray_int64: TypeAlias = np_1darray[np.int64]
449449
np_1darray_anyint: TypeAlias = np_1darray[np.integer]

tests/series/test_series.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@
7373
np_1darray_dt,
7474
np_1darray_float,
7575
np_1darray_object,
76-
np_1darray_str,
7776
np_1darray_td,
7877
np_ndarray_num,
7978
pytest_warns_bounded,
@@ -2004,18 +2003,22 @@ def test_dtype_type() -> None:
20042003

20052004
def test_types_to_numpy() -> None:
20062005
s = pd.Series(["a", "b", "c"], dtype=str)
2007-
check(assert_type(s.to_numpy(), np_1darray_str), np_1darray_str)
2006+
check(assert_type(s.to_numpy(), np_1darray_object), np_1darray_object)
2007+
check( # <U1, not str_
2008+
assert_type(s.to_numpy(dtype="str", copy=True), np_1darray), np_1darray
2009+
)
2010+
check(assert_type(s.to_numpy(na_value=0), np_1darray_object), np_1darray_object)
20082011
check(
2009-
assert_type(s.to_numpy(dtype="str", copy=True), np_1darray_str), np_1darray_str
2012+
assert_type(s.to_numpy(na_value=np.int32(4)), np_1darray_object),
2013+
np_1darray_object,
20102014
)
2011-
check(assert_type(s.to_numpy(na_value=0), np_1darray_str), np_1darray_str)
2012-
check(assert_type(s.to_numpy(na_value=np.int32(4)), np_1darray_str), np_1darray_str)
20132015
check(
2014-
assert_type(s.to_numpy(na_value=np.float16(4)), np_1darray_str), np_1darray_str
2016+
assert_type(s.to_numpy(na_value=np.float16(4)), np_1darray_object),
2017+
np_1darray_object,
20152018
)
20162019
check(
2017-
assert_type(s.to_numpy(na_value=np.complex128(4, 7)), np_1darray_str),
2018-
np_1darray_str,
2020+
assert_type(s.to_numpy(na_value=np.complex128(4, 7)), np_1darray_object),
2021+
np_1darray_object,
20192022
)
20202023

20212024
check(assert_type(pd.Series().to_numpy(), np_1darray), np_1darray)
@@ -2024,7 +2027,7 @@ def test_types_to_numpy() -> None:
20242027
def test_to_numpy() -> None:
20252028
"""Test Series.to_numpy for different types."""
20262029
s_str = pd.Series(["a", "b", "c"], dtype=str)
2027-
check(assert_type(s_str.to_numpy(), np_1darray_str), np_1darray_str)
2030+
check(assert_type(s_str.to_numpy(), np_1darray_object), np_1darray_object)
20282031

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

tests/test_timefuncs.py

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,9 @@
3030
check,
3131
np_1darray,
3232
np_1darray_bool,
33-
np_1darray_bytes,
3433
np_1darray_dt,
3534
np_1darray_int64,
3635
np_1darray_object,
37-
np_1darray_str,
3836
np_1darray_td,
3937
pytest_warns_bounded,
4038
)
@@ -956,14 +954,8 @@ def test_series_types_to_numpy() -> None:
956954
np_1darray,
957955
dtype=np.integer,
958956
)
959-
check(
960-
assert_type(o_s.to_numpy(dtype="bytes", copy=True), np_1darray),
961-
np_1darray_bytes,
962-
)
963-
check(
964-
assert_type(i_s.to_numpy(dtype="bytes", copy=True), np_1darray),
965-
np_1darray_bytes,
966-
)
957+
check(assert_type(o_s.to_numpy(dtype="bytes", copy=True), np_1darray), np_1darray)
958+
check(assert_type(i_s.to_numpy(dtype="bytes", copy=True), np_1darray), np_1darray)
967959

968960
# passed dtype-like with statically known generic
969961
check(
@@ -991,13 +983,12 @@ def test_series_types_to_numpy() -> None:
991983
np_1darray,
992984
np.int64,
993985
)
994-
check(
995-
assert_type(o_s.to_numpy(dtype=np.bytes_), np_1darray_bytes), np_1darray_bytes
996-
)
997-
check(
998-
assert_type(i_s.to_numpy(dtype=np.bytes_), np_1darray_bytes), np_1darray_bytes
999-
)
1000-
check(assert_type(i_s.to_numpy(dtype=np.str_), np_1darray_str), np_1darray_str)
986+
# |S20, not bytes_
987+
check(assert_type(o_s.to_numpy(dtype=np.bytes_), np_1darray), np_1darray)
988+
# |S6, not bytes_
989+
check(assert_type(i_s.to_numpy(dtype=np.bytes_), np_1darray), np_1darray)
990+
# <U6, not str_
991+
check(assert_type(i_s.to_numpy(dtype=np.str_), np_1darray), np_1darray)
1001992

1002993

1003994
def test_index_types_to_numpy() -> None:
@@ -1056,10 +1047,8 @@ def test_index_types_to_numpy() -> None:
10561047
np_1darray,
10571048
dtype=np.integer,
10581049
)
1059-
check(
1060-
assert_type(i_i.to_numpy(dtype="bytes", copy=True), np_1darray),
1061-
np_1darray_bytes,
1062-
)
1050+
# |S6, not bytes_
1051+
check(assert_type(i_i.to_numpy(dtype="bytes", copy=True), np_1darray), np_1darray)
10631052

10641053
# passed dtype-like with statically known generic
10651054
check(
@@ -1077,9 +1066,8 @@ def test_index_types_to_numpy() -> None:
10771066
np_1darray,
10781067
np.int64,
10791068
)
1080-
check(
1081-
assert_type(i_i.to_numpy(dtype=np.bytes_), np_1darray_bytes), np_1darray_bytes
1082-
)
1069+
# |S6, not bytes_
1070+
check(assert_type(i_i.to_numpy(dtype=np.bytes_), np_1darray), np_1darray)
10831071

10841072

10851073
def test_to_timedelta_units() -> None:

0 commit comments

Comments
 (0)