Convert Epoch to Datetime in a Pandas DataFrame [4 Ways]

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
4 min

banner

# Table of Contents

  1. Convert Epoch to Datetime in a Pandas DataFrame
  2. OutOfBoundsDatetime: cannot convert input X with the unit 's', at position 0
  3. Convert Epoch to Datetime in a Pandas DataFrame using apply()
  4. Convert Epoch to Datetime in a Pandas DataFrame using astype()

# Convert Epoch to Datetime in a Pandas DataFrame

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).

main.py
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)

convert epoch to datetime in pandas dataframe

The code for this article is available on GitHub

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:

  1. The object we want to convert to datetime.
  2. The unit of the supplied argument (D (days), s (seconds), ms (milliseconds), etc).
The Unix epoch time is the number of seconds that have elapsed since the 1st of January 1970 at midnight UTC.

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.

main.py
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)

verify epoch converted to datetime

The code for this article is available on GitHub

# OutOfBoundsDatetime: cannot convert input X with the unit 's', at position 0

When running the code sample, you might get the following error:

  • pandas._libs.tslibs.np_datetime.OutOfBoundsDatetime: cannot convert input X with the unit 's', at position 0

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".

main.py
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)

convert epoch stored in milliseconds to datetime

The code for this article is available on GitHub

The timestamps in the example above are stored in milliseconds, so we set the unit argument to "ms" when converting to datetime.

# Convert Epoch to Datetime in a Pandas DataFrame using apply()

You can also use the DataFrame.apply() method to convert epoch to datetime in a Pandas DataFrame.

main.py
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 code for this article is available on GitHub

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().

main.py
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].

# Convert Epoch to Datetime in a Pandas DataFrame using astype()

You can also explicitly use the astype() method to convert epoch to datetime in a Pandas DataFrame.

main.py
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)
The code for this article is available on GitHub

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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.