Skip to content
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
4 changes: 3 additions & 1 deletion pandas-stubs/core/indexes/accessors.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,9 @@ class TimedeltaIndexProperties(
@type_check_only
class DtDescriptor:
@overload
def __get__(self, instance: Series[Never], owner: type[Series]) -> Properties: ...
def __get__(
self, instance: Series[Never], owner: type[Series]
) -> CombinedDatetimelikeProperties: ...
@overload
def __get__(
self, instance: Series[Timestamp], owner: type[Series]
Expand Down
37 changes: 31 additions & 6 deletions tests/series/test_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
from pandas.core.arrays.timedeltas import TimedeltaArray
from pandas.core.frame import DataFrame
from pandas.core.indexes.accessors import (
CombinedDatetimelikeProperties,
DatetimeProperties,
PeriodProperties,
Properties,
TimedeltaProperties,
)
from pandas.core.indexes.interval import interval_range
Expand Down Expand Up @@ -49,12 +49,37 @@ def test_property_dt() -> None:
PeriodProperties,
)

df = DataFrame({"ts": [Timestamp(2025, 12, 6)], "td": [Timedelta(1, "s")]})
# python/mypy#19952: mypy gives Any
check(
assert_type( # type: ignore[assert-type]
df["ts"].dt, CombinedDatetimelikeProperties
),
DatetimeProperties,
)
check(
assert_type( # type: ignore[assert-type]
df["td"].dt, CombinedDatetimelikeProperties
),
TimedeltaProperties,
)

check(
assert_type(df["ts"].dt.year, "Series[int]"), # type: ignore[assert-type]
Series,
np.integer,
)
check(
assert_type( # type: ignore[assert-type]
Comment on lines +66 to +73
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment here as to why we need the type: ignore with mypy ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added above: # python/mypy#19952: mypy gives Any. It is python/mypy#19952 which has unfortunately been closed.

df["td"].dt.total_seconds(), "Series[float]"
),
Series,
np.floating,
)

if TYPE_CHECKING_INVALID_USAGE:
s = DataFrame({"a": [1]})["a"]
# python/mypy#19952: mypy believes Properties and its subclasses have a
# conflict and gives Any for s.dt
assert_type(s.dt, Properties) # type: ignore[assert-type]
_1 = Series([1]).dt # type: ignore[arg-type] # pyright: ignore[reportAttributeAccessIssue]
_0 = Series([1]).dt # type: ignore[arg-type] # pyright: ignore[reportAttributeAccessIssue]
_1 = Series(["2025-01-01"]).dt # type: ignore[arg-type] # pyright: ignore[reportAttributeAccessIssue]


def test_property_array() -> None:
Expand Down