使用问题pd.T系列偏移量.CustomBusinessDay从DatetimeIndex查找日期

2024-10-02 10:20:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个pandas系列(索引是日期),我试图从这个系列中定期(365天)提取日期。为了简单起见,我使用了CustomBusinessDay。这似乎工作得很好,但对于一个特定的例子,它失败了,这意味着我可能不明白它到底是如何工作的。你知道吗

我就是这么做的:

我有一系列的“测试”:

test
Out[29]: 
2008-04-02    0.006084
2008-04-03   -0.002923
2008-04-04    0.002602

等等。。你知道吗

更具体地说,问题出现在这里:

test[512:516]
Out[31]: 
2010-03-30   -0.002650
2010-03-31   -0.002898
2010-04-01    0.016151
2010-04-06    0.003690
dtype: float64

我使用原始索引创建了一个自定义的工作日,并创建了一个频率为365D的日期范围,并添加0*CustCal,以便获得作为原始索引一部分的日期:

CustCal=pd.tseries.offsets.CustomBusinessDay(calendar=test.index)
tempRestrikeDates = pd.date_range(start=test.index[0], end=test.index[-1], freq='365D')+0*CustCal

但是,正如您在下面的结果中看到的,'2010-04-02'是tempRestrikeDates的一部分,但不在测试的原始索引中。你知道吗

tempRestrikeDates
Out[35]: 
DatetimeIndex(['2008-04-02', '2009-04-02', '2010-04-02', '2011-04-04',
               '2012-04-02', '2013-04-01', '2014-04-01', '2015-04-01',
               '2016-03-31', '2017-03-31', '2018-04-02', '2019-04-01'],
              dtype='datetime64[ns]', freq=None)

在使用CustomBusinessDay和添加0*CustCal以获得作为CustCal一部分的日期的方法中,是否有什么错误?你知道吗

有没有其他有效的方法来实现这一点?你知道吗

结果应该是:

tempRestrikeDates
Out[35]: 
DatetimeIndex(['2008-04-02', '2009-04-02', '2010-04-01', '2011-04-04',
               '2012-04-02', '2013-04-01', '2014-04-01', '2015-04-01',
               '2016-03-31', '2017-03-31', '2018-04-02', '2019-04-01'],
              dtype='datetime64[ns]', freq=None)

Tags: 方法testnonepandasindexoutpdns

热门问题