-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Comments
On master, it appears the second example drops frequency as well
cc @jbrockmendel as you've been looking at frequency stuff lately. |
IIRC the issue here is that DatetimeIndex.factorize doesn't retain freq; it isn't obvious to me whether it should |
Thanks guys for looking into this so quickly. As a workaround, is there a way to re-set it, after creation of the |
I strongly discourage this, and make no promises that it will continue to work, but
|
It might be related to a bug with the 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 |
I just realised this is a separate bug. I'll open a new issue.
|
@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? |
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
Then changing to
MultiIndex
:Notice the
freq=None
.Problem description
In the snipped above, the
freq
attribute of theDateTimeIndex
is lost when aMultiIndex
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: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
The text was updated successfully, but these errors were encountered: