Python Pandas: Почему я не могу преобразовать 'Time' в to_datetime? Не узнает время - PullRequest
1 голос
/ 25 апреля 2020

Данные о времени выглядят следующим образом: Время

 20:15:00.0

 20:16:00.0

 20:17:00.0

 20:18:00.0

 20:19:00.0

 20:20:00.0

 20:21:00.0

 20:22:00.0

 20:23:00.0

 20:24:00.0

данные: https://imgur.com/a/LQIjHGt)


Python распознает их как:

Date             object

 **Time**            **object**

 Open           float64

 High           float64

 Low            float64

 Last           float64

Я пытался импортировать такие данные: час


df = pd.read_csv('ES_1min_2012_vwap_va.txt', sep=",", nrows=1000, parse_dates=True);

df['Time'] = pd.to_datetime(df['Time'])

**ERROR**: 

runfile('C:/Users/user/Desktop/Trading/Main/historical data/Index/ES/Intraday Volatility by VIX.py', wdir='C:/Users/user/Desktop/Trading/Main/historical data/Index/ES')
Traceback (most recent call last):

  File "C:\Users\user\miniconda3\lib\site-packages\pandas\core\indexes\base.py", line 2646, in get_loc
    return self._engine.get_loc(key)

  File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc

  File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc

  File "pandas\_libs\hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item

  File "pandas\_libs\hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item

KeyError: 'Time'


During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File "C:\Users\user\Desktop\Trading\Main\historical data\Index\ES\Intraday Volatility by VIX.py", line 18, in <module>
    df['Time'] = pd.to_datetime(df['Time'], errors='ignore')

  File "C:\Users\user\miniconda3\lib\site-packages\pandas\core\frame.py", line 2800, in __getitem__
    indexer = self.columns.get_loc(key)

  File "C:\Users\user\miniconda3\lib\site-packages\pandas\core\indexes\base.py", line 2648, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))

  File "pandas\_libs\index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc

  File "pandas\_libs\index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc

  File "pandas\_libs\hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item

  File "pandas\_libs\hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item

KeyError: 'Time'

Устранена эта ошибка за месяц go но совсем забыл, помогите пожалуйста

1 Ответ

2 голосов
/ 25 апреля 2020

Я думаю, что перед "Time" есть пробел, вы можете использовать skipinitialspace=True:

df = pd.read_csv('test.csv', sep=',', nrows=1000, parse_dates=True, skipinitialspace=True)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...