-1

I'm working with some CCXT data and I have converted the Timestamp to Datetime by using:

df['Timestamp'] = (pd.to_datetime(df['Timestamp'],unit='ms'))

Now I'm trying to turn that conversion into a function, modeled after this one:

def datetime_parse_timestamp (time_in_secs):    
    return datetime.datetime.fromtimestamp(float(time_in_secs))

So it can be called as

df = pd.read_csv('binance-BTCUSDT-4h.csv', parse_dates=True, date_parser=datetime_parse_timestamp, index_col='Timestamp')

What's the best way to go about this?

gosuto
  • 5,422
  • 6
  • 36
  • 57
Haytorade
  • 155
  • 1
  • 14

2 Answers2

0

Probably need to do something like:

def datetime_parse_timestamp(time_in_secs):
   return pd.to_datetime(time_in_secs,unit='ms')

Credit: You

Eric Carmichael
  • 560
  • 4
  • 15
0

If you're using the timestamp as a difference from current time you can use datetime.timedelta as

>>> import time
>>> import datetime

>>> a=1549032022
>>> str(datetime.timedelta(seconds=time.time()-a))
'4 days, 15:20:38.691180'

otherwise you already have the answer and if you want any modification in your answer you can use lambda function as per your ease.

Akshay Nailwal
  • 196
  • 1
  • 9