Skip to main content
 首页 » 编程设计

python之key 错误 : 0 when changing time format of data

2024年10月01日9webabcd

我有一列日期格式为“%d%m%Y”的数据,例如“15022016”。 我需要将它们转换为“%Y-%m-%d”,例如“2016-02-15”。

数据框有911,462行,代码如下:

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中查看了原始数据,都没有问题,原始数据应该没有问题。 Key Error 为 0 很奇怪。我完全不知道它出了什么问题以及如何处理它。

感谢阅读,期待您的帮助! :)

请您参考如下方法:

你需要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]