本文介绍了转移没有频率的时间序列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个时间序列,其索引如下:
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.
我的目标是获取一组新的索引,这些索引将偏移2行(不是数据中的2个日历日期,而是数据中的2个日期).
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).
我尝试:
indices2= indices.shift(2)
但是它说:
ValueError: Cannot shift with no freq
我想要的输出如下:
In [671]: indices2
Out[671]:
DatetimeIndex(['2000-02-20', '2001-03-26', '2001-04-12', ...., '2017-09-05'],
推荐答案
如果先将其加载到pd.Series
对象中,然后先然后 shift
-
This works if you load it into a pd.Series
object first, and then 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]
实际结果包含NaN,您可以使用dropna
将其删除.
The actual result contains NaNs, which you can remove using 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)
这篇关于转移没有频率的时间序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!