Skip to content
Open
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]
) -> TimestampProperties | TimedeltaProperties | PeriodProperties: ...
@overload
def __get__(
self, instance: Series[Timestamp], owner: type[Series]
Expand Down
23 changes: 17 additions & 6 deletions tests/series/test_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from pandas.core.indexes.accessors import (
DatetimeProperties,
PeriodProperties,
Properties,
TimedeltaProperties,
)
from pandas.core.indexes.interval import interval_range
Expand Down Expand Up @@ -49,12 +48,24 @@ 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, "TimestampProperties | TimedeltaProperties | PeriodProperties"
),
DatetimeProperties,
)
check(
assert_type( # type: ignore[assert-type]
df["td"].dt, "TimestampProperties | TimedeltaProperties | PeriodProperties"
),
TimedeltaProperties,
)
Comment on lines +53 to +64
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 tests that show that one of the TimestampProperties works for df["ts"] and that one of the TimedeltaProperties works for df["td"] ?


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