给定偏移量字符串,如何获取pandas.offsets对象 [英] how do I get at the pandas.offsets object given an offset string

查看:571
本文介绍了给定偏移量字符串,如何获取pandas.offsets对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个偏移字符串'BM''7W'
我知道'BM'的答案是pd.offsets.BMonthEnd()
对于'7W'pd.offsets.Week(7)

Suppose I have an offset string 'BM' or '7W'
I know the answer for 'BM' is pd.offsets.BMonthEnd()
for '7W' is pd.offsets.Week(7)

有没有通用的解决方案,可以传递字符串并获取偏移量对象?

Is there a generic solution in which I can pass a string and get the offset object?

推荐答案

它看起来像通过采用琐碎的DateTimeIndexfreq属性,您也可以更轻松地获得它,而无需任何导入:

You can also get it in more of a hackier way without any imports by taking the freq attribute of a trivial DateTimeIndex:

freq = pd.date_range('2016-03-14', periods=0, freq='7W').freq

使用任何一种方法:

print(freq)
<7 * Weeks: weekday=6>

print(type(freq))
<class 'pandas.tseries.offsets.Week'>

这篇关于给定偏移量字符串,如何获取pandas.offsets对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