简体   繁体   English

移动没有频率的时间序列?

[英]shifting a time series without frequency?

I have a time series whose indices looks like this: 我有一个时间序列,其索引如下所示:

In [671]: indices
Out[671]: 

DatetimeIndex(['2000-12-29', '2001-02-20', '2001-03-26', '2001-04-12',
               '2001-04-24', '2001-07-05', '2001-08-15', '2001-09-10',
               '2001-09-18', '2001-10-02', '2001-10-11', '2001-10-30',
               '2001-12-13', '2002-03-07', '2002-06-13', '2002-09-12',
               '2002-12-12', '2003-03-13', '2003-06-12', '2013-02-19',
               '2013-05-28', '2013-09-03', '2014-01-21', '2014-02-18',
               '2014-05-27', '2014-07-07', '2014-09-02', '2015-01-20',
               '2015-02-17', '2015-05-26', '2015-07-06', '2016-05-31',
               '2016-07-05', '2016-09-06', '2016-10-04', '2017-01-17',
               '2017-02-21', '2017-05-30', '2017-09-05'],
              dtype='datetime64[ns]', name='date', freq=None)

I can not assign a frequency since frequency is irregular. 由于频率不规则,我无法分配频率。

My goal is to get a new set of indices that are shifted by 2 rows (not 2 calendar dates later but two dates later in the data). 我的目标是获取一组新的索引,这些索引将移动2行(不是数据后的2个日历日期,而是数据的后两个日期)。

I try: 我尝试:

   indices2= indices.shift(2)

But it says: 但它说:

ValueError: Cannot shift with no freq

My desired output looks like: 我想要的输出看起来像:

In [671]: indices2
Out[671]: 

    DatetimeIndex(['2000-02-20', '2001-03-26', '2001-04-12', ...., '2017-09-05'],

This works if you load it into a pd.Series object first, and then shift - 如果pd.Series其加载到pd.Series对象中, 然后 shift -

pd.Series(i).shift(-1).head()

0   2001-02-20
1   2001-03-26
2   2001-04-12
3   2001-04-24
4   2001-07-05
Name: date, dtype: datetime64[ns]

The actual result contains NaNs, which you can remove using dropna . 实际结果包含NaN,您可以使用dropna将其删除。

pd.DatetimeIndex(pd.Series(i).shift(-1).dropna())

DatetimeIndex(['2001-02-20', '2001-03-26', '2001-04-12', '2001-04-24',
               '2001-07-05', '2001-08-15', '2001-09-10', '2001-09-18',
               '2001-10-02', '2001-10-11', '2001-10-30', '2001-12-13',
               '2002-03-07', '2002-06-13', '2002-09-12', '2002-12-12',
               '2003-03-13', '2003-06-12', '2013-02-19', '2013-05-28',
               '2013-09-03', '2014-01-21', '2014-02-18', '2014-05-27',
               '2014-07-07', '2014-09-02', '2015-01-20', '2015-02-17',
               '2015-05-26', '2015-07-06', '2016-05-31', '2016-07-05',
               '2016-09-06', '2016-10-04', '2017-01-17', '2017-02-21',
               '2017-05-30', '2017-09-05'],
              dtype='datetime64[ns]', name='date', freq=None)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM