Skip to content

Commit f1882b7

Browse files
authored
[fix]: pandas.Index.to_numpy() method's arguments not properly typed (#270)
* [fix]: pandas.Index.to_numpy() method's arguments not properly typed After cloning a fresh copy of Pandas. After generating new stubs using stubtest. After looking for all the type signature for to_numpy method. There was nothing to update in the following files: - ./_libs/tslibs/nattype.pyi - ./_libs/tslibs/timedeltas.pyi There was modification to do in the following files: - ./core/arrays/masked.pyi - ./core/arrays/numpy_.pyi - ./core/arrays/base.pyi - /core/frame.pyi - ./core/base.pyi The type signature was removed from the following file: - ./core/indexes/base.pyi Refs #237 * [fix]: pandas.Index.to_numpy() method's arguments not properly typed After cloning a fresh copy of Pandas. After generating new stubs using stubtest. After looking for all the type signature for to_numpy method. There was nothing to update in the following files: - _libs/tslibs/nattype.pyi - _libs/tslibs/timedeltas.pyi There was modification to do in the following files: - core/arrays/masked.pyi - core/arrays/numpy_.pyi - core/arrays/base.pyi - core/frame.pyi - core/base.pyi The type signature was removed from the following file: - ./core/indexes/base.pyi Some unit tests for to_numpy method were added in the following files: - tests/test_frame.py - tests/test_series.py - tests/test_timefuncs.py Refs #237 * [fix]: pandas.Index.to_numpy() method's arguments not properly typed After cloning a fresh copy of Pandas. After generating new stubs using stubtest. After looking for all the type signature for to_numpy method. There was nothing to update in the following files: - _libs/tslibs/nattype.pyi - _libs/tslibs/timedeltas.pyi There was modification to do in the following files: - core/arrays/masked.pyi - core/arrays/numpy_.pyi - core/arrays/base.pyi - core/frame.pyi - core/base.pyi The type signature was removed from the following file: - ./core/indexes/base.pyi Some unit tests for to_numpy method were added in the following files: - tests/test_frame.py - tests/test_series.py - tests/test_timefuncs.py Refs #237 * [fix]: pandas.Index.to_numpy() method's arguments not properly typed After cloning a fresh copy of Pandas. After generating new stubs using stubtest. After looking for all the type signature for to_numpy method. There was nothing to update in the following files: - _libs/tslibs/nattype.pyi - _libs/tslibs/timedeltas.pyi There was modification to do in the following files: - core/arrays/masked.pyi - core/arrays/numpy_.pyi - core/arrays/base.pyi - core/frame.pyi - core/base.pyi The type signature was removed from the following file: - ./core/indexes/base.pyi Some unit tests for to_numpy method were added in the following files: - tests/test_frame.py - tests/test_series.py - tests/test_timefuncs.py Refs #237 * [fix]: pandas.Index.to_numpy() method's arguments not properly typed After cloning a fresh copy of Pandas. After generating new stubs using stubtest. After looking for all the type signature for to_numpy method. There was nothing to update in the following files: - _libs/tslibs/nattype.pyi - _libs/tslibs/timedeltas.pyi There was modification to do in the following files: - core/arrays/masked.pyi - core/arrays/numpy_.pyi - core/arrays/base.pyi - core/frame.pyi - core/base.pyi The type signature was removed from the following file: - core/indexes/base.pyi Some unit tests for to_numpy method were added in the following files: - tests/test_frame.py - tests/test_series.py - tests/test_indexes.py - tests/test_timefuncs.py Refs #237
1 parent d310058 commit f1882b7

File tree

11 files changed

+72
-18
lines changed

11 files changed

+72
-18
lines changed

pandas-stubs/core/arrays/base.pyi

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ from typing import Sequence
22

33
import numpy as np
44

5-
from pandas._typing import ArrayLike
5+
from pandas._typing import (
6+
ArrayLike,
7+
Scalar,
8+
npt,
9+
)
610

711
from pandas.core.dtypes.dtypes import ExtensionDtype as ExtensionDtype
812
from pandas.core.dtypes.generic import ABCExtensionArray
@@ -12,7 +16,12 @@ class ExtensionArray:
1216
def __setitem__(self, key: int | slice | np.ndarray, value) -> None: ...
1317
def __len__(self) -> int: ...
1418
def __iter__(self): ...
15-
def to_numpy(self, dtype=..., copy: bool = ..., na_value=...): ...
19+
def to_numpy(
20+
self,
21+
dtype: npt.DTypeLike | None = ...,
22+
copy: bool = ...,
23+
na_value: Scalar = ...,
24+
) -> np.ndarray: ...
1625
@property
1726
def dtype(self) -> ExtensionDtype: ...
1827
@property

