-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: DatetimeIndex.to_period().to_timestamp() forgets freq value #38885
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
long story short: no. what you can do is something like
|
@jbrockmendel there is also an issue with idx = pd.date_range(start='1/1/2018 00:00:00', end='1/5/2018 00:01:00', freq="MS")
idx[-1].to_period()
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
pandas\_libs\tslibs\period.pyx in pandas._libs.tslibs.period.freq_to_dtype_code()
AttributeError: 'pandas._libs.tslibs.offsets.MonthBegin' object has no attribute '_period_dtype_code'
The above exception was the direct cause of the following exception:
ValueError Traceback (most recent call last)
<ipython-input-16-779b680b91e8> in <module>
1 idx = pd.date_range(start='1/1/2018 00:00:00', end='1/5/2018 00:01:00', freq="MS")
----> 2 idx[-1].to_period()
pandas\_libs\tslibs\timestamps.pyx in pandas._libs.tslibs.timestamps._Timestamp.to_period()
pandas\_libs\tslibs\period.pyx in pandas._libs.tslibs.period.Period.__new__()
pandas\_libs\tslibs\period.pyx in pandas._libs.tslibs.period.freq_to_dtype_code()
ValueError: Invalid frequency: <MonthBegin> This works for some Workaround: idx = pd.date_range(start='1/1/2018 00:00:00', end='1/5/2018 00:01:00', freq="MS")
cutoff = idx[-1]
date = pd.DatetimeIndex([cutoff], freq=cutoff.freq)
cutoff = date.to_period()[0]
cutoff Results in: |
@jbrockmendel Your solution is still resulting in dti = pd.date_range(start='1/1/2018 00:00:00', end='3/1/2018 00:01:00', freq="MS")
pi = dti.to_period()
roundtrip = pi.to_timestamp()
roundtrip = pd.DatetimeIndex(roundtrip, freq="infer")
roundtrip
|
Yes. I think with length<3 you'll need to specify
That should be caught and re-raised with a more helpful exception+message. PR would be wlecome. |
@jbrockmendel I raised a separate issue #38914 |
Well I think no, unless we make an enhancement request out of it so that it should be possible to do conversion between |
Since it seems like there isn't any much more movement on this issue to create a new |
But the issue is not solved, so maybe better to keep it open? |
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
Why is
freq=None
here in the end? Is it possible to make conversion betweenDatetimeIndex
andPeriodIndex
more reliable in any way? How can I know whichfreq
values are not able to be converted fromPeriodIndex
intoDatetimeIndex
?Expected Output:
I would expect to have the same
freq
value after doing.to_period().to_timestamp()
.Output of
pd.show_versions()
The text was updated successfully, but these errors were encountered: