How to convert Today to Date Time in Python

I have a dataframe with columns, from calling the api.

The first column is Today. Its values are: 1608181200, 1608094800, etc.

How do I convert that to a Date Time that I can understand? I read that it is in ISO format but that dos not look like Wikipedia’s example.

Thanks.

Those values are ‘epoch’ times and usually represent seconds. The ubiquitous Pandas to_datetime method can help getting that into a datetime value. That method expects epoch times to be in nanoseconds by default. Most of the epoch time values are in UTC milliseconds or seconds. It appears the values here are seconds. So, specify the unit to be s. Otherwise, specify it to be ms. Something like this

import pandas as pd

my_epoch_time = 1608094800
my_datetime = pd.to_datetime(my_epoch_time, unit='s', utc=True)

That will return

Timestamp(‘2020-12-16 05:00:00+0000’, tz=‘UTC’)`

If you have an entire column to convert, it’s very similar. Just pass the whole column. Here we assume the dataframe column with epoch times is named ‘Today’

data_df['today_as_datetime'] = pd.to_datetime(data_df.Today, unit='s', utc=True)

All that said, several of the Alpaca functions include a df method. You can simply append that method which will then return a dataframe with a datetime index (automatically converting that ‘weird’ epoch time). Something like this

data_df = api.polygon.historic_agg_v2(symbol='AA', 
                                   multiplier=1, timespan='day', 
                                   _from='2020-12-14', to='2020-12-17').df

Notice the .df method at the end. This will create a dataframe with the epoch times already converted. Not all functions support that .df method but generally it’s worth a try.

That worked perfectly! Thanks.