gpt4 book ai didi

python - 使用 inferred_freq 设置 pandas.tseries.index.DatetimeIndex.freq

转载 作者:太空狗 更新时间:2023-10-30 01:57:24 25 4
gpt4 key购买 nike

考虑 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 属性...所以我

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 的适当方法是什么?

最佳答案

目前还不清楚为什么文档声明您可以设置 freq 属性,但它不会持续存在,但如果您再次重建 datetimeindex 但传递一个 freq param 然后它起作用:

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')

关于python - 使用 inferred_freq 设置 pandas.tseries.index.DatetimeIndex.freq,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40222583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com