Skip to content

Commit 80f561f

Browse files
amotzopAmotz
and
Amotz
authored
Seris and DataFrame creation typing (#230)
* Cleaned uptyping on __new__ for Series and DataFrame * Added tests * Added checkoing to the test Co-authored-by: Amotz <[email protected]>
1 parent fa84c4c commit 80f561f

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

pandas-stubs/core/frame.pyi

+10
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ class DataFrame(NDFrame, OpsMixin):
171171

172172
__hash__: ClassVar[None] # type: ignore[assignment]
173173

174+
@overload
174175
def __new__(
175176
cls,
176177
data: ListLikeU
@@ -183,6 +184,15 @@ class DataFrame(NDFrame, OpsMixin):
183184
dtype=...,
184185
copy: _bool = ...,
185186
) -> DataFrame: ...
187+
@overload
188+
def __new__(
189+
cls,
190+
data: Scalar,
191+
index: Axes,
192+
columns: Axes,
193+
dtype=...,
194+
copy: _bool = ...,
195+
) -> DataFrame: ...
186196
@property
187197
def axes(self) -> list[Index]: ...
188198
@property

pandas-stubs/core/series.pyi

+6-6
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,14 @@ class _LocIndexerSeries(_LocIndexer, Generic[S1]):
124124
idx: MaskType
125125
| Index
126126
| Sequence[float]
127-
| list[_str]
127+
| list[str]
128128
| slice
129129
| tuple[str | float | slice | Index, ...],
130130
) -> Series[S1]: ...
131131
@overload
132132
def __getitem__(
133133
self,
134-
idx: _str | float,
134+
idx: str | float,
135135
) -> S1: ...
136136
@overload
137137
def __setitem__(
@@ -161,7 +161,7 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
161161
def __new__(
162162
cls,
163163
data: DatetimeIndex,
164-
index: _str | int | Series | list | Index | None = ...,
164+
index: Axes | None = ...,
165165
dtype=...,
166166
name: Hashable | None = ...,
167167
copy: bool = ...,
@@ -171,7 +171,7 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
171171
def __new__(
172172
cls,
173173
data: PeriodIndex,
174-
index: _str | int | Series | list | Index | None = ...,
174+
index: Axes | None = ...,
175175
dtype=...,
176176
name: Hashable | None = ...,
177177
copy: bool = ...,
@@ -182,7 +182,7 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
182182
cls,
183183
data: object | _ListLike | Series[S1] | dict[int, S1] | dict[_str, S1] | None,
184184
dtype: type[S1],
185-
index: _str | int | Series | list | Index | None = ...,
185+
index: Axes | None = ...,
186186
name: Hashable | None = ...,
187187
copy: bool = ...,
188188
fastpath: bool = ...,
@@ -196,7 +196,7 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
196196
| dict[int, S1]
197197
| dict[_str, S1]
198198
| None = ...,
199-
index: _str | int | Series | list | Index | None = ...,
199+
index: Axes | None = ...,
200200
dtype=...,
201201
name: Hashable | None = ...,
202202
copy: bool = ...,

tests/test_frame.py

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ def test_types_init() -> None:
4242
dtype=np.int8,
4343
copy=True,
4444
)
45+
check(
46+
assert_type(pd.DataFrame(0, index=[0, 1], columns=[0, 1]), pd.DataFrame),
47+
pd.DataFrame,
48+
)
4549

4650

4751
def test_types_all() -> None:

0 commit comments

Comments
 (0)