Search code examples
pythonpandasfunctiondatetimeccxt

Changing Timestamp conversion to a Function


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?


Solution

  • Probably need to do something like:

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

    Credit: You