user2123288
user2123288

Reputation: 1119

Convert freq string to DateOffset in pandas

In pandas documentation one can read "Under the hood, these frequency strings are being translated into an instance of pandas DateOffset" when speaking of freq string such as "W" or "W-SUN".

Then, how can I get an instance of a DateOffset given a string ? Ultimately want to configure my program with frequency as string (say "W-SUN"), but internally want to do something like

offset = Week(weekday=0)
if d1-3*offset<d2:
    pass

but defining offset from string.

Thanks

Upvotes: 11

Views: 2818

Answers (1)

joris
joris

Reputation: 139292

You could use the to_offset function for this, although this is a rather internal pandas function (so possibly no backwards compatibility guaranteed). Some examples:

In [12]: pd.tseries.frequencies.to_offset('4Min')
Out[12]: <4 * Minutes>

In [13]: pd.tseries.frequencies.to_offset('W-SUN')
Out[13]: <Week: weekday=6>

Upvotes: 17

Related Questions