2016-05-29 44 views
5

我有不同类型的DateTime索引(可能是每周,每月,每年的数据)的数据帧。我想要生成其他列的滞后值的列。我从电子表格中导入这些文件,我不在python中生成日期时间索引。延迟日期时间索引列的pythonic方式

我正在努力寻找这样做的'pythonic'方式。我想如果我使用Pandas的日期时间功能,在奇怪或异常数据的情况下,滞后可能更加稳健。

我做了一个玩具的例子,似乎工作,但它失败了我的现实世界的例子。

玩具例子正常工作(使有前一个月的“富”值的新列)

rng = pd.date_range('2012-01-01', '2013-1-01', freq="M") 
toy2 = pd.DataFrame(pd.Series(np.random.randint(0, 50, len(rng)), index=rng, name="foo")) 

      foo 
2012-01-31 4 
2012-02-29 2 
2012-03-31 27 
2012-04-30 7 
2012-05-31 44 
2012-06-30 22 
2012-07-31 16 
2012-08-31 18 
2012-09-30 35 
2012-10-31 35 
2012-11-30 16 
2012-12-31 32 

toy2['lag_foo']= toy2['foo'].shift(1,'m') 

    foo lag_foo 
2012-01-31 4 NaN 
2012-02-29 2 4.0 
2012-03-31 27 2.0 
2012-04-30 7 27.0 
2012-05-31 44 7.0 
2012-06-30 22 44.0 
2012-07-31 16 22.0 
2012-08-31 18 16.0 
2012-09-30 35 18.0 
2012-10-31 35 35.0 
2012-11-30 16 35.0 
2012-12-31 32 16.0 

但是,当我在我的现实生活中的例子运行它,它失败:

ValueError: cannot reindex from a duplicate axis

print type(toy) 
print toy.columns 
print toy['IPE m2'][0:5] 

<class 'pandas.core.frame.DataFrame'> 
Index([u'IPE m2'], dtype='object') 
Date 
2016-04-30 43.29 
2016-03-31 40.44 
2016-02-29 34.17 
2016-01-31 32.47 
2015-12-31 39.35 
Name: IPE m2, dtype: float64 

的异常跟踪:

ValueError        Traceback (most recent call last) 
<ipython-input-170-9cb57a2ed681> in <module>() 
----> 1 toy['prev_1m']= toy['IPE m2'].shift(1,'m') 

C:\Users\mds\Anaconda2\lib\site-packages\pandas\core\frame.pyc in __setitem__(self, key, value) 
    2355   else: 
    2356    # set column 
-> 2357    self._set_item(key, value) 
    2358 
    2359  def _setitem_slice(self, key, value): 

C:\Users\mds\Anaconda2\lib\site-packages\pandas\core\frame.pyc in _set_item(self, key, value) 
    2421 
    2422   self._ensure_valid_index(value) 
-> 2423   value = self._sanitize_column(key, value) 
    2424   NDFrame._set_item(self, key, value) 
    2425 

C:\Users\mds\Anaconda2\lib\site-packages\pandas\core\frame.pyc in _sanitize_column(self, key, value) 
    2555 
    2556   if isinstance(value, Series): 
-> 2557    value = reindexer(value) 
    2558 
    2559   elif isinstance(value, DataFrame): 

C:\Users\mds\Anaconda2\lib\site-packages\pandas\core\frame.pyc in reindexer(value) 
    2547      # duplicate axis 
    2548      if not value.index.is_unique: 
-> 2549       raise e 
    2550 
    2551      # other 

ValueError: cannot reindex from a duplicate axis 

似乎我错过了熊猫日期时间指数的一些微妙之处,我认为。另外我甚至不确定这是做这件事的理想方式。我怀疑的唯一的事情是,非工作toy.index具有无作为频率,而工作toy2例子,有其频率设置为“M”

toy.index 
DatetimeIndex(['2016-04-30', '2016-03-31', '2016-02-29', '2016-01-31', 
       '2015-12-31', '2015-11-30', '2015-10-31', '2015-09-30', 
       '2015-08-31', '2015-07-31', 
       ... 
         'NaT',  'NaT',  'NaT',  'NaT', 
         'NaT',  'NaT',  'NaT',  'NaT', 
         'NaT',  'NaT'], 
       dtype='datetime64[ns]', name=u'Date', length=142, freq=None) 


