Pandas version checks
-
[x] I have checked that this issue has not already been reported.
-
[x] I have confirmed this bug exists on the latest version of pandas.
-
[ ] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
>>> import pandas as pd
>>> i = pd.date_range('2020-02-01', freq='QS-MAY', periods=3)
>>> i
DatetimeIndex(['2020-02-01', '2020-05-01', '2020-08-01'], dtype='datetime64[ns]', freq='QS-MAY')
>>> pd.DatetimeIndex(i, freq='QS-FEB')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Program Files\Python312\Lib\site-packages\pandas\core\indexes\datetimes.py", line 370, in __new__
dtarr = DatetimeArray._from_sequence_not_strict(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\Python312\Lib\site-packages\pandas\core\arrays\datetimes.py", line 394, in _from_sequence_not_strict
result._maybe_pin_freq(freq, validate_kwds)
File "C:\Program Files\Python312\Lib\site-packages\pandas\core\arrays\datetimelike.py", line 2094, in _maybe_pin_freq
_validate_inferred_freq(freq, self._freq)
File "C:\Program Files\Python312\Lib\site-packages\pandas\core\arrays\datetimelike.py", line 2526, in _validate_inferred_freq
raise ValueError(
ValueError: Inferred frequency <QuarterBegin: startingMonth=5> from passed values does not conform to passed frequency QS-FEB
Issue Description
pd.DatetimeIndex(...)
, when passed both an index with a frequency, AND a frequency, throws an error if the frequencies are unequal - even if they are equivalent.
Expected Behavior
If the frequencies are unequal, but equivalent (like in the example), an index with the specified (new) frequency should be returned.
That the frequencies are equivalent can be seen in the following snippet: simply direct setting the frequency of i
to the specified frequency DOES work:
>>> i = pd.date_range('2020-02-01', freq='QS-MAY', periods=3)
>>> i.freq = 'QS-FEB'
>>> i
DatetimeIndex(['2020-02-01', '2020-05-01', '2020-08-01'], dtype='datetime64[ns]', freq='QS-FEB')
I think this only occurs for quarter-frequency, where "equivalent" means that the starting month mod 3 is equal.
Current workaround:
We don't want to change i
, so the current workaround to i2 = pd.DatetimeIndex(i, freq='QS-FEB')
is
i2 = i.copy()
i2.freq = 'QS-FEB'
...which is cumbersome, because it requires 2 steps and cannot be used in list comprehensions etc.
Alternatively, there is the elegant
i2 = pd.DatetimeIndex(i, freq=None).to_frame().asfreq(i.freq).index
Installed Versions
Comment From: rhshadrach
Thanks for the report. When setting the attribute, pandas generates the NumPy array with the new frequency and confirms that it matches the data underlying the DatetimeIndex. I'd be supportive of doing the same in DatetimeIndex.__init__
.
~I think we should also just be comparing frequency strings for those that do not have equivalents, and possibly fast-pathing other cases (i.e. not generating an entire new NumPy array). But that can be for another issue.~
Edit: Any frequency can be equivalent to some other, I think.
Comment From: rwijtvliet
Thanks for the quick triage!
Another welcome addition would be a method like DatetimeIndex.set_freq()
, returning a copy of the index but with the specified frequency.
Analogous methods exist for changing other properties of (a copy of) the index - to such as DatetimeIndex.rename()
(for the index name), .tz_localize()
and .tz_convert()
(for the timezone).
If that exists, I'd prefer it over using DatetimeIndex.__init__
, as it is more specific and signals intent.
Comment From: ycdjun
Take
Comment From: rhshadrach
Another welcome addition would be a method like
DatetimeIndex.set_freq()
, returning a copy of the index but with the specified frequency.
I think this should be separated off into its own issue. Can you open a new one with this enhancement request.
Comment From: rwijtvliet
Another welcome addition would be a method like
DatetimeIndex.set_freq()
, returning a copy of the index but with the specified frequency.I think this should be separated off into its own issue. Can you open a new one with this enhancement request.
Yes, I agree. See here.