Skip to content

BUG: frequency attribute lost if datetimeindex is localized #33677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
2 of 3 tasks
rwijtvliet opened this issue Apr 20, 2020 · 7 comments
Open
2 of 3 tasks

BUG: frequency attribute lost if datetimeindex is localized #33677

rwijtvliet opened this issue Apr 20, 2020 · 7 comments
Labels
Bug Datetime Datetime data dtype freq retention User expects "freq" attribute to be preserved Frequency DateOffsets

Comments

@rwijtvliet
Copy link

  • 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()

INSTALLED VERSIONS ------------------ commit : None python : 3.8.2.final.0 python-bits : 64 OS : Windows OS-release : 10 machine : AMD64 processor : Intel64 Family 6 Model 158 Stepping 10, GenuineIntel byteorder : little LC_ALL : None LANG : en LOCALE : de_DE.cp1252

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

@rwijtvliet rwijtvliet added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 20, 2020
@rwijtvliet rwijtvliet changed the title BUG: BUG: frequency attribute lost if datetimeindex is localized Apr 20, 2020
@mroeschke
Copy link
Member

On master, it appears the second example drops frequency as well

In [6]: 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)
Out[6]: DatetimeIndex(['2020-01-31', '2020-02-29', '2020-03-31'], dtype='datetime64[ns]', freq=None)

In [7]: pd.__version__
Out[7]: '1.1.0.dev0+1316.g38e1b9bc0'

cc @jbrockmendel as you've been looking at frequency stuff lately.

@mroeschke mroeschke added Frequency DateOffsets Datetime Datetime data dtype and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Apr 20, 2020
@jbrockmendel
Copy link
Member

IIRC the issue here is that DatetimeIndex.factorize doesn't retain freq; it isn't obvious to me whether it should

@rwijtvliet
Copy link
Author

Thanks guys for looking into this so quickly.

As a workaround, is there a way to re-set it, after creation of the MultiIndex?

@jbrockmendel
Copy link
Member

As a workaround, is there a way to re-set it, after creation of the MultiIndex?

I strongly discourage this, and make no promises that it will continue to work, but

lvl = s2.index.levels[0]
lvl._data.freq = orig_freq

@giuliobeseghi
Copy link

giuliobeseghi commented Sep 23, 2020

It might be related to a bug with the tz_localize method of pd.Series:

import pandas as pd

s = pd.Series(
    [1, 2, 3, 4, 5], index=pd.date_range("2020", periods=5, freq="D")
)
print(s.index.freq)  # <Day>
print(s.tz_localize("europe/london").index.freq)  # None

@giuliobeseghi
Copy link

I just realised this is a separate bug. I'll open a new issue.

It might be related to a bug with the tz_localize method of pd.Series:

import pandas as pd

s = pd.Series(
    [1, 2, 3, 4, 5], index=pd.date_range("2020", periods=5, freq="D")
)
print(s.index.freq)  # <Day>
print(s.tz_localize("europe/london").index.freq)  # None

@giuliobeseghi
Copy link

@rwijtvliet I could recreate your problem with a naive index as well:

import pandas as pd

s = pd.Series(
    [2, 5, 3], index=pd.date_range("2020-01-01", periods=3, freq="M"),
)
s.index  # freq correctly set

s.index = pd.MultiIndex.from_arrays([s.index, s.index.month])
s.index.get_level_values(0)  # freq has been lost

Maybe the name of the issue is a bit misleading then?

@jbrockmendel jbrockmendel added the freq retention User expects "freq" attribute to be preserved label Aug 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype freq retention User expects "freq" attribute to be preserved Frequency DateOffsets
Projects
None yet
Development

No branches or pull requests

4 participants