You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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.
Pandas version checks
I have checked that this issue has not already been reported.
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
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 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 toi2 = pd.DatetimeIndex(i, freq='QS-FEB')
is...which is cumbersome, because it requires 2 steps and cannot be used in list comprehensions etc.
Alternatively, there is the elegant
Installed Versions
pandas : 2.2.3
numpy : 2.1.1
pytz : 2024.2
dateutil : 2.9.0.post0
pip : 24.2
Cython : None
sphinx : None
IPython : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : None
lxml.etree : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : None
tzdata : 2024.2
qtpy : None
pyqt5 : None
The text was updated successfully, but these errors were encountered: