Skip to content

Commit 6e002f8

Browse files
authored
Fix DataFrame.xs and Series.dtype (#266)
* WIP: fix xs issue. Bump to 1.4.4 * Fix xs arguments and dtype return type
1 parent fbe591f commit 6e002f8

File tree

5 files changed

+37
-7
lines changed

5 files changed

+37
-7
lines changed

pandas-stubs/core/generic.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ class NDFrame(PandasObject, indexing.IndexingMixin):
282282
) -> NDFrame: ...
283283
def xs(
284284
self,
285-
key: _str | tuple[_str],
285+
key: Hashable,
286286
axis: SeriesAxisType = ...,
287287
level: Level | None = ...,
288288
drop_level: _bool = ...,

pandas-stubs/core/series.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ from pandas._typing import (
6565
Axis,
6666
AxisType,
6767
CompressionOptions,
68-
Dtype,
6968
DtypeNp,
69+
DtypeObj,
7070
FilePathOrBuffer,
7171
FillnaOptions,
7272
GroupByObjectNonScalar,
@@ -220,9 +220,9 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]):
220220
axis: SeriesAxisType = ...,
221221
) -> Series[S1]: ...
222222
@property
223-
def dtype(self) -> Dtype: ...
223+
def dtype(self) -> DtypeObj: ...
224224
@property
225-
def dtypes(self) -> Dtype: ...
225+
def dtypes(self) -> DtypeObj: ...
226226
@property
227227
def name(self) -> Hashable | None: ...
228228
@name.setter

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pytest = ">=7.1.2"
4242
pyright = ">=1.1.266"
4343
poethepoet = "0.16.0"
4444
loguru = ">=0.6.0"
45-
pandas = "1.4.3"
45+
pandas = "1.4.4"
4646
typing-extensions = ">=4.2.0"
4747
matplotlib = ">=3.3.2"
4848
pre-commit = ">=2.19.0"

tests/test_frame.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,3 +1714,12 @@ def test_pos() -> None:
17141714
# GH 253
17151715
df = pd.DataFrame(data={"col1": [1, 2], "col2": [3, 4]})
17161716
check(assert_type(+df, pd.DataFrame), pd.DataFrame)
1717+
1718+
1719+
def test_xs_key() -> None:
1720+
# GH 214
1721+
mi = pd.MultiIndex.from_product([[0, 1], [0, 1]], names=["foo", "bar"])
1722+
df = pd.DataFrame({"x": [10, 20, 30, 40], "y": [50, 60, 70, 80]}, index=mi)
1723+
check(
1724+
assert_type(df.xs(0, level="foo"), Union[pd.DataFrame, pd.Series]), pd.DataFrame
1725+
)

tests/test_series.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,19 @@
1919
import numpy as np
2020
import pandas as pd
2121
from pandas._testing import ensure_clean
22-
from pandas.api.extensions import ExtensionArray
22+
from pandas.api.extensions import (
23+
ExtensionArray,
24+
ExtensionDtype,
25+
)
2326
from pandas.core.window import ExponentialMovingWindow
2427
import pytest
2528
from typing_extensions import assert_type
2629
import xarray as xr
2730

28-
from pandas._typing import Scalar
31+
from pandas._typing import (
32+
DtypeObj,
33+
Scalar,
34+
)
2935

3036
from tests import (
3137
TYPE_CHECKING_INVALID_USAGE,
@@ -1139,3 +1145,18 @@ def test_neg() -> None:
11391145
sr_int = pd.Series([1, 2, 3], dtype=int)
11401146
check(assert_type(-sr, pd.Series), pd.Series)
11411147
check(assert_type(-sr_int, "pd.Series[int]"), pd.Series, int)
1148+
1149+
1150+
def test_dtype_type() -> None:
1151+
# GH 216
1152+
s1 = pd.Series(["foo"], dtype="string")
1153+
check(assert_type(s1.dtype, DtypeObj), ExtensionDtype)
1154+
check(assert_type(s1.dtype.kind, str), str)
1155+
1156+
s2 = pd.Series([1], dtype="Int64")
1157+
check(assert_type(s2.dtype, DtypeObj), ExtensionDtype)
1158+
check(assert_type(s2.dtype.kind, str), str)
1159+
1160+
s3 = pd.Series([1, 2, 3])
1161+
check(assert_type(s3.dtype, DtypeObj), np.dtype)
1162+
check(assert_type(s3.dtype.kind, str), str)

0 commit comments

Comments
 (0)