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

标签 python python-3.x datetime strptime

我有一列数据,其日期格式为“%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带参数格式:

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]

关于python - key 错误 : 0 when changing time format of data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44651950/

相关文章:

sql - 在SQL Server 2008中将NVARCHAR转换为DATETIME

python - 导入时出现 UnicodeDecodeError (tweepy)

python - 我的代码不会检查 X、Y 和 Z 是否在列表中,但如果仅覆盖其中之一,则会进行检查。我究竟做错了什么?

python - 子进程在完成之前终止 PowerShell 脚本

php - 如何在php中获取给定日期范围内月份的开始日期和结束日期

java - Joda 在 GMT 时区解析 ISO8601 日期

python - celery - 类型错误 : Object of type bytes is not JSON serializable

python - python3创建scrapy项目的方法

python - 如何快速抽取一个 numpy 数组?

python - 如何管理多层字典,自定义列,减去列数据,添加新列?