Skip to content

Commit f61ded5

Browse files
GH1409 Improve Series.to_numpy typing
1 parent a22516c commit f61ded5

File tree

2 files changed

+131
-6
lines changed

2 files changed

+131
-6
lines changed

pandas-stubs/core/series.pyi

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4494,6 +4494,54 @@ class Series(IndexOpsMixin[S1], ElementOpsMixin[S1], NDFrame):
44944494
**kwargs: Any,
44954495
) -> np_1darray[_T_INTERVAL_NP]: ...
44964496
@overload
4497+
def to_numpy(
4498+
self: Series[int],
4499+
dtype: DTypeLike | None = None,
4500+
copy: bool = False,
4501+
na_value: Scalar = ...,
4502+
**kwargs: Any,
4503+
) -> np_1darray[np.integer]: ...
4504+
@overload
4505+
def to_numpy(
4506+
self: Series[float],
4507+
dtype: DTypeLike | None = None,
4508+
copy: bool = False,
4509+
na_value: Scalar = ...,
4510+
**kwargs: Any,
4511+
) -> np_1darray[np.floating]: ...
4512+
@overload
4513+
def to_numpy(
4514+
self: Series[complex],
4515+
dtype: DTypeLike | None = None,
4516+
copy: bool = False,
4517+
na_value: Scalar = ...,
4518+
**kwargs: Any,
4519+
) -> np_1darray[np.complexfloating]: ...
4520+
@overload
4521+
def to_numpy(
4522+
self: Series[bool],
4523+
dtype: DTypeLike | None = None,
4524+
copy: bool = False,
4525+
na_value: Scalar = ...,
4526+
**kwargs: Any,
4527+
) -> np_1darray[np.bool_]: ...
4528+
@overload
4529+
def to_numpy(
4530+
self: Series[_str],
4531+
dtype: DTypeLike | None = None,
4532+
copy: bool = False,
4533+
na_value: Scalar = ...,
4534+
**kwargs: Any,
4535+
) -> np_1darray[np.str_]: ...
4536+
@overload
4537+
def to_numpy(
4538+
self: Series[bytes],
4539+
dtype: DTypeLike | None = None,
4540+
copy: bool = False,
4541+
na_value: Scalar = ...,
4542+
**kwargs: Any,
4543+
) -> np_1darray[np.bytes_]: ...
4544+
@overload
44974545
def to_numpy( # pyright: ignore[reportIncompatibleMethodOverride]
44984546
self,
44994547
dtype: DTypeLike | None = None,

tests/series/test_series.py

Lines changed: 83 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
)
4646
import xarray as xr
4747

