Skip to content

Commit e2c11d0

Browse files
committed
update mypy and allow timestamp slicing
1 parent 8f48f32 commit e2c11d0

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

pandas-stubs/_libs/tslibs/timestamps.pyi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ from time import struct_time
1111
from typing import (
1212
ClassVar,
1313
Literal,
14+
SupportsIndex,
1415
overload,
1516
)
1617

@@ -48,7 +49,7 @@ _Nonexistent: TypeAlias = (
4849
Literal["raise", "NaT", "shift_backward", "shift_forward"] | Timedelta | timedelta
4950
)
5051

51-
class Timestamp(datetime):
52+
class Timestamp(datetime, SupportsIndex):
5253
min: ClassVar[Timestamp] # pyright: ignore[reportIncompatibleVariableOverride]
5354
max: ClassVar[Timestamp] # pyright: ignore[reportIncompatibleVariableOverride]
5455

@@ -309,3 +310,5 @@ class Timestamp(datetime):
309310
@property
310311
def unit(self) -> TimeUnit: ...
311312
def as_unit(self, unit: TimeUnit, round_ok: bool = ...) -> Self: ...
313+
# To support slicing
314+
def __index__(self) -> int: ...

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ types-pytz = ">= 2022.1.1"
3333
numpy = ">= 1.23.5"
3434

3535
[tool.poetry.group.dev.dependencies]
36-
mypy = "1.13.0"
36+
mypy = "1.14.1"
3737
pandas = "2.2.3"
3838
pyarrow = ">=10.0.1"
3939
pytest = ">=7.1.2"
40-
pyright = ">= 1.1.390"
40+
pyright = ">= 1.1.391"
4141
poethepoet = ">=0.16.5"
4242
loguru = ">=0.6.0"
4343
typing-extensions = ">=4.4.0"

tests/test_frame.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2409,14 +2409,12 @@ def test_indexslice_getitem():
24092409
.set_index(["x", "y"])
24102410
)
24112411
ind = pd.Index([2, 3])
2412-
# This next test is written this way to support both mypy 1.13 and newer
2413-
# versions of mypy and pyright that treat slice as a Generic due to
2414-
# a change in typeshed.
2415-
# Once pyright 1.1.390 and mypy 1.14 are released, the test can be
2416-
# reverted to the standard form.
2417-
# check(assert_type(pd.IndexSlice[ind, :], tuple["pd.Index[int]", slice]), tuple)
2418-
tmp = cast(tuple["pd.Index[int]", slice], pd.IndexSlice[ind, :]) # type: ignore[redundant-cast]
2419-
check(assert_type(tmp, tuple["pd.Index[int]", slice]), tuple)
2412+
check(
2413+
assert_type(
2414+
pd.IndexSlice[ind, :], tuple["pd.Index[int]", "slice[None, None, None]"]
2415+
),
2416+
tuple,
2417+
)
24202418
check(assert_type(df.loc[pd.IndexSlice[ind, :]], pd.DataFrame), pd.DataFrame)
24212419
check(assert_type(df.loc[pd.IndexSlice[1:2]], pd.DataFrame), pd.DataFrame)
24222420
check(

tests/test_series.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3435,3 +3435,22 @@ def test_series_unique_timedelta() -> None:
34353435
"""Test type return of Series.unique on Series[timedeta64[ns]]."""
34363436
sr = pd.Series([pd.Timedelta("1 days"), pd.Timedelta("3 days")])
34373437
check(assert_type(sr.unique(), TimedeltaArray), TimedeltaArray)
3438+
3439+
3440+
def test_slice_timestamp() -> None:
3441+
dti = pd.date_range("1/1/2025", "2/28/2025")
3442+
3443+
s = pd.Series([i for i in range(len(dti))], index=dti)
3444+
3445+
# For `s1`, see discussion in GH 397. Needs mypy fix.
3446+
# s1 = s.loc["2025-01-15":"2025-01-20"]
3447+
3448+
# GH 397
3449+
check(
3450+
assert_type(
3451+
s.loc[pd.Timestamp("2025-01-15") : pd.Timestamp("2025-01-20")],
3452+
"pd.Series[int]",
3453+
),
3454+
pd.Series,
3455+
np.integer,
3456+
)

0 commit comments

Comments
 (0)