【问题标题】:Python pandas producing error when trying to access 'DATE' column on large data setPython pandas 在尝试访问大型数据集上的“日期”列时产生错误
【发布时间】:2016-08-09 09:12:51
【问题描述】:

我有一个包含 3'502'379 行和 3 列的文件。以下脚本应该被执行,但在日期处理行中引发和错误:

import matplotlib.pyplot as plt
import numpy as np
import csv
import pandas

path = 'data_prices.csv'
data = pandas.read_csv(path, sep=';')
data['DATE'] = pandas.to_datetime(data['DATE'], format='%Y%m%d')

这是发生的错误:

Traceback (most recent call last):
  File "C:\Program Files\Python35\lib\site-packages\pandas\indexes\base.py", line 1945, in get_loc
    return self._engine.get_loc(key)
  File "pandas\index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas\index.c:4066)
  File "pandas\index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas\index.c:3930)
  File "pandas\hashtable.pyx", line 675, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12408)
  File "pandas\hashtable.pyx", line 683, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12359)
KeyError: 'DATE'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\data\script.py", line 15, in <module>
    data['DATE'] = pandas.to_datetime(data['DATE'], format='%Y%m%d')
  File "C:\Program Files\Python35\lib\site-packages\pandas\core\frame.py", line 1997, in __getitem__
    return self._getitem_column(key)
  File "C:\Program Files\Python35\lib\site-packages\pandas\core\frame.py", line 2004, in _getitem_column
    return self._get_item_cache(key)
  File "C:\Program Files\Python35\lib\site-packages\pandas\core\generic.py", line 1350, in _get_item_cache
    values = self._data.get(item)
  File "C:\Program Files\Python35\lib\site-packages\pandas\core\internals.py", line 3290, in get
    loc = self.items.get_loc(item)
  File "C:\Program Files\Python35\lib\site-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:4066)
  File "pandas\index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas\index.c:3930)
  File "pandas\hashtable.pyx", line 675, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12408)
  File "pandas\hashtable.pyx", line 683, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12359)
KeyError: 'DATE'

【问题讨论】:

标签: python date datetime pandas dataframe


【解决方案1】:

第一列名称中的'\ufeffDATE' 表明您的CSV 文件有一个UTF-16 Byte Order Mark (BOM) signature,因此必须相应地读取它。

所以在阅读您的 CSV 时试试这个:

df = pd.read_csv(path, sep=';', encoding='utf-8-sig')

@EdChum suggested:

df = pd.read_csv(path, sep=';', encoding='utf-16')

两种变体都应该正常工作

PS this answer 显示如何处理 BOM

【讨论】:

  • 错了,这是 utf-16 大端:en.wikipedia.org/wiki/… FE FF 是 utf-16 大端
  • @EdChum,我已经确认了我的答案。实际上这两种变体都可以正常工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
  • 2016-09-10
  • 1970-01-01
  • 2020-05-08
  • 2021-12-11
  • 2015-10-11
  • 2022-11-09
相关资源
最近更新 更多