Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
88 views
in Technique[技术] by (71.8m points)

python - Convert `pandas` frequency string to `DateOffset`

I have a timezone-aware pandas DateTimeIndex, which I would like to advance by one timestep, with the timestep as specified by its .freq attribute. However, doing this does not respect the time zone information:

import pandas as pd
i = pd.date_range('2020-03-28', freq='D', periods=3, tz='Europe/Amsterdam')
# DatetimeIndex(['2020-03-28 00:00:00+01:00', '2020-03-29 00:00:00+01:00',
#                '2020-03-30 00:00:00+02:00'],
#               dtype='datetime64[ns, Europe/Amsterdam]', freq='D')

i + i.freq
# Not what I want; second timestamp is advanced by 24h instead of 23h and is no longer at midnight:
# DatetimeIndex(['2020-03-29 00:00:00+01:00', '2020-03-30 01:00:00+02:00',
#                '2020-03-31 00:00:00+02:00'],
#               dtype='datetime64[ns, Europe/Amsterdam]', freq='D')

What does work is using pd.DateOffset:

i + pd.DateOffset(days=1)
# What I want; all timestamps at midnight (I just need to re-set the .freq attribute):
# DatetimeIndex(['2020-03-29 00:00:00+01:00', '2020-03-30 00:00:00+02:00',
#                '2020-03-31 00:00:00+02:00'],
#               dtype='datetime64[ns, Europe/Amsterdam]', freq=None)

However, as I don't know in advance what the frequency of the index will be, I'd like to use the value of i.freq to get the correct DateOffset. Is there a way to do this? (Apart from using a long if... elif... elif... block.)

Other solutions also welcome, of course.

This is the only other question related to this that I found, but I cannot use it here:

i + pd.tseries.frequencies.to_offset(i.freq)
# Not what I want:
# DatetimeIndex(['2020-03-29 00:00:00+01:00', '2020-03-30 01:00:00+02:00',
#                '2020-03-31 00:00:00+02:00'],
#               dtype='datetime64[ns, Europe/Amsterdam]', freq='D')

(In fact, the latter term returns exactly i.freq.)

Many thanks.

EDIT (1)

As suggested in the comments, using .shift(1) works in some cases, including in my stated case above...

i.shift(1)
# What I want; all timestamps at midnight:
# DatetimeIndex(['2020-03-29 00:00:00+01:00', '2020-03-30 00:00:00+02:00',
#                '2020-03-31 00:00:00+02:00'],
#               dtype='datetime64[ns, Europe/Amsterdam]', freq='D')

...but not in all. In fact, advancing the start date in my original index by one day causes a timestamp to get dropped, and the remaining ones are wrong:

i2 = pd.date_range('2020-03-29', freq='D', periods=3, tz='Europe/Amsterdam')
# DatetimeIndex(['2020-03-29 00:00:00+01:00', '2020-03-30 00:00:00+02:00',
#               '2020-03-31 00:00:00+02:00'],
#              dtype='datetime64[ns, Europe/Amsterdam]', freq='D')

i2.shift(1)
# Not what I want: timestamps not at midnight, and one got dropped!
# DatetimeIndex(['2020-03-30 01:00:00+02:00', '2020-03-31 01:00:00+02:00'],
#               dtype='datetime64[ns, Europe/Amsterdam]', freq='D')

EDIT (2)

As suggested in the answer by @MrFruppes, using the .nanos attribute of i.freq works as input to pd.DateOffset...

i + pd.DateOffset(nanoseconds=i.freq.nanos)
# What I want; all timestamps at midnight (I just need to re-set the .freq attribute):
# DatetimeIndex(['2020-03-29 00:00:00+01:00', '2020-03-30 00:00:00+02:00',
#                '2020-03-31 00:00:00+02:00'],
#               dtype='datetime64[ns, Europe/Amsterdam]', freq=None)

... but it breaks when we try to advance to the beginning of next month:

i3 = pd.date_range('2020-03-01', freq='MS', periods=3, tz='Europe/Amsterdam')
# DatetimeIndex(['2020-03-01 00:00:00+01:00', '2020-04-01 00:00:00+02:00',
#                '2020-05-01 00:00:00+02:00'],
#               dtype='datetime64[ns, Europe/Amsterdam]', freq='MS')

i3 + pd.DateOffset(nanoseconds=i3.freq.nanos)
Traceback (most recent call last):

  File "<ipython-input-58-f3a32c654a6e>", line 1, in <module>
    i3 + pd.DateOffset(nanoseconds=i3.freq.nanos)

  File "pandas\_libsslibsoffsets.pyx", line 690, in pandas._libs.tslibs.offsets.BaseOffset.nanos.__get__

ValueError: <MonthBegin> is a non-fixed frequency
question from:https://stackoverflow.com/questions/65890605/convert-pandas-frequency-string-to-dateoffset

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you have a fixed frequency, you can use the nanos property of the freq. Ex:

import pandas as pd
i = pd.date_range('2020-03-29', freq='D', periods=3, tz='Europe/Amsterdam')
# DatetimeIndex(['2020-03-29 00:00:00+01:00', '2020-03-30 00:00:00+02:00',
#               '2020-03-31 00:00:00+02:00'],
#              dtype='datetime64[ns, Europe/Amsterdam]', freq='D')

i + pd.DateOffset(nanoseconds=i.freq.nanos)
# DatetimeIndex(['2020-03-30 00:00:00+02:00', '2020-03-31 00:00:00+02:00',
#                '2020-04-01 00:00:00+02:00'],
#               dtype='datetime64[ns, Europe/Amsterdam]', freq=None)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...