Skip to content

Commit 79e4b96

Browse files
authored
TYP: #1519 #1524 dt descriptor (#1531)
* #1519 #1524 * #1531 (comment)
1 parent b9081da commit 79e4b96

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

pandas-stubs/core/indexes/accessors.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,9 @@ class TimedeltaIndexProperties(
444444
@type_check_only
445445
class DtDescriptor:
446446
@overload
447-
def __get__(self, instance: Series[Never], owner: type[Series]) -> Properties: ...
447+
def __get__(
448+
self, instance: Series[Never], owner: type[Series]
449+
) -> CombinedDatetimelikeProperties: ...
448450
@overload
449451
def __get__(
450452
self, instance: Series[Timestamp], owner: type[Series]

tests/series/test_properties.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
from pandas.core.arrays.timedeltas import TimedeltaArray
99
from pandas.core.frame import DataFrame
1010
from pandas.core.indexes.accessors import (
11+
CombinedDatetimelikeProperties,
1112
DatetimeProperties,
1213
PeriodProperties,
13-
Properties,
1414
TimedeltaProperties,
1515
)
1616
from pandas.core.indexes.interval import interval_range
@@ -49,12 +49,37 @@ def test_property_dt() -> None:
4949
PeriodProperties,
5050
)
5151

52+
df = DataFrame({"ts": [Timestamp(2025, 12, 6)], "td": [Timedelta(1, "s")]})
53+
# python/mypy#19952: mypy gives Any
54+
check(
55+
assert_type( # type: ignore[assert-type]
56+
df["ts"].dt, CombinedDatetimelikeProperties
57+
),
58+
DatetimeProperties,
59+
)
60+
check(
61+
assert_type( # type: ignore[assert-type]
62+
df["td"].dt, CombinedDatetimelikeProperties
63+
),
64+
TimedeltaProperties,
65+
)
66+
67+
check(
68+
assert_type(df["ts"].dt.year, "Series[int]"), # type: ignore[assert-type]
69+
Series,
70+
np.integer,
71+
)
72+
check(
73+
assert_type( # type: ignore[assert-type]
74+
df["td"].dt.total_seconds(), "Series[float]"
75+
),
76+
Series,
77+
np.floating,
78+
)
79+
5280
if TYPE_CHECKING_INVALID_USAGE:
53-
s = DataFrame({"a": [1]})["a"]
54-
# python/mypy#19952: mypy believes Properties and its subclasses have a
55-
# conflict and gives Any for s.dt
56-
assert_type(s.dt, Properties) # type: ignore[assert-type]
57-
_1 = Series([1]).dt # type: ignore[arg-type] # pyright: ignore[reportAttributeAccessIssue]
81+
_0 = Series([1]).dt # type: ignore[arg-type] # pyright: ignore[reportAttributeAccessIssue]
82+
_1 = Series(["2025-01-01"]).dt # type: ignore[arg-type] # pyright: ignore[reportAttributeAccessIssue]
5883

5984

6085
def test_property_array() -> None:

0 commit comments

Comments
 (0)