-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
BUG: tz_localize
drops freq
from DatetimIndex
#36575
Comments
Originally posted in #33677 |
It actually works if print(s.tz_localize("europe/London").index.freq) # None
|
Extended the example to show the impact is solely to Index.
On version 1.0.4:
On version 1.1.2:
|
Also link to here: #33940 (comment) |
Well, that's because the import pandas as pd
date_range = pd.date_range("2020", periods=5, freq="D")
date_range.freq = None # remove frequency from index
s = pd.Series(data=date_range, index=None)
print(s.dt.freq) # 'D' - dt infers frequency |
Freq is still being dropped by tz_localize. It is actually maintained if you use tz_localize via dt: import pandas as pd
s = pd.Series(
index=pd.date_range("2020", periods=5, freq="D"),
data=pd.date_range("2020", periods=5, freq="D"),
name="date"
)
print(s.index.freq)
print(s.tz_localize("europe/london").index.freq)
print(s.dt.tz_localize("europe/london").index.freq) Prints:
However, if you directly work with the index this import pandas as pd
s = pd.Series(
index=pd.date_range("2020", periods=5, freq="D"),
data=pd.date_range("2020", periods=5, freq="D"),
name="date"
)
index = s.index
index_aware = index.tz_localize("europe/london")
print(index.freq)
print(index_aware.freq)
index_aware.freq = index_aware.inferred_freq
print(index_aware.freq) Prints:
No clue how robust this is, so it would be great if the freq can just be passed when applying |
I have checked that this issue has not already been reported.
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
Problem description
The index should retain the
freq
attribute despite localization.Output of
pd.show_versions()
It raises
The text was updated successfully, but these errors were encountered: