pd.to_datetime 无法转换 unix 时间戳:值错误:不可转换值 以"ms"为单位的开放时间



以下是数据帧:

0.         1514764800000
1.         1514765100000
2.         1514765400000
3.         1514765700000
4.         1514766000000
...      
105286.    1577835300000
105287.    1577835600000
105288.    1577835900000
105289.    1577836200000
105290.    1577836500000
Name: Open Time, Length: 232102, dtype: object

我得到的错误是:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
pandas/_libs/tslib.pyx in pandas._libs.tslib.array_with_unit_to_datetime()
ValueError: could not convert string to float: 'Open Time'
During handling of the above exception, another exception occurred:
ValueError                                Traceback (most recent call last)
<ipython-input-16-e715d250d2ce> in <module>()
17 
18 print("Loading : ")
---> 19 btc_data = load_data(['2018', '2019'], 'BTCUSDT', continuous_columns, '5m')
20 btc_test_data = load_data(['2020'], 'BTCUSDT', continuous_columns, interval = '5m')

/content/Temporal-Fusion-Transformer/data.py in load_data(year, symbol, con_cols, interval, normalise)
31     print("done")
32     #convert timestamp into month and day numbers
---> 33     data_['Hour'] = pd.to_datetime(data_["Open Time"],origin='unix', unit='ms').apply(lambda x: x.hour)
34     data_['Day'] = pd.to_datetime(data_["Open Time"],origin='unix', unit='ms').apply(lambda x: x.day - 1)
35     data_['Month'] = pd.to_datetime(data_["Open Time"],origin='unix', unit='ms').apply(lambda x: x.month - 1)
/usr/local/lib/python3.7/dist-packages/pandas/core/tools/datetimes.py in to_datetime(arg, errors, dayfirst, yearfirst, utc, format, exact, unit, infer_datetime_format, origin, cache)
801             result = arg.map(cache_array)
802         else:
--> 803             values = convert_listlike(arg._values, format)
804             result = arg._constructor(values, index=arg.index, name=arg.name)
805     elif isinstance(arg, (ABCDataFrame, abc.MutableMapping)):
/usr/local/lib/python3.7/dist-packages/pandas/core/tools/datetimes.py in _convert_listlike_datetimes(arg, format, name, tz, unit, errors, infer_datetime_format, dayfirst, yearfirst, exact)
338 
339             result, tz_parsed = tslib.array_with_unit_to_datetime(
--> 340                 arg, unit, errors=errors
341             )
342 
pandas/_libs/tslib.pyx in pandas._libs.tslib.array_with_unit_to_datetime()
ValueError: non convertible value Open Time with the unit 'ms'

如错误所示:

ValueError:不可转换值打开时间,单位为"ms">

您有一个字符串"打开时间";正如@АлексейР所评论的,在您的数据中,这导致了问题。

最新更新