pandas-stubs/core/arrays/masked.pyi

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,22 @@ from pandas.core.arrays import (
44
ExtensionOpsMixin,
55
)
66

7-
from pandas._typing import Scalar
7+
from pandas._typing import (
8+
Scalar,
9+
npt,
10+
)
811

912
class BaseMaskedArray(ExtensionArray, ExtensionOpsMixin):
1013
def __getitem__(self, item): ...
1114
def __iter__(self): ...
1215
def __len__(self) -> int: ...
1316
def __invert__(self): ...
14-
def to_numpy(self, dtype=..., copy=..., na_value: Scalar = ...): ...
17+
def to_numpy(
18+
self,
19+
dtype: npt.DTypeLike | None = ...,
20+
copy: bool = ...,
21+
na_value: Scalar = ...,
22+
) -> np.ndarray: ...
1523
__array_priority__: int = ...
1624
def __array__(self, dtype=...) -> np.ndarray: ...
1725
def __arrow_array__(self, type=...): ...

pandas-stubs/core/arrays/numpy_.pyi

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ from pandas.core.arrays.base import (
55
ExtensionOpsMixin,
66
)
77

8+
from pandas._typing import (
9+
Scalar,
10+
npt,
11+
)
12+
813
from pandas.core.dtypes.dtypes import ExtensionDtype
914

1015
class PandasDtype(ExtensionDtype):
@@ -109,6 +114,11 @@ class PandasArray(ExtensionArray, ExtensionOpsMixin, NDArrayOperatorsMixin):
109114
def skew(
110115
self, axis=..., dtype=..., out=..., keepdims: bool = ..., skipna: bool = ...
111116
): ...
112-
def to_numpy(self, dtype=..., copy: bool = ..., na_value=...): ...
117+
def to_numpy(
118+
self,
119+
dtype: npt.DTypeLike | None = ...,
120+
copy: bool = ...,
121+
na_value: Scalar = ...,
122+
) -> np.ndarray: ...
113123
def searchsorted(self, value, side: str = ..., sorter=...): ...
114124
def __invert__(self): ...

pandas-stubs/core/base.pyi

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ from pandas.core.arrays.categorical import Categorical
1212
from pandas._typing import (
1313
NaPosition,
1414
NDFrameT,
15+
Scalar,
1516
SeriesAxisType,
17+
npt,
1618
)
1719

1820
# TODO: These two moved to pandas.errors after switch to 1.5.x
@@ -45,7 +47,13 @@ class IndexOpsMixin:
4547
def size(self) -> int: ...
4648
@property
4749
def array(self) -> ExtensionArray: ...
48-
def to_numpy(self) -> np.ndarray: ...
50+
def to_numpy(
51+
self,
52+
dtype: npt.DTypeLike | None = ...,
53+
copy: bool = ...,
54+
na_value: Scalar = ...,
55+
**kwargs,
56+
) -> np.ndarray: ...
4957
@property
5058
def empty(self) -> bool: ...
5159
def max(self, axis=..., skipna: bool = ..., **kwargs): ...

