From cd569295ea474b07497aaa98f7c27e3fecd12d3a Mon Sep 17 00:00:00 2001 From: Jixun Sun <160219251+AnonToky@users.noreply.github.com> Date: Tue, 11 Nov 2025 13:13:37 +0800 Subject: [PATCH 01/22] Fix zip function call to include strict parameter --- pandas/core/reshape/encoding.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/reshape/encoding.py b/pandas/core/reshape/encoding.py index 24efa34870545..c719f15c0b0ca 100644 --- a/pandas/core/reshape/encoding.py +++ b/pandas/core/reshape/encoding.py @@ -538,7 +538,7 @@ def from_dummies( raise ValueError(len_msg) elif isinstance(default_category, Hashable): default_category = dict( - zip(variables_slice, [default_category] * len(variables_slice)) + zip(variables_slice, [default_category] * len(variables_slice), strict=True) ) else: raise TypeError( From bc08bff12cfb2c4c1e4139d8d8c429925293281e Mon Sep 17 00:00:00 2001 From: Jixun Sun <160219251+AnonToky@users.noreply.github.com> Date: Tue, 11 Nov 2025 13:23:53 +0800 Subject: [PATCH 02/22] handled the too-long modified --- pandas/core/reshape/encoding.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/reshape/encoding.py b/pandas/core/reshape/encoding.py index c719f15c0b0ca..1c414d0b6d2a0 100644 --- a/pandas/core/reshape/encoding.py +++ b/pandas/core/reshape/encoding.py @@ -538,7 +538,8 @@ def from_dummies( raise ValueError(len_msg) elif isinstance(default_category, Hashable): default_category = dict( - zip(variables_slice, [default_category] * len(variables_slice), strict=True) + zip(variables_slice, + [default_category] * len(variables_slice), strict=True) ) else: raise TypeError( From 74a09aedc740653a6178f2079471ae9d1f1aa3ee Mon Sep 17 00:00:00 2001 From: Jixun Sun <160219251+AnonToky@users.noreply.github.com> Date: Tue, 11 Nov 2025 13:35:42 +0800 Subject: [PATCH 03/22] edited --- pandas/core/reshape/encoding.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/reshape/encoding.py b/pandas/core/reshape/encoding.py index 1c414d0b6d2a0..b45660abda314 100644 --- a/pandas/core/reshape/encoding.py +++ b/pandas/core/reshape/encoding.py @@ -538,8 +538,8 @@ def from_dummies( raise ValueError(len_msg) elif isinstance(default_category, Hashable): default_category = dict( - zip(variables_slice, - [default_category] * len(variables_slice), strict=True) + zip(variables_slice, [default_category] * len(variables_slice), + strict=True) ) else: raise TypeError( From 2200f710430482a07ad74c914d77c58c346fc844 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 11 Nov 2025 08:52:59 +0000 Subject: [PATCH 04/22] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pandas/core/reshape/encoding.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pandas/core/reshape/encoding.py b/pandas/core/reshape/encoding.py index b45660abda314..35fed5a24fb30 100644 --- a/pandas/core/reshape/encoding.py +++ b/pandas/core/reshape/encoding.py @@ -538,8 +538,11 @@ def from_dummies( raise ValueError(len_msg) elif isinstance(default_category, Hashable): default_category = dict( - zip(variables_slice, [default_category] * len(variables_slice), - strict=True) + zip( + variables_slice, + [default_category] * len(variables_slice), + strict=True, + ) ) else: raise TypeError( From 5c9d1be6cb75a50e163cc068de56b409f331f3ed Mon Sep 17 00:00:00 2001 From: Jixun Sun <160219251+AnonToky@users.noreply.github.com> Date: Wed, 19 Nov 2025 21:14:45 +0800 Subject: [PATCH 05/22] Refactor datetimelike.py by cleaning up decorators Removed unused decorators and added docstrings to several methods. --- pandas/core/indexes/datetimelike.py | 148 +++++++++++++++++++++++++--- 1 file changed, 133 insertions(+), 15 deletions(-) diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index 58529c5597b6e..f1d324fec697c 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -36,9 +36,7 @@ NullFrequencyError, ) from pandas.util._decorators import ( - Appender, cache_readonly, - doc, ) from pandas.core.dtypes.common import ( @@ -57,12 +55,10 @@ PeriodArray, TimedeltaArray, ) -from pandas.core.arrays.datetimelike import DatetimeLikeArrayMixin import pandas.core.common as com import pandas.core.indexes.base as ibase from pandas.core.indexes.base import ( Index, - _index_shared_docs, ) from pandas.core.indexes.extension import NDArrayBackedExtensionIndex from pandas.core.indexes.range import RangeIndex @@ -92,8 +88,22 @@ class DatetimeIndexOpsMixin(NDArrayBackedExtensionIndex, ABC): _can_hold_strings = False _data: DatetimeArray | TimedeltaArray | PeriodArray - @doc(DatetimeLikeArrayMixin.mean) def mean(self, *, skipna: bool = True, axis: int | None = 0): + """ + Return the mean value of the Array. + + Parameters + ---------- + skipna : bool, default True + Whether to ignore any NaT elements. + axis : int, optional, default 0 + Axis along which to compute the mean. + + Returns + ------- + scalar + Timestamp or Timedelta. + """ return self._data.mean(skipna=skipna, axis=axis) @property @@ -136,8 +146,10 @@ def asi8(self) -> npt.NDArray[np.int64]: return self._data.asi8 @property - @doc(DatetimeLikeArrayMixin.freqstr) def freqstr(self) -> str: + """ + Return the frequency string if it is set, otherwise None. + """ from pandas import PeriodIndex if self._data.freqstr is not None and isinstance( @@ -153,8 +165,15 @@ def freqstr(self) -> str: def _resolution_obj(self) -> Resolution: ... @cache_readonly - @doc(DatetimeLikeArrayMixin.resolution) def resolution(self) -> str: + """ + Return the resolution of the array. + + Returns + ------- + str + The resolution of the array. + """ return self._data.resolution # ------------------------------------------------------------------------ @@ -199,8 +218,10 @@ def equals(self, other: Any) -> bool: return np.array_equal(self.asi8, other.asi8) - @Appender(Index.__contains__.__doc__) def __contains__(self, key: Any) -> bool: + """ + Return True if the key is in the Index. + """ hash(key) try: self.get_loc(key) @@ -243,8 +264,10 @@ def _format_attrs(self): attrs.append(("freq", freq)) return attrs - @Appender(Index._summary.__doc__) def _summary(self, name=None) -> str: + """ + Return a summary of the Index. + """ result = super()._summary(name=name) if self.freq: result += f"\nFreq: {self.freqstr}" @@ -405,8 +428,20 @@ def shift(self, periods: int = 1, freq=None) -> Self: # -------------------------------------------------------------------- - @doc(Index._maybe_cast_listlike_indexer) def _maybe_cast_listlike_indexer(self, keyarr): + """ + Ensure that the list-like indexer is of the correct type. + + Parameters + ---------- + keyarr : list-like + The list-like indexer to check. + + Returns + ------- + list-like + The list-like indexer of the correct type. + """ try: res = self._data._validate_listlike(keyarr, allow_object=True) except (ValueError, TypeError): @@ -497,8 +532,33 @@ def values(self) -> np.ndarray: data.flags.writeable = False return data - @doc(DatetimeIndexOpsMixin.shift) def shift(self, periods: int = 1, freq=None) -> Self: + """ + Shift index by desired number of time frequency increments. + + This method is for shifting the values of datetime-like indexes + by a specified time increment a given number of times. + + Parameters + ---------- + periods : int, default 1 + Number of periods (or increments) to shift by, + can be positive or negative. + freq : pandas.DateOffset, pandas.Timedelta or string, optional + Frequency increment to shift by. + If None, the index is shifted by its own `freq` attribute. + Offset aliases are valid strings, e.g., 'D', 'W', 'M' etc. + + Returns + ------- + pandas.DatetimeIndex + Shifted index. + + See Also + -------- + Index.shift : Shift values of Index. + PeriodIndex.shift : Shift values of PeriodIndex. + """ if freq is not None and freq != self.freq: if isinstance(freq, str): freq = to_offset(freq) @@ -524,8 +584,14 @@ def shift(self, periods: int = 1, freq=None) -> Self: return type(self)._simple_new(result, name=self.name) @cache_readonly - @doc(DatetimeLikeArrayMixin.inferred_freq) def inferred_freq(self) -> str | None: + """ + Tries to return a string representing a frequency guess generated by infer_freq. + + Returns + ------- + str or None + """ return self._data.inferred_freq # -------------------------------------------------------------------- @@ -816,14 +882,41 @@ def _get_insert_freq(self, loc: int, item): freq = self.freq return freq - @doc(NDArrayBackedExtensionIndex.delete) def delete(self, loc) -> Self: + """ + Make new Index with passed location(-s) deleted. + + Parameters + ---------- + loc : int or list of int + Location of item(-s) to delete. Use a list of locations to delete + multiple items. If a slice is provided, the step must be 1. + + Returns + ------- + Index + Will be same type as self, except for RangeIndex. + """ result = super().delete(loc) result._data._freq = self._get_delete_freq(loc) return result - @doc(NDArrayBackedExtensionIndex.insert) def insert(self, loc: int, item): + """ + Make new Index inserting new item at location. + + Follows Python list.insert semantics for negative values. Only at + least one of the index/item pairs must be specified. + + Parameters + ---------- + loc : int + item : object + + Returns + ------- + Index + """ result = super().insert(loc, item) if isinstance(result, type(self)): # i.e. parent class method did not cast @@ -833,7 +926,6 @@ def insert(self, loc: int, item): # -------------------------------------------------------------------- # NDArray-Like Methods - @Appender(_index_shared_docs["take"] % _index_doc_kwargs) def take( self, indices, @@ -842,6 +934,32 @@ def take( fill_value=None, **kwargs, ) -> Self: + """ + Return a new Index of the values selected by the indices. + + For internal compatibility with numpy arrays. + + Parameters + ---------- + indices : array-like + Indices to be taken. + axis : int, optional + The axis over which to select values. Always 0. + allow_fill : bool, default True + fill_value : scalar, default None + If allow_fill=True and fill_value is not None, this value is used + instead of raising if the index is out of bounds. + + Returns + ------- + Index + An element of same type as self. + + See Also + -------- + numpy.ndarray.take : Return an array formed from the elements of a + at the given indices. + """ nv.validate_take((), kwargs) indices = np.asarray(indices, dtype=np.intp) From 8fd7c662ca2cceed002fba24c8eda06a9e14fc46 Mon Sep 17 00:00:00 2001 From: Jixun Sun <160219251+AnonToky@users.noreply.github.com> Date: Wed, 19 Nov 2025 22:36:27 +0800 Subject: [PATCH 06/22] Update --- pandas/core/indexes/datetimelike.py | 54 +++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index f1d324fec697c..105672f371526 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -89,7 +89,7 @@ class DatetimeIndexOpsMixin(NDArrayBackedExtensionIndex, ABC): _data: DatetimeArray | TimedeltaArray | PeriodArray def mean(self, *, skipna: bool = True, axis: int | None = 0): - """ + """ Return the mean value of the Array. Parameters @@ -103,9 +103,25 @@ def mean(self, *, skipna: bool = True, axis: int | None = 0): ------- scalar Timestamp or Timedelta. + + See Also + -------- + DatetimeIndex.median : Return the median of the index. + DatetimeIndex.sum : Sum of the values in the index. + DatetimeIndex.inferred_freq : Attempt to infer frequency from values. + + Examples + -------- + >>> import pandas as pd + >>> idx = pd.date_range("2020-01-01", periods=3) + >>> idx + DatetimeIndex(['2020-01-01', '2020-01-02', '2020-01-03'], + dtype='datetime64[ns]', freq='D') + >>> idx.mean() + Timestamp('2020-01-02 00:00:00') """ return self._data.mean(skipna=skipna, axis=axis) - + @property def freq(self) -> BaseOffset | None: """ @@ -149,6 +165,24 @@ def asi8(self) -> npt.NDArray[np.int64]: def freqstr(self) -> str: """ Return the frequency string if it is set, otherwise None. + + Returns + ------- + str or None + Frequency string such as 'D', 'H', etc., or None. + + See Also + -------- + DatetimeIndex.freq : The offset object for the frequency. + PeriodIndex.freq : Return the frequency object for PeriodIndex. + DatetimeIndex.inferred_freq : Attempt to infer frequency from values. + + Examples + -------- + >>> import pandas as pd + >>> idx = pd.date_range("2020-01-01", periods=3, freq="D") + >>> idx.freqstr + 'D' """ from pandas import PeriodIndex @@ -583,7 +617,7 @@ def shift(self, periods: int = 1, freq=None) -> Self: ) return type(self)._simple_new(result, name=self.name) - @cache_readonly + @cache_readonly def inferred_freq(self) -> str | None: """ Tries to return a string representing a frequency guess generated by infer_freq. @@ -591,6 +625,20 @@ def inferred_freq(self) -> str | None: Returns ------- str or None + Inferred frequency string (e.g. 'D', 'H') or None if it cannot be inferred. + + See Also + -------- + DatetimeIndex.freqstr : Readable frequency string. + DatetimeIndex.freq : The offset object for the frequency. + pandas.infer_freq : Lower-level frequency inference utility. + + Examples + -------- + >>> import pandas as pd + >>> idx = pd.to_datetime(['2020-01-01', '2020-01-02', '2020-01-03']) + >>> idx.inferred_freq + 'D' """ return self._data.inferred_freq From 3d9ad61e3fccf600ff7b521307c6c5763701d6ae Mon Sep 17 00:00:00 2001 From: Jixun Sun <160219251+AnonToky@users.noreply.github.com> Date: Wed, 19 Nov 2025 22:42:15 +0800 Subject: [PATCH 07/22] Refactor mean and inferred_freq methods documentation --- pandas/core/indexes/datetimelike.py | 54 ++--------------------------- 1 file changed, 3 insertions(+), 51 deletions(-) diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index 105672f371526..f1d324fec697c 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -89,7 +89,7 @@ class DatetimeIndexOpsMixin(NDArrayBackedExtensionIndex, ABC): _data: DatetimeArray | TimedeltaArray | PeriodArray def mean(self, *, skipna: bool = True, axis: int | None = 0): - """ + """ Return the mean value of the Array. Parameters @@ -103,25 +103,9 @@ def mean(self, *, skipna: bool = True, axis: int | None = 0): ------- scalar Timestamp or Timedelta. - - See Also - -------- - DatetimeIndex.median : Return the median of the index. - DatetimeIndex.sum : Sum of the values in the index. - DatetimeIndex.inferred_freq : Attempt to infer frequency from values. - - Examples - -------- - >>> import pandas as pd - >>> idx = pd.date_range("2020-01-01", periods=3) - >>> idx - DatetimeIndex(['2020-01-01', '2020-01-02', '2020-01-03'], - dtype='datetime64[ns]', freq='D') - >>> idx.mean() - Timestamp('2020-01-02 00:00:00') """ return self._data.mean(skipna=skipna, axis=axis) - + @property def freq(self) -> BaseOffset | None: """ @@ -165,24 +149,6 @@ def asi8(self) -> npt.NDArray[np.int64]: def freqstr(self) -> str: """ Return the frequency string if it is set, otherwise None. - - Returns - ------- - str or None - Frequency string such as 'D', 'H', etc., or None. - - See Also - -------- - DatetimeIndex.freq : The offset object for the frequency. - PeriodIndex.freq : Return the frequency object for PeriodIndex. - DatetimeIndex.inferred_freq : Attempt to infer frequency from values. - - Examples - -------- - >>> import pandas as pd - >>> idx = pd.date_range("2020-01-01", periods=3, freq="D") - >>> idx.freqstr - 'D' """ from pandas import PeriodIndex @@ -617,7 +583,7 @@ def shift(self, periods: int = 1, freq=None) -> Self: ) return type(self)._simple_new(result, name=self.name) - @cache_readonly + @cache_readonly def inferred_freq(self) -> str | None: """ Tries to return a string representing a frequency guess generated by infer_freq. @@ -625,20 +591,6 @@ def inferred_freq(self) -> str | None: Returns ------- str or None - Inferred frequency string (e.g. 'D', 'H') or None if it cannot be inferred. - - See Also - -------- - DatetimeIndex.freqstr : Readable frequency string. - DatetimeIndex.freq : The offset object for the frequency. - pandas.infer_freq : Lower-level frequency inference utility. - - Examples - -------- - >>> import pandas as pd - >>> idx = pd.to_datetime(['2020-01-01', '2020-01-02', '2020-01-03']) - >>> idx.inferred_freq - 'D' """ return self._data.inferred_freq From 1ec3cdc09079f78e85d51404aa760c61bae114c5 Mon Sep 17 00:00:00 2001 From: Jixun Sun <160219251+AnonToky@users.noreply.github.com> Date: Wed, 19 Nov 2025 23:22:49 +0800 Subject: [PATCH 08/22] Update datetimelike.py --- pandas/core/indexes/datetimelike.py | 42 +++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index f1d324fec697c..e001f3a0ba596 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -36,7 +36,9 @@ NullFrequencyError, ) from pandas.util._decorators import ( + Appender, cache_readonly, + doc, ) from pandas.core.dtypes.common import ( @@ -55,10 +57,12 @@ PeriodArray, TimedeltaArray, ) +from pandas.core.arrays.datetimelike import DatetimeLikeArrayMixin import pandas.core.common as com import pandas.core.indexes.base as ibase from pandas.core.indexes.base import ( Index, + _index_shared_docs, ) from pandas.core.indexes.extension import NDArrayBackedExtensionIndex from pandas.core.indexes.range import RangeIndex @@ -103,6 +107,21 @@ def mean(self, *, skipna: bool = True, axis: int | None = 0): ------- scalar Timestamp or Timedelta. + + See Also + -------- + numpy.ndarray.mean : Returns the average of the array elements. + Series.mean : Return the mean value in a Series. + + Examples + -------- + >>> idx = pd.date_range('2023-01-01', periods=3) + >>> idx.mean() + Timestamp('2023-01-02 00:00:00') + + >>> idx = pd.to_timedelta([1, 2, 3], unit='D') + >>> idx.mean() + Timedelta('2 days 00:00:00') """ return self._data.mean(skipna=skipna, axis=axis) @@ -149,6 +168,18 @@ def asi8(self) -> npt.NDArray[np.int64]: def freqstr(self) -> str: """ Return the frequency string if it is set, otherwise None. + + See Also + -------- + DatetimeIndex.freq : Return the frequency object if it is set, otherwise None. + DatetimeIndex.inferred_freq : Tries to return a string representing a frequency + guess generated by infer_freq. + + Examples + -------- + >>> idx = pd.date_range('2023-01-01', periods=3, freq='D') + >>> idx.freqstr + 'D' """ from pandas import PeriodIndex @@ -591,6 +622,17 @@ def inferred_freq(self) -> str | None: Returns ------- str or None + + See Also + -------- + DatetimeIndex.freq : Return the frequency object if it is set, otherwise None. + DatetimeIndex.freqstr : Return the frequency string if it is set, otherwise None. + + Examples + -------- + >>> idx = pd.DatetimeIndex(['2018-01-01', '2018-01-03', '2018-01-05']) + >>> idx.inferred_freq + '2D' """ return self._data.inferred_freq From e1b76d9a5de3ae7ae232583c8de8823e98856064 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 19 Nov 2025 15:28:20 +0000 Subject: [PATCH 09/22] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pandas/core/indexes/datetimelike.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index e001f3a0ba596..2f7d24d4c053b 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -36,9 +36,7 @@ NullFrequencyError, ) from pandas.util._decorators import ( - Appender, cache_readonly, - doc, ) from pandas.core.dtypes.common import ( @@ -57,12 +55,10 @@ PeriodArray, TimedeltaArray, ) -from pandas.core.arrays.datetimelike import DatetimeLikeArrayMixin import pandas.core.common as com import pandas.core.indexes.base as ibase from pandas.core.indexes.base import ( Index, - _index_shared_docs, ) from pandas.core.indexes.extension import NDArrayBackedExtensionIndex from pandas.core.indexes.range import RangeIndex @@ -115,11 +111,11 @@ def mean(self, *, skipna: bool = True, axis: int | None = 0): Examples -------- - >>> idx = pd.date_range('2023-01-01', periods=3) + >>> idx = pd.date_range("2023-01-01", periods=3) >>> idx.mean() Timestamp('2023-01-02 00:00:00') - >>> idx = pd.to_timedelta([1, 2, 3], unit='D') + >>> idx = pd.to_timedelta([1, 2, 3], unit="D") >>> idx.mean() Timedelta('2 days 00:00:00') """ @@ -177,7 +173,7 @@ def freqstr(self) -> str: Examples -------- - >>> idx = pd.date_range('2023-01-01', periods=3, freq='D') + >>> idx = pd.date_range("2023-01-01", periods=3, freq="D") >>> idx.freqstr 'D' """ @@ -630,7 +626,7 @@ def inferred_freq(self) -> str | None: Examples -------- - >>> idx = pd.DatetimeIndex(['2018-01-01', '2018-01-03', '2018-01-05']) + >>> idx = pd.DatetimeIndex(["2018-01-01", "2018-01-03", "2018-01-05"]) >>> idx.inferred_freq '2D' """ From 08446fa48737740b580c03cd58c89ab94ffdff23 Mon Sep 17 00:00:00 2001 From: Jixun Sun <160219251+AnonToky@users.noreply.github.com> Date: Wed, 19 Nov 2025 23:31:09 +0800 Subject: [PATCH 10/22] to fix line too long --- pandas/core/indexes/datetimelike.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index 2f7d24d4c053b..02a55150dcb85 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -622,7 +622,8 @@ def inferred_freq(self) -> str | None: See Also -------- DatetimeIndex.freq : Return the frequency object if it is set, otherwise None. - DatetimeIndex.freqstr : Return the frequency string if it is set, otherwise None. + DatetimeIndex.freqstr : + Return the frequency string if it is set, otherwise None. Examples -------- From 4d1e0df7beadd3c70a874389738b87786a924cf8 Mon Sep 17 00:00:00 2001 From: Jixun Sun <160219251+AnonToky@users.noreply.github.com> Date: Wed, 19 Nov 2025 23:35:13 +0800 Subject: [PATCH 11/22] Update datetimelike.py --- pandas/core/indexes/datetimelike.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index 02a55150dcb85..7fdf955442dfe 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -622,8 +622,8 @@ def inferred_freq(self) -> str | None: See Also -------- DatetimeIndex.freq : Return the frequency object if it is set, otherwise None. - DatetimeIndex.freqstr : - Return the frequency string if it is set, otherwise None. + DatetimeIndex.freqstr : Return the frequency string + if it is set, otherwise None. Examples -------- From 3dfbdcccb292f0146e6dec8870d5581739acb204 Mon Sep 17 00:00:00 2001 From: Jixun Sun <160219251+AnonToky@users.noreply.github.com> Date: Wed, 19 Nov 2025 23:40:45 +0800 Subject: [PATCH 12/22] Update datetimelike.py --- pandas/core/indexes/datetimelike.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index 7fdf955442dfe..11e71a80b9dee 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -622,8 +622,7 @@ def inferred_freq(self) -> str | None: See Also -------- DatetimeIndex.freq : Return the frequency object if it is set, otherwise None. - DatetimeIndex.freqstr : Return the frequency string - if it is set, otherwise None. + DatetimeIndex.freqstr : Return the frequency string if it is set. Examples -------- From 167ae367d9bb0336f6c15667a394cc837d5e3315 Mon Sep 17 00:00:00 2001 From: Jixun Sun <160219251+AnonToky@users.noreply.github.com> Date: Tue, 25 Nov 2025 19:09:30 +0800 Subject: [PATCH 13/22] revoke the changes in @doc Added doc decorators to methods for better documentation integration. --- pandas/core/indexes/datetimelike.py | 151 +++------------------------- 1 file changed, 12 insertions(+), 139 deletions(-) diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index 11e71a80b9dee..bc9c208d5e6e4 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -36,7 +36,9 @@ NullFrequencyError, ) from pandas.util._decorators import ( + Appender, cache_readonly, + doc, ) from pandas.core.dtypes.common import ( @@ -55,10 +57,12 @@ PeriodArray, TimedeltaArray, ) +from pandas.core.arrays.datetimelike import DatetimeLikeArrayMixin import pandas.core.common as com import pandas.core.indexes.base as ibase from pandas.core.indexes.base import ( Index, + _index_shared_docs, ) from pandas.core.indexes.extension import NDArrayBackedExtensionIndex from pandas.core.indexes.range import RangeIndex @@ -88,37 +92,8 @@ class DatetimeIndexOpsMixin(NDArrayBackedExtensionIndex, ABC): _can_hold_strings = False _data: DatetimeArray | TimedeltaArray | PeriodArray + @doc(DatetimeLikeArrayMixin.mean) def mean(self, *, skipna: bool = True, axis: int | None = 0): - """ - Return the mean value of the Array. - - Parameters - ---------- - skipna : bool, default True - Whether to ignore any NaT elements. - axis : int, optional, default 0 - Axis along which to compute the mean. - - Returns - ------- - scalar - Timestamp or Timedelta. - - See Also - -------- - numpy.ndarray.mean : Returns the average of the array elements. - Series.mean : Return the mean value in a Series. - - Examples - -------- - >>> idx = pd.date_range("2023-01-01", periods=3) - >>> idx.mean() - Timestamp('2023-01-02 00:00:00') - - >>> idx = pd.to_timedelta([1, 2, 3], unit="D") - >>> idx.mean() - Timedelta('2 days 00:00:00') - """ return self._data.mean(skipna=skipna, axis=axis) @property @@ -161,22 +136,8 @@ def asi8(self) -> npt.NDArray[np.int64]: return self._data.asi8 @property + @doc(DatetimeLikeArrayMixin.freqstr) def freqstr(self) -> str: - """ - Return the frequency string if it is set, otherwise None. - - See Also - -------- - DatetimeIndex.freq : Return the frequency object if it is set, otherwise None. - DatetimeIndex.inferred_freq : Tries to return a string representing a frequency - guess generated by infer_freq. - - Examples - -------- - >>> idx = pd.date_range("2023-01-01", periods=3, freq="D") - >>> idx.freqstr - 'D' - """ from pandas import PeriodIndex if self._data.freqstr is not None and isinstance( @@ -192,15 +153,8 @@ def freqstr(self) -> str: def _resolution_obj(self) -> Resolution: ... @cache_readonly + @doc(DatetimeLikeArrayMixin.resolution) def resolution(self) -> str: - """ - Return the resolution of the array. - - Returns - ------- - str - The resolution of the array. - """ return self._data.resolution # ------------------------------------------------------------------------ @@ -455,20 +409,8 @@ def shift(self, periods: int = 1, freq=None) -> Self: # -------------------------------------------------------------------- + @doc(Index._maybe_cast_listlike_indexer) def _maybe_cast_listlike_indexer(self, keyarr): - """ - Ensure that the list-like indexer is of the correct type. - - Parameters - ---------- - keyarr : list-like - The list-like indexer to check. - - Returns - ------- - list-like - The list-like indexer of the correct type. - """ try: res = self._data._validate_listlike(keyarr, allow_object=True) except (ValueError, TypeError): @@ -559,33 +501,8 @@ def values(self) -> np.ndarray: data.flags.writeable = False return data + @doc(DatetimeIndexOpsMixin.shift) def shift(self, periods: int = 1, freq=None) -> Self: - """ - Shift index by desired number of time frequency increments. - - This method is for shifting the values of datetime-like indexes - by a specified time increment a given number of times. - - Parameters - ---------- - periods : int, default 1 - Number of periods (or increments) to shift by, - can be positive or negative. - freq : pandas.DateOffset, pandas.Timedelta or string, optional - Frequency increment to shift by. - If None, the index is shifted by its own `freq` attribute. - Offset aliases are valid strings, e.g., 'D', 'W', 'M' etc. - - Returns - ------- - pandas.DatetimeIndex - Shifted index. - - See Also - -------- - Index.shift : Shift values of Index. - PeriodIndex.shift : Shift values of PeriodIndex. - """ if freq is not None and freq != self.freq: if isinstance(freq, str): freq = to_offset(freq) @@ -611,25 +528,8 @@ def shift(self, periods: int = 1, freq=None) -> Self: return type(self)._simple_new(result, name=self.name) @cache_readonly + @doc(DatetimeLikeArrayMixin.inferred_freq) def inferred_freq(self) -> str | None: - """ - Tries to return a string representing a frequency guess generated by infer_freq. - - Returns - ------- - str or None - - See Also - -------- - DatetimeIndex.freq : Return the frequency object if it is set, otherwise None. - DatetimeIndex.freqstr : Return the frequency string if it is set. - - Examples - -------- - >>> idx = pd.DatetimeIndex(["2018-01-01", "2018-01-03", "2018-01-05"]) - >>> idx.inferred_freq - '2D' - """ return self._data.inferred_freq # -------------------------------------------------------------------- @@ -920,41 +820,14 @@ def _get_insert_freq(self, loc: int, item): freq = self.freq return freq + @doc(NDArrayBackedExtensionIndex.delete) def delete(self, loc) -> Self: - """ - Make new Index with passed location(-s) deleted. - - Parameters - ---------- - loc : int or list of int - Location of item(-s) to delete. Use a list of locations to delete - multiple items. If a slice is provided, the step must be 1. - - Returns - ------- - Index - Will be same type as self, except for RangeIndex. - """ result = super().delete(loc) result._data._freq = self._get_delete_freq(loc) return result + @doc(NDArrayBackedExtensionIndex.insert) def insert(self, loc: int, item): - """ - Make new Index inserting new item at location. - - Follows Python list.insert semantics for negative values. Only at - least one of the index/item pairs must be specified. - - Parameters - ---------- - loc : int - item : object - - Returns - ------- - Index - """ result = super().insert(loc, item) if isinstance(result, type(self)): # i.e. parent class method did not cast From f2ce2186844aba4a2d59a35d3228adf40ec5799f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 25 Nov 2025 11:15:08 +0000 Subject: [PATCH 14/22] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pandas/core/indexes/datetimelike.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index bc9c208d5e6e4..555c5c705cf34 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -36,7 +36,6 @@ NullFrequencyError, ) from pandas.util._decorators import ( - Appender, cache_readonly, doc, ) @@ -62,7 +61,6 @@ import pandas.core.indexes.base as ibase from pandas.core.indexes.base import ( Index, - _index_shared_docs, ) from pandas.core.indexes.extension import NDArrayBackedExtensionIndex from pandas.core.indexes.range import RangeIndex From ca2abb30aa879d30b29a2d8698d7b220a1bd8e05 Mon Sep 17 00:00:00 2001 From: Jixun Sun <160219251+AnonToky@users.noreply.github.com> Date: Wed, 26 Nov 2025 13:23:05 +0800 Subject: [PATCH 15/22] Update datetimelike.py --- pandas/core/indexes/datetimelike.py | 39 +++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index 555c5c705cf34..fe0e0e6106319 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -844,30 +844,47 @@ def take( **kwargs, ) -> Self: """ - Return a new Index of the values selected by the indices. - For internal compatibility with numpy arrays. Parameters ---------- indices : array-like Indices to be taken. - axis : int, optional - The axis over which to select values. Always 0. + axis : {0 or 'index'}, optional + The axis over which to select values, always 0 or 'index'. allow_fill : bool, default True + How to handle negative values in `indices`. + + * False: negative values in `indices` indicate positional indices + from the right (the default). This is similar to + :func:`numpy.take`. + + * True: negative values in `indices` indicate + missing values. These values are set to `fill_value`. Any other + other negative values raise a ``ValueError``. + fill_value : scalar, default None - If allow_fill=True and fill_value is not None, this value is used - instead of raising if the index is out of bounds. - + If allow_fill=True and fill_value is not None, indices specified by + -1 are regarded as NA. If Index doesn't hold NA, raise ValueError. + **kwargs + Required for compatibility with numpy. + Returns ------- Index - An element of same type as self. - + An index formed of elements at the given indices. Will be the same + type as self, except for RangeIndex. + See Also -------- - numpy.ndarray.take : Return an array formed from the elements of a - at the given indices. + numpy.ndarray.take: Return an array formed from the + elements of a at the given indices. + + Examples + -------- + >>> idx = pd.Index(['a', 'b', 'c']) + >>> idx.take([2, 2, 1, 2]) + Index(['c', 'c', 'b', 'c'], dtype='str') """ nv.validate_take((), kwargs) indices = np.asarray(indices, dtype=np.intp) From ae48396b9f01d4fc57408b9c8746c0d875c65e8c Mon Sep 17 00:00:00 2001 From: Jixun Sun <160219251+AnonToky@users.noreply.github.com> Date: Wed, 26 Nov 2025 13:24:40 +0800 Subject: [PATCH 16/22] Update datetimelike.py --- pandas/core/indexes/datetimelike.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index fe0e0e6106319..da2ca8a78e32d 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -844,6 +844,8 @@ def take( **kwargs, ) -> Self: """ + Return a new %(klass)s of the values selected by the indices. + For internal compatibility with numpy arrays. Parameters From 33a4d34beb075338a8d1747f3dbe9fc284d3f915 Mon Sep 17 00:00:00 2001 From: Jixun Sun <160219251+AnonToky@users.noreply.github.com> Date: Wed, 26 Nov 2025 13:30:15 +0800 Subject: [PATCH 17/22] Update datetimelike.py --- pandas/core/indexes/datetimelike.py | 44 +++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index da2ca8a78e32d..219d7f18e5e6b 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -199,7 +199,38 @@ def equals(self, other: Any) -> bool: def __contains__(self, key: Any) -> bool: """ - Return True if the key is in the Index. + Return a boolean indicating whether the provided key is in the index. + + Parameters + ---------- + key : label + The key to check if it is present in the index. + + Returns + ------- + bool + Whether the key search is in the index. + + Raises + ------ + TypeError + If the key is not hashable. + + See Also + -------- + Index.isin : Returns an ndarray of boolean dtype indicating whether the + list-like key is in the index. + + Examples + -------- + >>> idx = pd.Index([1, 2, 3, 4]) + >>> idx + Index([1, 2, 3, 4], dtype='int64') + + >>> 2 in idx + True + >>> 6 in idx + False """ hash(key) try: @@ -245,7 +276,16 @@ def _format_attrs(self): def _summary(self, name=None) -> str: """ - Return a summary of the Index. + Return a summarized representation. + + Parameters + ---------- + name : str + name to use in the summary representation + + Returns + ------- + String with a summarized representation of the index """ result = super()._summary(name=name) if self.freq: From ac5cc297dbdaaf827d0efad24afe5de577eebb24 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 26 Nov 2025 05:35:19 +0000 Subject: [PATCH 18/22] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pandas/core/indexes/datetimelike.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index 219d7f18e5e6b..db7fb9ac66c2d 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -885,7 +885,7 @@ def take( ) -> Self: """ Return a new %(klass)s of the values selected by the indices. - + For internal compatibility with numpy arrays. Parameters @@ -896,35 +896,35 @@ def take( The axis over which to select values, always 0 or 'index'. allow_fill : bool, default True How to handle negative values in `indices`. - + * False: negative values in `indices` indicate positional indices from the right (the default). This is similar to :func:`numpy.take`. - + * True: negative values in `indices` indicate missing values. These values are set to `fill_value`. Any other other negative values raise a ``ValueError``. - + fill_value : scalar, default None If allow_fill=True and fill_value is not None, indices specified by -1 are regarded as NA. If Index doesn't hold NA, raise ValueError. **kwargs Required for compatibility with numpy. - + Returns ------- Index An index formed of elements at the given indices. Will be the same type as self, except for RangeIndex. - + See Also -------- numpy.ndarray.take: Return an array formed from the elements of a at the given indices. - + Examples -------- - >>> idx = pd.Index(['a', 'b', 'c']) + >>> idx = pd.Index(["a", "b", "c"]) >>> idx.take([2, 2, 1, 2]) Index(['c', 'c', 'b', 'c'], dtype='str') """ From e916e2281f2d105af8063fe107793a1b9eb1067a Mon Sep 17 00:00:00 2001 From: Jixun Sun <160219251+AnonToky@users.noreply.github.com> Date: Sat, 29 Nov 2025 19:54:32 +0800 Subject: [PATCH 19/22] Update datetimelike.py --- pandas/core/indexes/datetimelike.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index db7fb9ac66c2d..4dc8d819ddca4 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -884,7 +884,7 @@ def take( **kwargs, ) -> Self: """ - Return a new %(klass)s of the values selected by the indices. + Return a new Index of the same type as self containing the values selected by the indices. For internal compatibility with numpy arrays. From d0f6eb0e6d23e714a18a1826ef6fb5f53ca7f40a Mon Sep 17 00:00:00 2001 From: Jixun Sun <160219251+AnonToky@users.noreply.github.com> Date: Sat, 29 Nov 2025 20:00:05 +0800 Subject: [PATCH 20/22] Update datetimelike.py --- pandas/core/indexes/datetimelike.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index b1bc732b4ad3a..129970fcdd993 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -884,7 +884,8 @@ def take( **kwargs, ) -> Self: """ - Return a new Index of the same type as self containing the values selected by the indices. + Return a new Index of the same type as self containing the values + selected by the indices. For internal compatibility with numpy arrays. From b2eb8f16bbb0a4fb73876f60b8a74638301d54e1 Mon Sep 17 00:00:00 2001 From: Jixun Sun <160219251+AnonToky@users.noreply.github.com> Date: Sun, 30 Nov 2025 22:24:28 +0800 Subject: [PATCH 21/22] Update datetimelike.py --- pandas/core/indexes/datetimelike.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index 129970fcdd993..68e25f44df94c 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -884,8 +884,7 @@ def take( **kwargs, ) -> Self: """ - Return a new Index of the same type as self containing the values - selected by the indices. + Return a new Index of the same type as self. For internal compatibility with numpy arrays. From a23ed976d89a1234579f02660d2be0e1d47c7016 Mon Sep 17 00:00:00 2001 From: Jixun Sun <160219251+AnonToky@users.noreply.github.com> Date: Sun, 30 Nov 2025 23:27:34 +0800 Subject: [PATCH 22/22] Update datetimelike.py --- pandas/core/indexes/datetimelike.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index 68e25f44df94c..bee019e31fd69 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -884,7 +884,7 @@ def take( **kwargs, ) -> Self: """ - Return a new Index of the same type as self. + Return a new Index of the values selected by the indices. For internal compatibility with numpy arrays.