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
bus_day = pd.offsets.CustomBusinessDay(holidays=['2023-07-04']) # Mon-Fri except July 4th
dates = pd.date_range('2023-07-03', '2023-07-06', freq=bus_day)
df1 = pd.DataFrame({'col1': [300, 500, 600]}, index=dates)
print(df1.index.freq.holidays) # Can get freq from dataframe index
mi = pd.MultiIndex.from_product([dates, ('foo','bar')])
df2 = pd.DataFrame({'col1': [300, 301, 500, 501, 600, 601]}, index=mi)
print(df2.index.get_level_values(0).freq) # Gives None. Freq lost from dataframe multi-index
periods = pd.period_range('2023-07-03', '2023-07-06', freq=bus_day) # Try PeriodIndex?
# Gives: TypeError: CustomBusinessDay cannot be used with Period or PeriodDtype
Issue Description
I can use a CustomBusinessDay
frequency in a DatetimeIndex
and retrieve the frequency from the .index
of a dataframe.
If I use a DatetimeIndex
in a MultiIndex
, then I lose the freq
from the level of the index.
I note the freq
does persist in a PeriodIndex
within a MultiIndex
, but you can't use a CustomBusinessDay
in a PeriodIndex
as shown.
Expected Behavior
I would like the Datetime level of the MultiIndex to show the orginial freq:
print(df2.index.get_level_values(0).freq)
would give <CustomBusinessDay>
Installed Versions
Comment From: dontgoto
This loss of information does happen with the DateTimeIndex and not with the PeriodIndex. Reason is that the freq
is an attribute of each value in the PeriodIndex. With the DateTimeIndex, the freq, etc. get lost when calling get_level_values()
, due to the shallow copying logic.
The DateTimeIndex is the only index type with this edge case.
Comment From: cokelid
Thanks. Is this the shallow copy you're referring to?
return lev._shallow_copy(filled, name=name)