熊猫和时间序列

时间:2016-07-26 15:46:16

标签: pandas time-series

我有一个数据帧字典。我想将其中的每个数据帧转换为各自的时间序列。我能很好地转换一个。但是,如果我在迭代器中执行它,它会抱怨。例如:

这有效:

df = dfDict[4]
df['start_date'] = pd.to_datetime(df['start_date'])
df.set_index('start_date', inplace = True)
df.sort_index(inplace = True)

print df.head()效果很好。

但是,这不起作用:

tsDict = {}
for id, df in dfDict.iteritems():
    df['start_date'] = pd.to_datetime(df['start_date'])
    df.set_index('start_date', inplace = True)
    df.sort_index(inplace = True)
    tsDict[id] = df

它给出以下错误消息:

Traceback (most recent call last):
  File "tsa.py", line 105, in <module>
    main()
  File "tsa.py", line 84, in main
    df['start_date'] = pd.to_datetime(df['start_date'])
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 1997, in __getitem__
    return self._getitem_column(key)
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 2004, in _getitem_column
    return self._get_item_cache(key)
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/generic.py", line 1350, in _get_item_cache
    values = self._data.get(item)
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/internals.py", line 3290, in get
    loc = self.items.get_loc(item)
  File "/usr/local/lib/python2.7/dist-packages/pandas/indexes/base.py", line 1947, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas/index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas/index.c:4154)
  File "pandas/index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas/index.c:4018)
  File "pandas/hashtable.pyx", line 675, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12368)
  File "pandas/hashtable.pyx", line 683, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12322)
KeyError: 'start_date'

我无法在这里看到微妙的问题......

0 个答案:

没有答案