pd.to_datetime can't convert unix timestamp : ValueError: non convertible value Open Time with the unit 'ms'

Viewed 841

Here's the dataframe:

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

And the error I'm getting is:

---------------------------------------------------------------------------
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'
0 Answers
Related