From 3ce2133016daa11c662c2fe55550bfdd243d1f04 Mon Sep 17 00:00:00 2001 From: cloudboat <15851404+cloudboat111@user.noreply.gitee.com> Date: Tue, 11 Nov 2025 22:25:18 +0800 Subject: [PATCH 1/9] REF: Replace @Substitution with hardcoded docstring in strftime --- pandas/core/arrays/datetimelike.py | 288 +++++++++++++++++++++++++++++ 1 file changed, 288 insertions(+) diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index 1fbcd0665c467..7ff1130b6c95a 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -2265,6 +2265,102 @@ def round( ambiguous: TimeAmbiguous = "raise", nonexistent: TimeNonexistent = "raise", ) -> Self: + """ + Perform round operation on the data to the specified `freq`. + + Parameters + ---------- + freq : str or Offset + The frequency level to round the index to. Must be a fixed + frequency like 's' (second) not 'ME' (month end). See + :ref:`frequency aliases ` for + a list of possible `freq` values. + ambiguous : 'infer', bool-ndarray, 'NaT', default 'raise' + Only relevant for DatetimeIndex: + + - 'infer' will attempt to infer fall dst-transition hours based on + order + - bool-ndarray where True signifies a DST time, False designates + a non-DST time (note that this flag is only applicable for + ambiguous times) + - 'NaT' will return NaT where there are ambiguous times + - 'raise' will raise a ValueError if there are ambiguous + times. + + nonexistent : 'shift_forward', 'shift_backward', 'NaT', timedelta, \ + default 'raise' + A nonexistent time does not exist in a particular timezone + where clocks moved forward due to DST. + + - 'shift_forward' will shift the nonexistent time forward to the + closest existing time + - 'shift_backward' will shift the nonexistent time backward to the + closest existing time + - 'NaT' will return NaT where there are nonexistent times + - timedelta objects will shift nonexistent times by the timedelta + - 'raise' will raise a ValueError if there are + nonexistent times. + + Returns + ------- + DatetimeIndex, TimedeltaIndex, or Series + Index of the same type for a DatetimeIndex or TimedeltaIndex, + or a Series with the same index for a Series. + + Raises + ------ + ValueError if the `freq` cannot be converted. + + See Also + -------- + DatetimeIndex.floor : + Perform floor operation on the data to the specified `freq`. + DatetimeIndex.snap : + Snap time stamps to nearest occurring frequency. + + Notes + ----- + If the timestamps have a timezone, rounding will take place relative to the + local ("wall") time and re-localized to the same timezone. When rounding + near daylight savings time, use ``nonexistent`` and ``ambiguous`` to + control the re-localization behavior. + + Examples + -------- + **DatetimeIndex** + + >>> rng = pd.date_range("1/1/2018 11:59:00", periods=3, freq="min") + >>> rng + DatetimeIndex(['2018-01-01 11:59:00', '2018-01-01 12:00:00', + '2018-01-01 12:01:00'], + dtype='datetime64[ns]', freq='min') + + >>> rng.round("h") + DatetimeIndex(['2018-01-01 12:00:00', '2018-01-01 12:00:00', + '2018-01-01 12:00:00'], + dtype='datetime64[ns]', freq=None) + + **Series** + + >>> pd.Series(rng).dt.round("h") + 0 2018-01-01 12:00:00 + 1 2018-01-01 12:00:00 + 2 2018-01-01 12:00:00 + dtype: datetime64[ns] + + When rounding near a daylight savings time transition, use ``ambiguous`` or + ``nonexistent`` to control how the timestamp should be re-localized. + + >>> rng_tz = pd.DatetimeIndex(["2021-10-31 03:30:00"], tz="Europe/Amsterdam") + + >>> rng_tz.floor("2h", ambiguous=False) + DatetimeIndex(['2021-10-31 02:00:00+01:00'], + dtype='datetime64[s, Europe/Amsterdam]', freq=None) + + >>> rng_tz.floor("2h", ambiguous=True) + DatetimeIndex(['2021-10-31 02:00:00+02:00'], + dtype='datetime64[s, Europe/Amsterdam]', freq=None) + """ return self._round(freq, RoundTo.NEAREST_HALF_EVEN, ambiguous, nonexistent) @Appender((_round_doc + _floor_example).format(op="floor")) @@ -2274,6 +2370,102 @@ def floor( ambiguous: TimeAmbiguous = "raise", nonexistent: TimeNonexistent = "raise", ) -> Self: + """ + Perform floor operation on the data to the specified `freq`. + + Parameters + ---------- + freq : str or Offset + The frequency level to floor the index to. Must be a fixed + frequency like 's' (second) not 'ME' (month end). See + :ref:`frequency aliases ` for + a list of possible `freq` values. + ambiguous : 'infer', bool-ndarray, 'NaT', default 'raise' + Only relevant for DatetimeIndex: + + - 'infer' will attempt to infer fall dst-transition hours based on + order + - bool-ndarray where True signifies a DST time, False designates + a non-DST time (note that this flag is only applicable for + ambiguous times) + - 'NaT' will return NaT where there are ambiguous times + - 'raise' will raise a ValueError if there are ambiguous + times. + + nonexistent : 'shift_forward', 'shift_backward', 'NaT', timedelta, \ + default 'raise' + A nonexistent time does not exist in a particular timezone + where clocks moved forward due to DST. + + - 'shift_forward' will shift the nonexistent time forward to the + closest existing time + - 'shift_backward' will shift the nonexistent time backward to the + closest existing time + - 'NaT' will return NaT where there are nonexistent times + - timedelta objects will shift nonexistent times by the timedelta + - 'raise' will raise a ValueError if there are + nonexistent times. + + Returns + ------- + DatetimeIndex, TimedeltaIndex, or Series + Index of the same type for a DatetimeIndex or TimedeltaIndex, + or a Series with the same index for a Series. + + Raises + ------ + ValueError if the `freq` cannot be converted. + + See Also + -------- + DatetimeIndex.floor : + Perform floor operation on the data to the specified `freq`. + DatetimeIndex.snap : + Snap time stamps to nearest occurring frequency. + + Notes + ----- + If the timestamps have a timezone, flooring will take place relative to the + local ("wall") time and re-localized to the same timezone. When flooring + near daylight savings time, use ``nonexistent`` and ``ambiguous`` to + control the re-localization behavior. + + Examples + -------- + **DatetimeIndex** + + >>> rng = pd.date_range("1/1/2018 11:59:00", periods=3, freq="min") + >>> rng + DatetimeIndex(['2018-01-01 11:59:00', '2018-01-01 12:00:00', + '2018-01-01 12:01:00'], + dtype='datetime64[ns]', freq='min') + + >>> rng.floor("h") + DatetimeIndex(['2018-01-01 11:00:00', '2018-01-01 12:00:00', + '2018-01-01 12:00:00'], + dtype='datetime64[ns]', freq=None) + + **Series** + + >>> pd.Series(rng).dt.floor("h") + 0 2018-01-01 11:00:00 + 1 2018-01-01 12:00:00 + 2 2018-01-01 12:00:00 + dtype: datetime64[ns] + + When rounding near a daylight savings time transition, use ``ambiguous`` or + ``nonexistent`` to control how the timestamp should be re-localized. + + >>> rng_tz = pd.DatetimeIndex(["2021-10-31 03:30:00"], tz="Europe/Amsterdam") + + >>> rng_tz.floor("2h", ambiguous=False) + DatetimeIndex(['2021-10-31 02:00:00+01:00'], + dtype='datetime64[s, Europe/Amsterdam]', freq=None) + + >>> rng_tz.floor("2h", ambiguous=True) + DatetimeIndex(['2021-10-31 02:00:00+02:00'], + dtype='datetime64[s, Europe/Amsterdam]', freq=None) + """ return self._round(freq, RoundTo.MINUS_INFTY, ambiguous, nonexistent) @Appender((_round_doc + _ceil_example).format(op="ceil")) @@ -2283,6 +2475,102 @@ def ceil( ambiguous: TimeAmbiguous = "raise", nonexistent: TimeNonexistent = "raise", ) -> Self: + """ + Perform ceil operation on the data to the specified `freq`. + + Parameters + ---------- + freq : str or Offset + The frequency level to ceil the index to. Must be a fixed + frequency like 's' (second) not 'ME' (month end). See + :ref:`frequency aliases ` for + a list of possible `freq` values. + ambiguous : 'infer', bool-ndarray, 'NaT', default 'raise' + Only relevant for DatetimeIndex: + + - 'infer' will attempt to infer fall dst-transition hours based on + order + - bool-ndarray where True signifies a DST time, False designates + a non-DST time (note that this flag is only applicable for + ambiguous times) + - 'NaT' will return NaT where there are ambiguous times + - 'raise' will raise a ValueError if there are ambiguous + times. + + nonexistent : 'shift_forward', 'shift_backward', 'NaT', timedelta, \ + default 'raise' + A nonexistent time does not exist in a particular timezone + where clocks moved forward due to DST. + + - 'shift_forward' will shift the nonexistent time forward to the + closest existing time + - 'shift_backward' will shift the nonexistent time backward to the + closest existing time + - 'NaT' will return NaT where there are nonexistent times + - timedelta objects will shift nonexistent times by the timedelta + - 'raise' will raise a ValueError if there are + nonexistent times. + + Returns + ------- + DatetimeIndex, TimedeltaIndex, or Series + Index of the same type for a DatetimeIndex or TimedeltaIndex, + or a Series with the same index for a Series. + + Raises + ------ + ValueError if the `freq` cannot be converted. + + See Also + -------- + DatetimeIndex.floor : + Perform floor operation on the data to the specified `freq`. + DatetimeIndex.snap : + Snap time stamps to nearest occurring frequency. + + Notes + ----- + If the timestamps have a timezone, ceiling will take place relative to the + local ("wall") time and re-localized to the same timezone. When ceiling + near daylight savings time, use ``nonexistent`` and ``ambiguous`` to + control the re-localization behavior. + + Examples + -------- + **DatetimeIndex** + + >>> rng = pd.date_range("1/1/2018 11:59:00", periods=3, freq="min") + >>> rng + DatetimeIndex(['2018-01-01 11:59:00', '2018-01-01 12:00:00', + '2018-01-01 12:01:00'], + dtype='datetime64[ns]', freq='min') + + >>> rng.ceil("h") + DatetimeIndex(['2018-01-01 12:00:00', '2018-01-01 12:00:00', + '2018-01-01 13:00:00'], + dtype='datetime64[ns]', freq=None) + + **Series** + + >>> pd.Series(rng).dt.ceil("h") + 0 2018-01-01 12:00:00 + 1 2018-01-01 12:00:00 + 2 2018-01-01 13:00:00 + dtype: datetime64[ns] + + When rounding near a daylight savings time transition, use ``ambiguous`` or + ``nonexistent`` to control how the timestamp should be re-localized. + + >>> rng_tz = pd.DatetimeIndex(["2021-10-31 01:30:00"], tz="Europe/Amsterdam") + + >>> rng_tz.ceil("h", ambiguous=False) + DatetimeIndex(['2021-10-31 02:00:00+01:00'], + dtype='datetime64[s, Europe/Amsterdam]', freq=None) + + >>> rng_tz.ceil("h", ambiguous=True) + DatetimeIndex(['2021-10-31 02:00:00+02:00'], + dtype='datetime64[s, Europe/Amsterdam]', freq=None) + """ return self._round(freq, RoundTo.PLUS_INFTY, ambiguous, nonexistent) # -------------------------------------------------------------- From 7eb63bda45be76b1980f14d1e9bc17a6f2a42288 Mon Sep 17 00:00:00 2001 From: cloudboat <15851404+cloudboat111@user.noreply.gitee.com> Date: Tue, 11 Nov 2025 22:32:27 +0800 Subject: [PATCH 2/9] REF: Replace @Substitution with hardcoded docstring in strftime --- pandas/core/arrays/datetimelike.py | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index 7ff1130b6c95a..3df12c106dfb7 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -81,8 +81,6 @@ PerformanceWarning, ) from pandas.util._decorators import ( - Appender, - Substitution, cache_readonly, ) from pandas.util._exceptions import find_stack_level @@ -1774,10 +1772,6 @@ class DatelikeOps(DatetimeLikeArrayMixin): Common ops for DatetimeIndex/PeriodIndex, but not TimedeltaIndex. """ - @Substitution( - URL="https://docs.python.org/3/library/datetime.html" - "#strftime-and-strptime-behavior" - ) def strftime(self, date_format: str) -> npt.NDArray[np.object_]: """ Convert to Index using specified date_format. @@ -1785,7 +1779,7 @@ def strftime(self, date_format: str) -> npt.NDArray[np.object_]: Return an Index of formatted strings specified by date_format, which supports the same string format as the python standard library. Details of the string format can be found in `python string format - doc <%(URL)s>`__. + doc `__. Formats supported by the C `strftime` API but not by the python string format doc (such as `"%%R"`, `"%%r"`) are not officially supported and should be @@ -2258,7 +2252,6 @@ def _round(self, freq, mode, ambiguous, nonexistent): result = result.view(self._ndarray.dtype) return self._simple_new(result, dtype=self.dtype) - @Appender((_round_doc + _round_example).format(op="round")) def round( self, freq, @@ -2363,7 +2356,6 @@ def round( """ return self._round(freq, RoundTo.NEAREST_HALF_EVEN, ambiguous, nonexistent) - @Appender((_round_doc + _floor_example).format(op="floor")) def floor( self, freq, @@ -2468,7 +2460,6 @@ def floor( """ return self._round(freq, RoundTo.MINUS_INFTY, ambiguous, nonexistent) - @Appender((_round_doc + _ceil_example).format(op="ceil")) def ceil( self, freq, From 372e8fea1ed7a76ee2ded6a3b97f50b719c347d2 Mon Sep 17 00:00:00 2001 From: cloudboat <15851404+cloudboat111@user.noreply.gitee.com> Date: Tue, 11 Nov 2025 23:09:41 +0800 Subject: [PATCH 3/9] chang int the correct url --- pandas/core/arrays/datetimelike.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index 3df12c106dfb7..4c1897082af5f 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -1779,7 +1779,8 @@ def strftime(self, date_format: str) -> npt.NDArray[np.object_]: Return an Index of formatted strings specified by date_format, which supports the same string format as the python standard library. Details of the string format can be found in `python string format - doc `__. + doc `__. Formats supported by the C `strftime` API but not by the python string format doc (such as `"%%R"`, `"%%r"`) are not officially supported and should be From 6fcd50d629a437c1d924f39e6f80d6994c14c129 Mon Sep 17 00:00:00 2001 From: cloudboat <15851404+cloudboat111@user.noreply.gitee.com> Date: Wed, 12 Nov 2025 14:44:30 +0800 Subject: [PATCH 4/9] remove %% --- pandas/core/arrays/datetimelike.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index 3948dd539345a..adf94e62979f6 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -1788,9 +1788,9 @@ def strftime(self, date_format: str) -> npt.NDArray[np.object_]: #strftime-and-strptime-behavior>`__. Formats supported by the C `strftime` API but not by the python string format - doc (such as `"%%R"`, `"%%r"`) are not officially supported and should be - preferably replaced with their supported equivalents (such as `"%%H:%%M"`, - `"%%I:%%M:%%S %%p"`). + doc (such as `"%R"`, `"%r"`) are not officially supported and should be + preferably replaced with their supported equivalents (such as `"%H:%M"`, + `"%I:%M:%S %p"`). Note that `PeriodIndex` support additional directives, detailed in `Period.strftime`. @@ -1817,7 +1817,7 @@ def strftime(self, date_format: str) -> npt.NDArray[np.object_]: Examples -------- >>> rng = pd.date_range(pd.Timestamp("2018-03-10 09:00"), periods=3, freq="s") - >>> rng.strftime("%%B %%d, %%Y, %%r") + >>> rng.strftime("%B %d, %Y, %r") Index(['March 10, 2018, 09:00:00 AM', 'March 10, 2018, 09:00:01 AM', 'March 10, 2018, 09:00:02 AM'], dtype='str') From 26bade0cd42a2957c1bba978a602208c38a8446d Mon Sep 17 00:00:00 2001 From: cloudboat <15851404+cloudboat111@user.noreply.gitee.com> Date: Wed, 26 Nov 2025 15:57:24 +0800 Subject: [PATCH 5/9] fix run doctests --- pandas/core/arrays/datetimelike.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index adf94e62979f6..a58fde95dd0a7 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -2353,12 +2353,12 @@ def round( >>> rng_tz = pd.DatetimeIndex(["2021-10-31 03:30:00"], tz="Europe/Amsterdam") >>> rng_tz.floor("2h", ambiguous=False) - DatetimeIndex(['2021-10-31 02:00:00+01:00'], - dtype='datetime64[s, Europe/Amsterdam]', freq=None) + DatetimeIndex(['2021-10-31 02:00:00+01:00'],dtype=( + 'datetime64[s, Europe/Amsterdam]'), freq=None) >>> rng_tz.floor("2h", ambiguous=True) - DatetimeIndex(['2021-10-31 02:00:00+02:00'], - dtype='datetime64[s, Europe/Amsterdam]', freq=None) + DatetimeIndex(['2021-10-31 02:00:00+02:00'],dtype=( + 'datetime64[s, Europe/Amsterdam]'), freq=None) """ return self._round(freq, RoundTo.NEAREST_HALF_EVEN, ambiguous, nonexistent) @@ -2439,9 +2439,9 @@ def floor( dtype='datetime64[ns]', freq='min') >>> rng.floor("h") - DatetimeIndex(['2018-01-01 11:00:00', '2018-01-01 12:00:00', - '2018-01-01 12:00:00'], - dtype='datetime64[ns]', freq=None) + DatetimeIndex(['2018-01-01 11:00:00', '2018-01-01 12:00:00', + '2018-01-01 12:00:00'], + dtype='datetime64[ns]', freq=None) **Series** @@ -2456,11 +2456,11 @@ def floor( >>> rng_tz = pd.DatetimeIndex(["2021-10-31 03:30:00"], tz="Europe/Amsterdam") - >>> rng_tz.floor("2h", ambiguous=False) + >>> rng_tz.floor("2h", ambiguous=False) # doctest: +NORMALIZE_WHITESPACE DatetimeIndex(['2021-10-31 02:00:00+01:00'], dtype='datetime64[s, Europe/Amsterdam]', freq=None) - >>> rng_tz.floor("2h", ambiguous=True) + >>> rng_tz.floor("2h", ambiguous=True) # doctest: +NORMALIZE_WHITESPACE DatetimeIndex(['2021-10-31 02:00:00+02:00'], dtype='datetime64[s, Europe/Amsterdam]', freq=None) """ @@ -2543,9 +2543,9 @@ def ceil( dtype='datetime64[ns]', freq='min') >>> rng.ceil("h") - DatetimeIndex(['2018-01-01 12:00:00', '2018-01-01 12:00:00', - '2018-01-01 13:00:00'], - dtype='datetime64[ns]', freq=None) + DatetimeIndex(['2018-01-01 12:00:00', '2018-01-01 12:00:00', + '2018-01-01 13:00:00'], + dtype='datetime64[ns]', freq=None) **Series** From 76cb3ce392254784355211450d20b567bf9aa12c Mon Sep 17 00:00:00 2001 From: cloudboat <15851404+cloudboat111@user.noreply.gitee.com> Date: Wed, 26 Nov 2025 16:32:47 +0800 Subject: [PATCH 6/9] fix precision --- pandas/core/arrays/datetimelike.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index 3a2b113c46766..d386b4d491844 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -2334,7 +2334,7 @@ def round( '2018-01-01 12:01:00'], dtype='datetime64[ns]', freq='min') - >>> rng.round("h") + >>> rng.round('h') DatetimeIndex(['2018-01-01 12:00:00', '2018-01-01 12:00:00', '2018-01-01 12:00:00'], dtype='datetime64[ns]', freq=None) @@ -2353,12 +2353,12 @@ def round( >>> rng_tz = pd.DatetimeIndex(["2021-10-31 03:30:00"], tz="Europe/Amsterdam") >>> rng_tz.floor("2h", ambiguous=False) - DatetimeIndex(['2021-10-31 02:00:00+01:00'],dtype=( - 'datetime64[s, Europe/Amsterdam]'), freq=None) + DatetimeIndex(['2021-10-31 02:00:00+01:00'], + dtype='datetime64[us, Europe/Amsterdam]', freq=None) >>> rng_tz.floor("2h", ambiguous=True) - DatetimeIndex(['2021-10-31 02:00:00+02:00'],dtype=( - 'datetime64[s, Europe/Amsterdam]'), freq=None) + DatetimeIndex(['2021-10-31 02:00:00+02:00'], + dtype='datetime64[us, Europe/Amsterdam]', freq=None) """ return self._round(freq, RoundTo.NEAREST_HALF_EVEN, ambiguous, nonexistent) @@ -2438,12 +2438,12 @@ def floor( '2018-01-01 12:01:00'], dtype='datetime64[ns]', freq='min') - >>> rng.floor("h") + >>> rng.floor('h') DatetimeIndex(['2018-01-01 11:00:00', '2018-01-01 12:00:00', '2018-01-01 12:00:00'], dtype='datetime64[ns]', freq=None) - **Series** + **Series** >>> pd.Series(rng).dt.floor("h") 0 2018-01-01 11:00:00 @@ -2456,13 +2456,13 @@ def floor( >>> rng_tz = pd.DatetimeIndex(["2021-10-31 03:30:00"], tz="Europe/Amsterdam") - >>> rng_tz.floor("2h", ambiguous=False) # doctest: +NORMALIZE_WHITESPACE + >>> rng_tz.floor("2h", ambiguous=False) DatetimeIndex(['2021-10-31 02:00:00+01:00'], - dtype='datetime64[s, Europe/Amsterdam]', freq=None) + dtype='datetime64[us, Europe/Amsterdam]', freq=None) - >>> rng_tz.floor("2h", ambiguous=True) # doctest: +NORMALIZE_WHITESPACE + >>> rng_tz.floor("2h", ambiguous=True) DatetimeIndex(['2021-10-31 02:00:00+02:00'], - dtype='datetime64[s, Europe/Amsterdam]', freq=None) + dtype='datetime64[us, Europe/Amsterdam]', freq=None) """ return self._round(freq, RoundTo.MINUS_INFTY, ambiguous, nonexistent) @@ -2542,7 +2542,7 @@ def ceil( '2018-01-01 12:01:00'], dtype='datetime64[ns]', freq='min') - >>> rng.ceil("h") + >>> rng.ceil('h') DatetimeIndex(['2018-01-01 12:00:00', '2018-01-01 12:00:00', '2018-01-01 13:00:00'], dtype='datetime64[ns]', freq=None) @@ -2562,11 +2562,11 @@ def ceil( >>> rng_tz.ceil("h", ambiguous=False) DatetimeIndex(['2021-10-31 02:00:00+01:00'], - dtype='datetime64[s, Europe/Amsterdam]', freq=None) + dtype='datetime64[us, Europe/Amsterdam]', freq=None) >>> rng_tz.ceil("h", ambiguous=True) DatetimeIndex(['2021-10-31 02:00:00+02:00'], - dtype='datetime64[s, Europe/Amsterdam]', freq=None) + dtype='datetime64[us, Europe/Amsterdam]', freq=None) """ return self._round(freq, RoundTo.PLUS_INFTY, ambiguous, nonexistent) From a113e3e3eb1e9c24152c7932fa59b4165f1b1d9b Mon Sep 17 00:00:00 2001 From: cloudboat <15851404+cloudboat111@user.noreply.gitee.com> Date: Thu, 27 Nov 2025 13:05:00 +0800 Subject: [PATCH 7/9] Remove extra documents --- pandas/core/arrays/datetimelike.py | 150 ----------------------------- 1 file changed, 150 deletions(-) diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index d386b4d491844..988f1d469547f 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -1830,156 +1830,6 @@ def strftime(self, date_format: str) -> npt.NDArray[np.object_]: return result.astype(object, copy=False) -_round_doc = """ - Perform {op} operation on the data to the specified `freq`. - - Parameters - ---------- - freq : str or Offset - The frequency level to {op} the index to. Must be a fixed - frequency like 's' (second) not 'ME' (month end). See - :ref:`frequency aliases ` for - a list of possible `freq` values. - ambiguous : 'infer', bool-ndarray, 'NaT', default 'raise' - Only relevant for DatetimeIndex: - - - 'infer' will attempt to infer fall dst-transition hours based on - order - - bool-ndarray where True signifies a DST time, False designates - a non-DST time (note that this flag is only applicable for - ambiguous times) - - 'NaT' will return NaT where there are ambiguous times - - 'raise' will raise a ValueError if there are ambiguous - times. - - nonexistent : 'shift_forward', 'shift_backward', 'NaT', timedelta, default 'raise' - A nonexistent time does not exist in a particular timezone - where clocks moved forward due to DST. - - - 'shift_forward' will shift the nonexistent time forward to the - closest existing time - - 'shift_backward' will shift the nonexistent time backward to the - closest existing time - - 'NaT' will return NaT where there are nonexistent times - - timedelta objects will shift nonexistent times by the timedelta - - 'raise' will raise a ValueError if there are - nonexistent times. - - Returns - ------- - DatetimeIndex, TimedeltaIndex, or Series - Index of the same type for a DatetimeIndex or TimedeltaIndex, - or a Series with the same index for a Series. - - Raises - ------ - ValueError if the `freq` cannot be converted. - - See Also - -------- - DatetimeIndex.floor : Perform floor operation on the data to the specified `freq`. - DatetimeIndex.snap : Snap time stamps to nearest occurring frequency. - - Notes - ----- - If the timestamps have a timezone, {op}ing will take place relative to the - local ("wall") time and re-localized to the same timezone. When {op}ing - near daylight savings time, use ``nonexistent`` and ``ambiguous`` to - control the re-localization behavior. - - Examples - -------- - **DatetimeIndex** - - >>> rng = pd.date_range('1/1/2018 11:59:00', periods=3, freq='min') - >>> rng - DatetimeIndex(['2018-01-01 11:59:00', '2018-01-01 12:00:00', - '2018-01-01 12:01:00'], - dtype='datetime64[ns]', freq='min') - """ - -_round_example = """>>> rng.round('h') - DatetimeIndex(['2018-01-01 12:00:00', '2018-01-01 12:00:00', - '2018-01-01 12:00:00'], - dtype='datetime64[ns]', freq=None) - - **Series** - - >>> pd.Series(rng).dt.round("h") - 0 2018-01-01 12:00:00 - 1 2018-01-01 12:00:00 - 2 2018-01-01 12:00:00 - dtype: datetime64[ns] - - When rounding near a daylight savings time transition, use ``ambiguous`` or - ``nonexistent`` to control how the timestamp should be re-localized. - - >>> rng_tz = pd.DatetimeIndex(["2021-10-31 03:30:00"], tz="Europe/Amsterdam") - - >>> rng_tz.floor("2h", ambiguous=False) - DatetimeIndex(['2021-10-31 02:00:00+01:00'], - dtype='datetime64[us, Europe/Amsterdam]', freq=None) - - >>> rng_tz.floor("2h", ambiguous=True) - DatetimeIndex(['2021-10-31 02:00:00+02:00'], - dtype='datetime64[us, Europe/Amsterdam]', freq=None) - """ - -_floor_example = """>>> rng.floor('h') - DatetimeIndex(['2018-01-01 11:00:00', '2018-01-01 12:00:00', - '2018-01-01 12:00:00'], - dtype='datetime64[ns]', freq=None) - - **Series** - - >>> pd.Series(rng).dt.floor("h") - 0 2018-01-01 11:00:00 - 1 2018-01-01 12:00:00 - 2 2018-01-01 12:00:00 - dtype: datetime64[ns] - - When rounding near a daylight savings time transition, use ``ambiguous`` or - ``nonexistent`` to control how the timestamp should be re-localized. - - >>> rng_tz = pd.DatetimeIndex(["2021-10-31 03:30:00"], tz="Europe/Amsterdam") - - >>> rng_tz.floor("2h", ambiguous=False) - DatetimeIndex(['2021-10-31 02:00:00+01:00'], - dtype='datetime64[us, Europe/Amsterdam]', freq=None) - - >>> rng_tz.floor("2h", ambiguous=True) - DatetimeIndex(['2021-10-31 02:00:00+02:00'], - dtype='datetime64[us, Europe/Amsterdam]', freq=None) - """ - -_ceil_example = """>>> rng.ceil('h') - DatetimeIndex(['2018-01-01 12:00:00', '2018-01-01 12:00:00', - '2018-01-01 13:00:00'], - dtype='datetime64[ns]', freq=None) - - **Series** - - >>> pd.Series(rng).dt.ceil("h") - 0 2018-01-01 12:00:00 - 1 2018-01-01 12:00:00 - 2 2018-01-01 13:00:00 - dtype: datetime64[ns] - - When rounding near a daylight savings time transition, use ``ambiguous`` or - ``nonexistent`` to control how the timestamp should be re-localized. - - >>> rng_tz = pd.DatetimeIndex(["2021-10-31 01:30:00"], tz="Europe/Amsterdam") - - >>> rng_tz.ceil("h", ambiguous=False) - DatetimeIndex(['2021-10-31 02:00:00+01:00'], - dtype='datetime64[us, Europe/Amsterdam]', freq=None) - - >>> rng_tz.ceil("h", ambiguous=True) - DatetimeIndex(['2021-10-31 02:00:00+02:00'], - dtype='datetime64[us, Europe/Amsterdam]', freq=None) - """ - - class TimelikeOps(DatetimeLikeArrayMixin): """ Common ops for TimedeltaIndex/DatetimeIndex, but not PeriodIndex. From cbcad50562de2a4bce59c4ed256bcfb9d54c5f84 Mon Sep 17 00:00:00 2001 From: cloudboat <15851404+cloudboat111@user.noreply.gitee.com> Date: Sat, 29 Nov 2025 15:08:45 +0800 Subject: [PATCH 8/9] Fix the accuracy --- pandas/core/arrays/datetimelike.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index 988f1d469547f..f3286dab43a10 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -1647,7 +1647,7 @@ def mean(self, *, skipna: bool = True, axis: AxisInt | None = 0): >>> idx = pd.date_range("2001-01-01 00:00", periods=3) >>> idx DatetimeIndex(['2001-01-01', '2001-01-02', '2001-01-03'], - dtype='datetime64[ns]', freq='D') + dtype='datetime64[us]', freq='D') >>> idx.mean() Timestamp('2001-01-02 00:00:00') @@ -1656,7 +1656,7 @@ def mean(self, *, skipna: bool = True, axis: AxisInt | None = 0): >>> tdelta_idx = pd.to_timedelta([1, 2, 3], unit="D") >>> tdelta_idx TimedeltaIndex(['1 days', '2 days', '3 days'], - dtype='timedelta64[ns]', freq=None) + dtype='timedelta64[us]', freq=None) >>> tdelta_idx.mean() Timedelta('2 days 00:00:00') """ @@ -2182,12 +2182,12 @@ def round( >>> rng DatetimeIndex(['2018-01-01 11:59:00', '2018-01-01 12:00:00', '2018-01-01 12:01:00'], - dtype='datetime64[ns]', freq='min') + dtype='datetime64[us]', freq='min') >>> rng.round('h') DatetimeIndex(['2018-01-01 12:00:00', '2018-01-01 12:00:00', '2018-01-01 12:00:00'], - dtype='datetime64[ns]', freq=None) + dtype='datetime64[us]', freq=None) **Series** @@ -2195,7 +2195,7 @@ def round( 0 2018-01-01 12:00:00 1 2018-01-01 12:00:00 2 2018-01-01 12:00:00 - dtype: datetime64[ns] + dtype: datetime64[us] When rounding near a daylight savings time transition, use ``ambiguous`` or ``nonexistent`` to control how the timestamp should be re-localized. @@ -2286,12 +2286,12 @@ def floor( >>> rng DatetimeIndex(['2018-01-01 11:59:00', '2018-01-01 12:00:00', '2018-01-01 12:01:00'], - dtype='datetime64[ns]', freq='min') + dtype='datetime64[us]', freq='min') >>> rng.floor('h') DatetimeIndex(['2018-01-01 11:00:00', '2018-01-01 12:00:00', '2018-01-01 12:00:00'], - dtype='datetime64[ns]', freq=None) + dtype='datetime64[us]', freq=None) **Series** @@ -2299,7 +2299,7 @@ def floor( 0 2018-01-01 11:00:00 1 2018-01-01 12:00:00 2 2018-01-01 12:00:00 - dtype: datetime64[ns] + dtype: datetime64[us] When rounding near a daylight savings time transition, use ``ambiguous`` or ``nonexistent`` to control how the timestamp should be re-localized. @@ -2390,12 +2390,12 @@ def ceil( >>> rng DatetimeIndex(['2018-01-01 11:59:00', '2018-01-01 12:00:00', '2018-01-01 12:01:00'], - dtype='datetime64[ns]', freq='min') + dtype='datetime64[us]', freq='min') >>> rng.ceil('h') DatetimeIndex(['2018-01-01 12:00:00', '2018-01-01 12:00:00', '2018-01-01 13:00:00'], - dtype='datetime64[ns]', freq=None) + dtype='datetime64[us]', freq=None) **Series** @@ -2403,7 +2403,7 @@ def ceil( 0 2018-01-01 12:00:00 1 2018-01-01 12:00:00 2 2018-01-01 13:00:00 - dtype: datetime64[ns] + dtype: datetime64[us] When rounding near a daylight savings time transition, use ``ambiguous`` or ``nonexistent`` to control how the timestamp should be re-localized. From 7a1690c6677fd2effc4997cd7784483718823171 Mon Sep 17 00:00:00 2001 From: cloudboat <15851404+cloudboat111@user.noreply.gitee.com> Date: Sat, 29 Nov 2025 15:35:12 +0800 Subject: [PATCH 9/9] Fix the accuracy for specific problem --- pandas/core/arrays/datetimelike.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index d4f5134ca18eb..c55a5d6dba175 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -1656,7 +1656,7 @@ def mean(self, *, skipna: bool = True, axis: AxisInt | None = 0): >>> tdelta_idx = pd.to_timedelta([1, 2, 3], unit="D") >>> tdelta_idx TimedeltaIndex(['1 days', '2 days', '3 days'], - dtype='timedelta64[us]', freq=None) + dtype='timedelta64[ns]', freq=None) >>> tdelta_idx.mean() Timedelta('2 days 00:00:00') """