Skip to content

Fix DataFrame.xs and Series.dtype #266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas-stubs/core/generic.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ...,
Expand Down
6 changes: 3 additions & 3 deletions pandas-stubs/core/series.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ from pandas._typing import (
Axis,
AxisType,
CompressionOptions,
Dtype,
DtypeNp,
DtypeObj,
FilePathOrBuffer,
FillnaOptions,
GroupByObjectNonScalar,
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 9 additions & 0 deletions tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1714,3 +1714,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
)
25 changes: 23 additions & 2 deletions tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)