2017-06-20 56 views
2

我有一列數據的日期格式爲「%d%m%Y」,如「15022016」。 我需要將它們轉換爲「%Y-%m-%d」,如「2016-02-15」。KeyError:當更改數據的時間格式時爲0

數據幀有911462行,並且代碼如下:

for i in range(0,911462): 
    df['Date'][i]=datetime.datetime.strftime(datetime.datetime.strptime(df['Date'][i],"%d%m%Y"),"%Y-%m-%d") 

然後我遇到錯誤如下:

Traceback (most recent call last): 
    File "C:\Users\liangfan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\indexes\base.py", line 2393, in get_loc 
    return self._engine.get_loc(key) 
    File "pandas\_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5239) 
    File "pandas\_libs\index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5085) 
    File "pandas\_libs\hashtable_class_helper.pxi", line 1207, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20405) 
    File "pandas\_libs\hashtable_class_helper.pxi", line 1215, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20359) 
KeyError: 0 
During handling of the above exception, another exception occurred: 
Traceback (most recent call last): 
    File "<input>", line 2, in <module> 
    File "C:\Users\liangfan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\frame.py", line 2062, in __getitem__ 
    return self._getitem_column(key) 
    File "C:\Users\liangfan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\frame.py", line 2074, in _getitem_column 
    result = result[key] 
    File "C:\Users\liangfan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\frame.py", line 2062, in __getitem__ 
    return self._getitem_column(key) 
    File "C:\Users\liangfan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\frame.py", line 2069, in _getitem_column 
    return self._get_item_cache(key) 
    File "C:\Users\liangfan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\generic.py", line 1534, in _get_item_cache 
    values = self._data.get(item) 
    File "C:\Users\liangfan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\internals.py", line 3590, in get 
    loc = self.items.get_loc(item) 
    File "C:\Users\liangfan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\pandas\core\indexes\base.py", line 2395, in get_loc 
    return self._engine.get_loc(self._maybe_cast_indexer(key)) 
    File "pandas\_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5239) 
    File "pandas\_libs\index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc (pandas\_libs\index.c:5085) 
    File "pandas\_libs\hashtable_class_helper.pxi", line 1207, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20405) 
    File "pandas\_libs\hashtable_class_helper.pxi", line 1215, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas\_libs\hashtable.c:20359) 
KeyError: 0 

予檢查的原始數據在Excel中,它們是一切都好,所以原始數據應該沒有問題。 關鍵錯誤是0非常有線。我完全不知道它有什麼問題以及如何處理它。

感謝您閱讀並等待您的幫助! :)

回答

2

您需要pandas.to_datetime與參數format

df = pd.DataFrame({'Date':[15022016,15022016]}) 
print (df) 
     Date 
0 15022016 
1 15022016 

df['Date'] = pd.to_datetime(df['Date'], format='%d%m%Y') 
print (df) 
     Date 
0 2016-02-15 
1 2016-02-15 

print (df['Date'].dtype) 
datetime64[ns] 
+0

太謝謝你了!但我仍然想知道爲什麼會出現以前的錯誤,只是好奇而已。 –

+0

不確定,但似乎沒有索引值0。 – jezrael