pandas-stubs/core/frame.pyi

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ from pandas._typing import (
5353
ColspaceArgType,
5454
CompressionOptions,
5555
Dtype,
56-
DtypeNp,
5756
FilePath,
5857
FilePathOrBuffer,
5958
FillnaOptions,
@@ -225,9 +224,9 @@ class DataFrame(NDFrame, OpsMixin):
225224
) -> DataFrame: ...
226225
def to_numpy(
227226
self,
228-
dtype: type[DtypeNp] | Dtype | None = ...,
229-
copy: _bool = ...,
230-
na_value: Any | None = ...,
227+
dtype: npt.DTypeLike | None = ...,
228+
copy: bool = ...,
229+
na_value: Scalar = ...,
231230
) -> np.ndarray: ...
232231
@overload
233232
def to_dict(

pandas-stubs/core/indexes/base.pyi

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ class Index(IndexOpsMixin, PandasObject):
206206
def __eq__(self, other: object) -> np_ndarray_bool: ... # type: ignore[override]
207207
def __iter__(self) -> Iterator: ...
208208
def __ne__(self, other: object) -> np_ndarray_bool: ... # type: ignore[override]
209-
def to_numpy(self) -> np.ndarray: ...
210209

211210
def ensure_index_from_sequences(
212211
sequences: Sequence[Sequence[Dtype]], names: list[str] = ...

pandas-stubs/core/series.pyi

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ from pandas._typing import (
6565
Axis,
6666
AxisType,
6767
CompressionOptions,
68-
DtypeNp,
6968
DtypeObj,
7069
FilePathOrBuffer,
7170
FillnaOptions,
@@ -87,6 +86,7 @@ from pandas._typing import (
8786
SortKind,
8887
TimestampConvention,
8988
np_ndarray_anyint,
89+
npt,
9090
num,
9191
)
9292

@@ -1610,9 +1610,9 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
16101610
def to_list(self) -> list[S1]: ...
16111611
def to_numpy(
16121612
self,
1613-
dtype: type[DtypeNp] | None = ...,
1614-
copy: _bool = ...,
1615-
na_value=...,
1613+
dtype: npt.DTypeLike | None = ...,
1614+
copy: bool = ...,
1615+
na_value: Scalar = ...,
16161616
**kwargs,
16171617
) -> np.ndarray: ...
16181618
def tolist(self) -> list[S1]: ...

tests/test_frame.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -779,10 +779,10 @@ def test_types_cov() -> None:
779779

780780
def test_types_to_numpy() -> None:
781781
df = pd.DataFrame(data={"col1": [1, 1, 2], "col2": [3, 4, 5]})
782-
df.to_numpy()
783-
df.to_numpy(dtype="str", copy=True)
782+
check(assert_type(df.to_numpy(), np.ndarray), np.ndarray)
783+
check(assert_type(df.to_numpy(dtype="str", copy=True), np.ndarray), np.ndarray)
784784
# na_value param was added in 1.1.0 https://pandas.pydata.org/docs/whatsnew/v1.1.0.html
785-
df.to_numpy(na_value=0)
785+
check(assert_type(df.to_numpy(na_value=0), np.ndarray), np.ndarray)
786786

787787

788788
def test_to_markdown() -> None:

tests/test_indexes.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,10 @@ def test_index_neg():
9494
# GH 253
9595
idx = pd.Index([1, 2])
9696
check(assert_type(-idx, pd.Index), pd.Index)
97+
98+
99+
def test_types_to_numpy() -> None:
100+
idx = pd.Index([1, 2])
101+
check(assert_type(idx.to_numpy(), np.ndarray), np.ndarray)
102+
check(assert_type(idx.to_numpy(dtype="int", copy=True), np.ndarray), np.ndarray)
103+
check(assert_type(idx.to_numpy(na_value=0), np.ndarray), np.ndarray)

tests/test_series.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,3 +1168,10 @@ def test_dtype_type() -> None:
11681168
s3 = pd.Series([1, 2, 3])
11691169
check(assert_type(s3.dtype, DtypeObj), np.dtype)
11701170
check(assert_type(s3.dtype.kind, str), str)
1171+
1172+
1173+
def test_types_to_numpy() -> None:
1174+
s = pd.Series(["a", "b", "c"], dtype=str)
1175+
check(assert_type(s.to_numpy(), np.ndarray), np.ndarray)
1176+
check(assert_type(s.to_numpy(dtype="str", copy=True), np.ndarray), np.ndarray)
1177+
check(assert_type(s.to_numpy(na_value=0), np.ndarray), np.ndarray)

tests/test_timefuncs.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,3 +440,10 @@ def test_some_offsets() -> None:
440440
)
441441
# GH 224
442442
check(assert_type(dt.date.today() - Day(), dt.date), dt.date)
443+
444+
445+
def test_types_to_numpy() -> None:
446+
td_s = pd.to_timedelta(pd.Series([10, 20]), "minutes")
447+
check(assert_type(td_s.to_numpy(), np.ndarray), np.ndarray)
448+
check(assert_type(td_s.to_numpy(dtype="int", copy=True), np.ndarray), np.ndarray)
449+
check(assert_type(td_s.to_numpy(na_value=0), np.ndarray), np.ndarray)

0 commit comments

Comments
 (0)