Convert freq string to DateOffset in pandas

可紊 提交于 2021-02-18 22:10:31

问题


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


回答1:


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>


来源:https://stackoverflow.com/questions/27368265/convert-freq-string-to-dateoffset-in-pandas

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!