diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index 62831f941b535..c3d3a2432df54 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -123,6 +123,28 @@ def freq(self) -> BaseOffset | None: >>> datetimeindex.freq """ + """# Conceptual fix (not in the provided file) +# In the DatetimeIndex class definition: + +def __sub__(self, other): + if isinstance(other, DatetimeIndex): + # ... existing code for subtraction ... + pass + + if isinstance(other, Timestamp): + # Perform the subtraction + # The result of the subtraction is a TimedeltaArray + result_array = self._data - other + + # Check if the original DatetimeIndex has a frequency. + # This is the key part of the fix. + freq = self.freq + + # Create the new TimedeltaIndex with the propagated frequency. + # This ensures the `freq` attribute is not None. + result_index = TimedeltaIndex(result_array, freq=freq) + return result_index + # ... other subtraction logic ...""" return self._data.freq @freq.setter