48+
from pandas._libs.tslibs.offsets import Day
4849
from pandas._typing import (
4950
DtypeObj,
5051
Scalar,
@@ -1980,16 +1981,92 @@ def test_dtype_type() -> None:
19801981

19811982
def test_types_to_numpy() -> None:
19821983
s = pd.Series(["a", "b", "c"], dtype=str)
1983-
check(assert_type(s.to_numpy(), np_1darray), np_1darray)
1984-
check(assert_type(s.to_numpy(dtype="str", copy=True), np_1darray), np_1darray)
1985-
check(assert_type(s.to_numpy(na_value=0), np_1darray), np_1darray)
1986-
check(assert_type(s.to_numpy(na_value=np.int32(4)), np_1darray), np_1darray)
1987-
check(assert_type(s.to_numpy(na_value=np.float16(4)), np_1darray), np_1darray)
1988-
check(assert_type(s.to_numpy(na_value=np.complex128(4, 7)), np_1darray), np_1darray)
1984+
check(assert_type(s.to_numpy(), np_1darray[np.str_]), np_1darray)
1985+
check(
1986+
assert_type(s.to_numpy(dtype="str", copy=True), np_1darray[np.str_]), np_1darray
1987+
)
1988+
check(assert_type(s.to_numpy(na_value=0), np_1darray[np.str_]), np_1darray)
1989+
check(
1990+
assert_type(s.to_numpy(na_value=np.int32(4)), np_1darray[np.str_]), np_1darray
1991+
)
1992+
check(
1993+
assert_type(s.to_numpy(na_value=np.float16(4)), np_1darray[np.str_]), np_1darray
1994+
)
1995+
check(
1996+
assert_type(s.to_numpy(na_value=np.complex128(4, 7)), np_1darray[np.str_]),
1997+
np_1darray,
1998+
)
19891999

19902000
check(assert_type(pd.Series().to_numpy(), np_1darray), np_1darray)
19912001

19922002

2003+
def test_to_numpy() -> None:
2004+
"""Test Series.to_numpy for different types."""
2005+
s1 = pd.Series(["a", "b", "c"], dtype=str)
2006+
check(assert_type(s1.to_numpy(), np_1darray[np.str_]), np_1darray, str)
2007+
2008+
s2 = pd.Series(["a", "b", "c"]).astype(bytes)
2009+
check(assert_type(s2.to_numpy(), np_1darray[np.bytes_]), np_1darray, np.bytes_)
2010+
2011+
s3 = pd.Series([True, False])
2012+
check(assert_type(s3.to_numpy(), np_1darray[np.bool_]), np_1darray, np.bool_)
2013+
2014+
s4 = pd.Series([2, 3, 4])
2015+
check(assert_type(s4.to_numpy(), np_1darray[np.integer]), np_1darray, np.integer)
2016+
2017+
s5 = pd.Series([2.0, 3.54, 4.84])
2018+
check(assert_type(s5.to_numpy(), np_1darray[np.floating]), np_1darray, np.floating)
2019+
2020+
s6 = pd.Series([2.0 + 2j, 3.54 + 4j, 4.84])
2021+
check(
2022+
assert_type(s6.to_numpy(), np_1darray[np.complexfloating]),
2023+
np_1darray,
2024+
np.complexfloating,
2025+
)
2026+
2027+
dates = pd.Series(
2028+
[
2029+
pd.Timestamp("2020-01-01"),
2030+
pd.Timestamp("2020-01-15"),
2031+
pd.Timestamp("2020-02-01"),
2032+
],
2033+
dtype="datetime64[ns]",
2034+
)
2035+
s7 = pd.Series(pd.PeriodIndex(dates, freq="M"))
2036+
check(assert_type(s7.to_numpy(), np_1darray[np.object_]), np_1darray, pd.Period)
2037+
2038+
s8 = pd.Series(
2039+
[
2040+
pd.Interval(date, date + pd.DateOffset(days=1), closed="left")
2041+
for date in dates
2042+
]
2043+
)
2044+
check(assert_type(s8.to_numpy(), np_1darray[np.object_]), np_1darray, pd.Interval)
2045+
2046+
s9 = (
2047+
pd.Series(pd.period_range(start="2017-01-01", end="2017-02-01", freq="1D"))
2048+
.diff()
2049+
.iloc[1:]
2050+
)
2051+
check(assert_type(s9.to_numpy(), np_1darray[np.object_]), np_1darray, Day)
2052+
2053+
s10 = pd.Series(pd.date_range(start="2017-01-01", end="2017-02-01"))
2054+
check(
2055+
assert_type(s10.to_numpy(), np_1darray[np.datetime64]),
2056+
np_1darray,
2057+
np.datetime64,
2058+
)
2059+
2060+
s11 = pd.Series(
2061+
[datetime.datetime.now().date(), datetime.datetime.now().date()]
2062+
).diff()
2063+
check(
2064+
assert_type(s11.to_numpy(), np_1darray[np.timedelta64]),
2065+
np_1darray,
2066+
np.timedelta64,
2067+
)
2068+
2069+
19932070
def test_where() -> None:
19942071
s = pd.Series([1, 2, 3], dtype=int)
19952072

0 commit comments

Comments
 (0)