toy2.index 
DatetimeIndex(['2012-01-31', '2012-02-29', '2012-03-31', '2012-04-30', 
       '2012-05-31', '2012-06-30', '2012-07-31', '2012-08-31', 
       '2012-09-30', '2012-10-31', '2012-11-30', '2012-12-31'], 
       dtype='datetime64[ns]', freq='M') 
In [ ]: 

======== ===================

我扔掉了NAT的

toy = toy.dropna() 

toy['prev_1m']= toy['IPE m2'].shift(1,'m') 

和我得到我想要的结果。不过,我也得到一个警告:

C:\Users\mds\Anaconda2\lib\site-packages\ipykernel\__main__.py:1: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame. 
Try using .loc[row_indexer,col_indexer] = value instead 

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy 
    if __name__ == '__main__': 

====

分配的这种方式抑制了警告:

toy.loc[:,'prev_1m2']= toy['IPE m2'].shift(1,'m') 

回答

2

还有一个问题 - 在许多指标在NaTtoyDataFrame ,因此index具有重复的值。 (也许有些日期时间被复制了。)

样品:

import pandas as pd 
import numpy as np 

rng = pd.date_range('2012-01-01', '2013-1-01', freq="M") 
toy2 = pd.DataFrame(pd.Series(np.random.randint(0, 50, len(rng)), index=rng, name="foo")) 

df = pd.DataFrame({'foo': [10,30,19]}, index=[np.nan, np.nan, np.nan]) 
print (df) 
    foo 
NaN 10 
NaN 30 
NaN 19 

toy2 = pd.concat([toy2, df]) 
print (toy2) 
      foo 
2012-01-31 18 
2012-02-29 34 
2012-03-31 43 
2012-04-30 17 
2012-05-31 45 
2012-06-30 8 
2012-07-31 36 
2012-08-31 26 
2012-09-30 5 
2012-10-31 18 
2012-11-30 39 
2012-12-31 3 
NaT   10 
NaT   30 
NaT   19 

toy2['lag_foo']= toy2['foo'].shift(1,'m') 
print (toy2) 

ValueError: cannot reindex from a duplicate axis

一个可能的解决方案,可以省略参数freq=m

toy2['lag_foo']= toy2['foo'].shift(1) 
print (toy2) 
      foo lag_foo 
2012-01-31 21  NaN 
2012-02-29 13  21.0 
2012-03-31 41  13.0 
2012-04-30 38  41.0 
2012-05-31 15  38.0 
2012-06-30 41  15.0 
2012-07-31 30  41.0 
2012-08-31 18  30.0 
2012-09-30 12  18.0 
2012-10-31 35  12.0 
2012-11-30 23  35.0 
2012-12-31 7  23.0 
NaT   10  7.0 
NaT   30  10.0 
NaT   19  30.0 

如果需要删除所有记录与NaNNaT )在index,使用notnullboolean indexing

print (toy2) 
      foo 
2012-01-31 41 
2012-02-29 15 
2012-03-31 8 
2012-04-30 2 
2012-05-31 16 
2012-06-30 43 
2012-07-31 2 
2012-08-31 15 
2012-09-30 3 
2012-10-31 46 
2012-11-30 34 
2012-12-31 36 
NaT   10 
NaT   30 
NaT   19 

toy2 = toy2[pd.notnull(toy2.index)] 

toy2['lag_foo']= toy2['foo'].shift(1, 'm') 
print (toy2) 
      foo lag_foo 
2012-01-31 41  NaN 
2012-02-29 15  41.0 
2012-03-31 8  15.0 
2012-04-30 2  8.0 
2012-05-31 16  2.0 
2012-06-30 43  16.0 
2012-07-31 2  43.0 
2012-08-31 15  2.0 
2012-09-30 3  15.0 
2012-10-31 46  3.0 
2012-11-30 34  46.0 
2012-12-31 36  34.0 
+0

我用dropna()抛出NaTs,它的工作原理,但它给出了一些警告。添加了原始问题的附录。 – user3556757

+1

你确定你需要'dropna'吗?如果需要在索引中用'NaN'删除记录,使用'toy = toy [ pd.notnull(toy.index)]'。 – jezrael