0

I get a list of data as follows. and I convert the timestamp to my own clock time as below but it gives an error. How can I do this in a list?

 1617009300000
 1617009300000
 1617009300000

code

 candles_df = pd.DataFrame(klines, columns=['T', 'open', 'high', 'low', 'close'])
 candles_df['T'] = datetime.fromtimestamp(int(candles_df['T'])/1000)
 candles_df['T'] = datetime.fromtimestamp(int(candles_df['T'])/1000)
 print(candles_df['T'])

result

 raise TypeError(f"cannot convert the series to {converter}")
 TypeError: cannot convert the series to <class 'int'>
johnvayne
  • 79
  • 3
  • 8
  • Try `candles_df['T']).astype(int)/1000` – ThePyGuy Mar 29 '21 at 10:08
  • if you work with `pandas`, make sure to use the pandas built-ins, as @NagaKiran shows in his answer. Also note that Python's `fromtimestamp` will give you *local time* if you don't specify a time zone. If you need UTC, you can use something like [this](https://stackoverflow.com/a/65076703/10197418). – FObersteiner Mar 29 '21 at 10:21

1 Answers1

0

use format of seconds unit=s with pandas to_datetime it would work for your requirement i guess.

import pandas as pd
pd.to_datetime(pd.Series(a).div(1000), unit='s')
# MrFuppes inputs
pd.to_datetime(pd.Series(a), unit='ms', utc=True).dt.tz_convert('Europe/Moscow')

Out:

0   2021-03-29 09:15:00
1   2021-03-29 09:15:00
2   2021-03-29 09:15:00
dtype: datetime64[ns]
Naga kiran
  • 4,528
  • 1
  • 17
  • 31