pandas unix timestamp to datetime code example
Example 1: pd.to_datetime python
import pandas as pd
date='2020/11/26 12:00:00'
date_time=pd.to_datetime(date, format='%Y/%m/%d %H:%M:%S')
Example 2: pandas change dtype to timestamp
pd.to_datetime(df.column)
Example 3: convert column to timestamp pandas
df2 = pd.to_datetime(df['col1'])
df2
Example 4: convert unix timestamp to datetime python pandas
In [23]: df.head()
Out[23]:
date price
0 1349720105 12.08
1 1349806505 12.35
2 1349892905 12.15
3 1349979305 12.19
4 1350065705 12.15
In [25]: df['date'] = pd.to_datetime(df['date'],unit='s')
In [26]: df.head()
Out[26]:
date price
0 2012-10-08 18:15:05 12.08
1 2012-10-09 18:15:05 12.35
2 2012-10-10 18:15:05 12.15
3 2012-10-11 18:15:05 12.19
4 2012-10-12 18:15:05 12.15
In [27]: df.dtypes
Out[27]:
date datetime64[ns]
price float64
dtype: object
Example 5: pandas datetime to unix timestamp
dates = pd.to_datetime(['2019-01-15 13:30:00'])
(dates - pd.Timestamp("1970-01-01"))
# Int64Index([1547559000], dtype='int64')