Pandas: Using Unix epoch timestamp as Datetime index
Convert them to datetime64[s]
:
np.array([1368431149, 1368431150]).astype('datetime64[s]')# array([2013-05-13 07:45:49, 2013-05-13 07:45:50], dtype=datetime64[s])
You can also use pandas to_datetime:
df['datetime'] = pd.to_datetime(df["timestamp"], unit='s')
This method requires Pandas 0.18 or later.
You can also use Pandas DatetimeIndex like so
pd.DatetimeIndex(df['timestamp']*10**9)
the *10**9
puts it into the format it's expecting for such timestamps.
This is nice since it allows you to use functions such as .date()
or .tz_localize()
on the series.