From 974fee39262e00a6eeec7bb5f5d5397c523adbf9 Mon Sep 17 00:00:00 2001 From: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Fri, 28 Feb 2025 18:09:13 +0000 Subject: [PATCH] type datetimeindex max/min reductions --- pandas-stubs/core/indexes/datetimelike.pyi | 18 ++++++++++++++---- tests/test_indexes.py | 8 ++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/pandas-stubs/core/indexes/datetimelike.pyi b/pandas-stubs/core/indexes/datetimelike.pyi index fb27fae21..8ff1ae144 100644 --- a/pandas-stubs/core/indexes/datetimelike.pyi +++ b/pandas-stubs/core/indexes/datetimelike.pyi @@ -1,3 +1,4 @@ +import numpy as np from pandas.core.indexes.extension import ExtensionIndex from pandas.core.indexes.timedeltas import TimedeltaIndex from typing_extensions import Self @@ -5,6 +6,7 @@ from typing_extensions import Self from pandas._libs.tslibs import BaseOffset from pandas._typing import ( S1, + AxisIndex, TimeUnit, ) @@ -15,10 +17,18 @@ class DatetimeIndexOpsMixin(ExtensionIndex[S1]): def freqstr(self) -> str | None: ... @property def is_all_dates(self) -> bool: ... - def min(self, axis=..., skipna: bool = ..., *args, **kwargs): ... - def argmin(self, axis=..., skipna: bool = ..., *args, **kwargs): ... - def max(self, axis=..., skipna: bool = ..., *args, **kwargs): ... - def argmax(self, axis=..., skipna: bool = ..., *args, **kwargs): ... + def min( + self, axis: AxisIndex | None = ..., skipna: bool = ..., *args, **kwargs + ) -> S1: ... + def argmin( + self, axis: AxisIndex | None = ..., skipna: bool = ..., *args, **kwargs + ) -> np.int64: ... + def max( + self, axis: AxisIndex | None = ..., skipna: bool = ..., *args, **kwargs + ) -> S1: ... + def argmax( + self, axis: AxisIndex | None = ..., skipna: bool = ..., *args, **kwargs + ) -> np.int64: ... def __rsub__( # type: ignore[override] # pyright: ignore[reportIncompatibleMethodOverride] self, other: DatetimeIndexOpsMixin ) -> TimedeltaIndex: ... diff --git a/tests/test_indexes.py b/tests/test_indexes.py index e77371cfb..b6becf357 100644 --- a/tests/test_indexes.py +++ b/tests/test_indexes.py @@ -1201,3 +1201,11 @@ def test_disallow_empty_index() -> None: # From GH 826 if TYPE_CHECKING_INVALID_USAGE: i0 = pd.Index() # type: ignore[call-overload] # pyright: ignore[reportCallIssue] + + +def test_datetime_index_max_min_reductions() -> None: + dtidx = pd.DatetimeIndex(["2020-01-01", "2020-01-02"]) + check(assert_type(dtidx.argmax(), np.int64), np.int64) + check(assert_type(dtidx.argmin(), np.int64), np.int64) + check(assert_type(dtidx.max(), pd.Timestamp), pd.Timestamp) + check(assert_type(dtidx.min(), pd.Timestamp), pd.Timestamp)