本文介绍了使用inferred_freq设置pandas.tseries.index.DatetimeIndex.freq的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑DatetimeIndex
tidx
tidx = pd.to_datetime(['2016-07-29', '2016-08-31', '2016-09-30'])
print(tidx.freq)
print(tidx.inferred_freq)
print(tidx)
None
BM
DatetimeIndex(['2016-07-29', '2016-08-31', '2016-09-30'], \
dtype='datetime64[ns]', freq=None)
我希望freq
属性承担inferred_freq
属性...所以我
I want the freq
attribute to assume the inferred_freq
attribute... so I
tidx.freq = tidx.inferred_freq
print(tidx)
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-24-b49b104fda04> in <module>()
1 tidx.freq = tidx.inferred_freq
----> 2 print(tidx)
C:\Users\ssmith\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\core\base.pyc in __str__(self)
45 if compat.PY3:
46 return self.__unicode__()
---> 47 return self.__bytes__()
48
49 def __bytes__(self):
C:\Users\ssmith\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\core\base.pyc in __bytes__(self)
57
58 encoding = get_option("display.encoding")
---> 59 return self.__unicode__().encode(encoding, 'replace')
60
61 def __repr__(self):
C:\Users\ssmith\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\indexes\base.pyc in __unicode__(self)
581 klass = self.__class__.__name__
582 data = self._format_data()
--> 583 attrs = self._format_attrs()
584 space = self._format_space()
585
C:\Users\ssmith\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\tseries\base.pyc in _format_attrs(self)
485 for attrib in self._attributes:
486 if attrib == 'freq':
--> 487 freq = self.freqstr
488 if freq is not None:
489 freq = "'%s'" % freq
C:\Users\ssmith\AppData\Local\Continuum\Anaconda2\lib\site-packages\pandas\tseries\base.pyc in freqstr(self)
223 if self.freq is None:
224 return None
--> 225 return self.freq.freqstr
226
227 @cache_readonly
AttributeError: 'str' object has no attribute 'freqstr'
问题
使索引假定为inferred_freq的合适方法是什么?
question
What is the appropriate way to have my index assume it's inferred_freq?
推荐答案
目前尚不清楚文档为何说明您可以设置freq
属性,但是该属性不能持久存在,但是如果您再次重构datetimeindex
但通过freq
param然后起作用:
It's unclear why the docs state you can set the freq
attribute but then it doesn't persist but if you reconstruct the datetimeindex
again but pass a freq
param then it works:
In [56]:
tidx = pd.DatetimeIndex(tidx.values, freq = tidx.inferred_freq)
tidx
Out[56]:
DatetimeIndex(['2016-07-29', '2016-08-31', '2016-09-30'], dtype='datetime64[ns]', freq='BM')
这篇关于使用inferred_freq设置pandas.tseries.index.DatetimeIndex.freq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!