Description
-
I have checked that this issue has not already been reported.
(Might be related to 33647, though there localization does not seem to play a role) -
I have confirmed this bug exists on the latest version of pandas.
-
(optional) I have confirmed this bug exists on the master branch of pandas.
Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.
Code Sample, a copy-pastable example
import pandas as pd
s = pd.Series([2,5,3], index=pd.date_range('2020-01-01', periods=3,
freq='M', tz='Europe/Berlin'))
s.index #freq correctly set
DatetimeIndex(['2020-01-31 00:00:00+01:00', '2020-02-29 00:00:00+01:00',
'2020-03-31 00:00:00+02:00'],
dtype='datetime64[ns, Europe/Berlin]', name='ts', freq='M')
Then changing to MultiIndex
:
s.index = pd.MultiIndex.from_arrays([s.index, s.index.month])
s.index.get_level_values(0) #freq has been lost
DatetimeIndex(['2020-01-31 00:00:00+01:00', '2020-02-29 00:00:00+01:00',
'2020-03-31 00:00:00+02:00'],
dtype='datetime64[ns, Europe/Berlin]', name='ts', freq=None
Notice the freq=None
.
Problem description
In the snipped above, the freq
attribute of the DateTimeIndex
is lost when a MultiIndex
is created from it.
The problem seems to stem from the localization. It's equally present in DataFrames and when using set_index
/tz_localize
to promote a normal datetime column to index.
Expected Output
I expect the freq
attribute to persist, as it does in this snippet, when I do not localize the index:
s2 = pd.Series([2,5,3], index=pd.date_range('2020-01-01', periods=3,
freq='M')) #NB no localization
s2.index = pd.MultiIndex.from_arrays([s2.index, s2.index.month]) #same as above
s2.index.get_level_values(0)
DatetimeIndex(['2020-01-31', '2020-02-29', '2020-03-31'],
dtype='datetime64[ns]', freq='M')
Output of pd.show_versions()
pandas : 1.0.3
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 46.1.3.post20200330
Cython : None
pytest : 5.4.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 7.13.0
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : 3.1.3
numexpr : None
odfpy : None
openpyxl : 3.0.3
pandas_gbq : None
pyarrow : None
pytables : None
pytest : 5.4.1
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : None
xlsxwriter : None
numba : None