Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert freq string to DateOffset in pandas

Tags:

python

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

like image 305
user2123288 Avatar asked Dec 08 '14 22:12

user2123288


1 Answers

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>
like image 62
joris Avatar answered Oct 22 '22 04:10

joris