Last updated: Apr 12, 2024
Reading time·4 min
Use the pandas.to_datetime
method to convert Epoch to datetime
in a Pandas
DataFrame
.
The method takes an optional unit
argument that can be set to s
(seconds).
import pandas as pd df = pd.DataFrame({ 'Name': ['Alice', 'Bobby', 'Carl'], 'Date': ['1688025596', '1687013326', '1689071143'] }) df['Date'] = pd.to_datetime(df['Date'], unit='s') # Name Date # 0 Alice 2023-06-29 08:00:00 # 1 Bobby 2023-06-17 14:49:36 # 2 Carl 2023-07-11 10:25:04 print(df)
The pandas.to_datetime() method
converts the supplied argument to a datetime
object.
The method can be used to convert a scalar, an array-like, a Series
or a
DataFrame
to a Pandas datetime
object.
We passed the following 2 parameters to the method:
datetime
.D
(days), s
(seconds), ms
(milliseconds), etc).We set the unit
argument to s
because the epoch time is stored in seconds.
You can use the
DataFrame.dtypes
attribute to verify that the epoch time has been converted to datetime
.
import pandas as pd df = pd.DataFrame({ 'Name': ['Alice', 'Bobby', 'Carl'], 'Date': ['1688025596', '1687013326', '1689071143'] }) df['Date'] = pd.to_datetime(df['Date'], unit='s') # Name Date # 0 Alice 2023-06-29 08:00:00 # 1 Bobby 2023-06-17 14:49:36 # 2 Carl 2023-07-11 10:25:04 print(df) print('-' * 50) # Name object # Date datetime64[ns] # dtype: object print(df.dtypes)
When running the code sample, you might get the following error:
We specified s
(seconds) as the unit
in the example above, however, your
timestamp might be stored in milliseconds.
1 second is equal to 1000 milliseconds, so the correct unit has to be specified
when calling pandas.to_datetime()
.
If your timestamps are stored in milliseconds, set the unit
argument to
"ms"
.
import pandas as pd df = pd.DataFrame({ 'Name': ['Alice', 'Bobby', 'Carl'], 'Date': ['1689056774584', '1689053172587', '1689023414345'] }) df['Date'] = pd.to_datetime(df['Date'], unit='ms') # Name Date # 0 Alice 2023-07-11 06:27:17.632 # 1 Bobby 2023-07-11 05:26:07.616 # 2 Carl 2023-07-10 21:10:14.272 print(df) print('-' * 50) # Name object # Date datetime64[ns] # dtype: object print(df.dtypes)
The timestamps in the example above are stored in milliseconds, so we set the
unit
argument to "ms"
when converting to datetime
.
You can also use the DataFrame.apply()
method to convert epoch to datetime
in a Pandas DataFrame
.
from datetime import datetime import pandas as pd df = pd.DataFrame({ 'Name': ['Alice', 'Bobby', 'Carl'], 'Date': ['1688025596', '1687013326', '1689071143'] }) df['Date'] = df['Date'].apply( lambda d: datetime.fromtimestamp(int(d)).strftime('%Y-%m-%d %H:%M:%S') ) # Name Date # 0 Alice 2023-06-29 10:59:56 # 1 Bobby 2023-06-17 17:48:46 # 2 Carl 2023-07-11 13:25:43 print(df)
The
DataFrame.apply()
method applies a function along an axis of the DataFrame
.
The lambda function we passed to apply
converts each timestamp to a datetime
object and formats it using strftime
.
Formatting the datetime
object to a string is optional, so you don't have to
call strftime()
.
from datetime import datetime import pandas as pd df = pd.DataFrame({ 'Name': ['Alice', 'Bobby', 'Carl'], 'Date': ['1688025596', '1687013326', '1689071143'] }) df['Date'] = df['Date'].apply( lambda d: datetime.fromtimestamp(int(d)) ) # Name Date # 0 Alice 2023-06-29 10:59:56 # 1 Bobby 2023-06-17 17:48:46 # 2 Carl 2023-07-11 13:25:43 print(df) print('-' * 50) # Name object # Date datetime64[ns] # dtype: object print(df.dtypes)
As shown in the code sample, the type of the Date
column is datetime64[ns]
.
You can also explicitly use the astype()
method to convert epoch to datetime
in a Pandas DataFrame
.
import pandas as pd df = pd.DataFrame({ 'Name': ['Alice', 'Bobby', 'Carl'], 'Date': ['1688025596', '1687013326', '1689071143'] }) df['Date'] = df['Date'].astype('int').astype('datetime64[s]') # Name Date # 0 Alice 2023-06-29 10:59:56 # 1 Bobby 2023-06-17 17:48:46 # 2 Carl 2023-07-11 13:25:43 print(df) print('-' * 50) # Name object # Date datetime64[s] # dtype: object print(df.dtypes)
We first used the astype()
method to convert the values in the Date
column
to integers and then used the method to convert the integers to datetime64[s]
objects.
You can learn more about the related topics by checking out the following tutorials: