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.
-
[X] I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pandas as pd
idx = pd.date_range('20200101', periods=12, freq='BM')
df = pd.DataFrame(dict(a=range(12)), index=idx)
print(df.index.freq)
>> <BusinessMonthEnd>
df.stack()
print(df.index.freq()
>> None
Issue Description
Some of our CI pipeline unittests failed after upgrading to pandas 1.5.0 (linux). (I can't share this example.)
I tried to replicate the error above locally, and it seems to behave the same way. However, the same issue is only visible in ipython within my pycharm console. When I open a python instance via cmd, I cannot replicate the behaviour, which makes this bug a bit harder to trace I believe.
Expected Behavior
I would expect that the index.freq property would not be lost
Installed Versions
Comment From: vamsi-verma-s
Hi @MMCMA
I think it is only happening when I do df2=df.stack()
and look at df2.index
. pls try yourself once.
```Python console
In [64]: idx = pd.date_range('20200101', periods=12, freq='BM')
In [65]: df = pd.DataFrame(dict(a=range(12)), index=idx)
In [66]: df.index Out[66]: DatetimeIndex(['2020-01-31', '2020-02-28', '2020-03-31', '2020-04-30', '2020-05-29', '2020-06-30', '2020-07-31', '2020-08-31', '2020-09-30', '2020-10-30', '2020-11-30', '2020-12-31'], dtype='datetime64[ns]', freq='BM')
In [67]: df2 = df.stack()
In [68]: df.index Out[68]: DatetimeIndex(['2020-01-31', '2020-02-28', '2020-03-31', '2020-04-30', '2020-05-29', '2020-06-30', '2020-07-31', '2020-08-31', '2020-09-30', '2020-10-30', '2020-11-30', '2020-12-31'], dtype='datetime64[ns]', freq='BM')
In [69]: df2.index Out[69]: MultiIndex([('2020-01-31', 'a'), ('2020-02-28', 'a'), ('2020-03-31', 'a'), ('2020-04-30', 'a'), ('2020-05-29', 'a'), ('2020-06-30', 'a'), ('2020-07-31', 'a'), ('2020-08-31', 'a'), ('2020-09-30', 'a'), ('2020-10-30', 'a'), ('2020-11-30', 'a'), ('2020-12-31', 'a')], )
In [70]: df.index Out[70]: DatetimeIndex(['2020-01-31', '2020-02-28', '2020-03-31', '2020-04-30', '2020-05-29', '2020-06-30', '2020-07-31', '2020-08-31', '2020-09-30', '2020-10-30', '2020-11-30', '2020-12-31'], dtype='datetime64[ns]', freq=None)
**Comment From: MMCMA**
Yes, I can replicate this with pandas 1.5.0, I also checked an older pandas version and the freq property in `df.index` is not lost
**Comment From: phofl**
commit fb3e3e60963ae4808ce976fc0f89864122eced15 Author: Luke Manley lukemanley@gmail.com Date: Tue Mar 15 18:43:02 2022 -0400
PERF: MultiIndex.values for MI's with DatetimeIndex, TimedeltaIndex, or ExtensionDtype levels (#46288)
cc @lukemanley #46288
Simple reproducer:
idx = pd.date_range('20200101', periods=12, freq='BM')
df = pd.DataFrame(dict(a=range(12)), index=idx)
df2 = df.stack() df2.index.repr() assert df.index.freq is not None
**Comment From: MarcoGorelli**
Hey @MMCMA
Thanks for having reported this - there's now a release candidate for pandas 2.0.0, which you can install with
mamba install -c conda-forge/label/pandas_rc pandas==2.0.0rc0
or
python -m pip install --upgrade --pre pandas==2.0.0rc0 ``` If you try it out and report bugs, then we can fix them before the final 2.0.0 release