From 040130d0e8586cd2b45e849b845c445efe645f45 Mon Sep 17 00:00:00 2001 From: Irv Lustig Date: Sun, 4 Sep 2022 17:47:03 -0400 Subject: [PATCH 1/2] WIP: fix xs issue. Bump to 1.4.4 --- pandas-stubs/core/generic.pyi | 2 +- pyproject.toml | 2 +- tests/test_frame.py | 9 +++++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/pandas-stubs/core/generic.pyi b/pandas-stubs/core/generic.pyi index 8dfcba55c..d09bde80d 100644 --- a/pandas-stubs/core/generic.pyi +++ b/pandas-stubs/core/generic.pyi @@ -282,7 +282,7 @@ class NDFrame(PandasObject, indexing.IndexingMixin): ) -> NDFrame: ... def xs( self, - key: _str | tuple[_str], + key: Hashable, axis: SeriesAxisType = ..., level: Level | None = ..., drop_level: _bool = ..., diff --git a/pyproject.toml b/pyproject.toml index e0adeb4a2..4bd2f92bd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -42,7 +42,7 @@ pytest = ">=7.1.2" pyright = ">=1.1.266" poethepoet = "0.16.0" loguru = ">=0.6.0" -pandas = "1.4.3" +pandas = "1.4.4" typing-extensions = ">=4.2.0" matplotlib = ">=3.3.2" pre-commit = ">=2.19.0" diff --git a/tests/test_frame.py b/tests/test_frame.py index 17a46951c..d2ca811ee 100644 --- a/tests/test_frame.py +++ b/tests/test_frame.py @@ -1745,3 +1745,12 @@ def test_pos() -> None: # GH 253 df = pd.DataFrame(data={"col1": [1, 2], "col2": [3, 4]}) check(assert_type(+df, pd.DataFrame), pd.DataFrame) + + +def test_xs_key() -> None: + # GH 214 + mi = pd.MultiIndex.from_product([[0, 1], [0, 1]], names=["foo", "bar"]) + df = pd.DataFrame({"x": [10, 20, 30, 40], "y": [50, 60, 70, 80]}, index=mi) + check( + assert_type(df.xs(0, level="foo"), Union[pd.DataFrame, pd.Series]), pd.DataFrame + ) From 4967e44e05d68b6a0a0501a6ee7376da3f82b60b Mon Sep 17 00:00:00 2001 From: Irv Lustig Date: Sun, 4 Sep 2022 18:48:37 -0400 Subject: [PATCH 2/2] Fix xs arguments and dtype return type --- pandas-stubs/core/series.pyi | 6 +++--- tests/test_series.py | 25 +++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/pandas-stubs/core/series.pyi b/pandas-stubs/core/series.pyi index 987b93ae8..324796736 100644 --- a/pandas-stubs/core/series.pyi +++ b/pandas-stubs/core/series.pyi @@ -65,8 +65,8 @@ from pandas._typing import ( Axis, AxisType, CompressionOptions, - Dtype, DtypeNp, + DtypeObj, FilePathOrBuffer, FillnaOptions, GroupByObjectNonScalar, @@ -220,9 +220,9 @@ class Series(IndexOpsMixin, NDFrame, Generic[S1]): axis: SeriesAxisType = ..., ) -> Series[S1]: ... @property - def dtype(self) -> Dtype: ... + def dtype(self) -> DtypeObj: ... @property - def dtypes(self) -> Dtype: ... + def dtypes(self) -> DtypeObj: ... @property def name(self) -> Hashable | None: ... @name.setter diff --git a/tests/test_series.py b/tests/test_series.py index d82e9f59f..c51d759f5 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -19,13 +19,19 @@ import numpy as np import pandas as pd from pandas._testing import ensure_clean -from pandas.api.extensions import ExtensionArray +from pandas.api.extensions import ( + ExtensionArray, + ExtensionDtype, +) from pandas.core.window import ExponentialMovingWindow import pytest from typing_extensions import assert_type import xarray as xr -from pandas._typing import Scalar +from pandas._typing import ( + DtypeObj, + Scalar, +) from tests import ( TYPE_CHECKING_INVALID_USAGE, @@ -1139,3 +1145,18 @@ def test_neg() -> None: sr_int = pd.Series([1, 2, 3], dtype=int) check(assert_type(-sr, pd.Series), pd.Series) check(assert_type(-sr_int, "pd.Series[int]"), pd.Series, int) + + +def test_dtype_type() -> None: + # GH 216 + s1 = pd.Series(["foo"], dtype="string") + check(assert_type(s1.dtype, DtypeObj), ExtensionDtype) + check(assert_type(s1.dtype.kind, str), str) + + s2 = pd.Series([1], dtype="Int64") + check(assert_type(s2.dtype, DtypeObj), ExtensionDtype) + check(assert_type(s2.dtype.kind, str), str) + + s3 = pd.Series([1, 2, 3]) + check(assert_type(s3.dtype, DtypeObj), np.dtype) + check(assert_type(s3.dtype.kind, str), str)