Pandas: How to Convert Epoch to Datetime

You can use the following basic syntax to convert epoch time to a recognizable datetime in pandas:

df['date_column'] = pd.to_datetime(df['date_column'], unit='s')

For example, this syntax will convert an epoch time of 1655439422 to a pandas datetime of 2022-06-17 04:17:02.

This format is more recognizable as a date and a time rather than a long string of numbers.

The following example shows how to use this syntax in practice.

Example: Convert Epoch to Datetime in Pandas

Suppose we have the following pandas Dataframe that contains information about the total sales of some product on specific dates and times:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'date': ['1655439422', '1655638422', '1664799422',
                            '1668439411', '1669939422', '1669993948'],
                   'sales': [120, 150, 224, 290, 340, 184]})

#view DataFrame

         date  sales
0  1655439422    120
1  1655638422    150
2  1664799422    224
3  1668439411    290
4  1669939422    340
5  1669993948    184

Currently the values in the date column are formatted as epoch times.

To convert the times from epoch to a pandas datetime format, we can use the following syntax:

#convert values in date column from epoch to datetime
df['date'] = pd.to_datetime(df['date'], unit='s')

#view updated DataFrame

                 date  sales
0 2022-06-17 04:17:02    120
1 2022-06-19 11:33:42    150
2 2022-10-03 12:17:02    224
3 2022-11-14 15:23:31    290
4 2022-12-02 00:03:42    340
5 2022-12-02 15:12:28    184

Notice that the values in the date column are now recognizable dates and times.

Note that most epoch times are stored as the number of seconds since 1/1/1970.

By using the argument unit=’s’ in the to_datetime() function, we’re explicitly telling pandas to convert the epoch to a datetime by calculating the number of seconds since 1/1/1970.

Note: You can find the complete documentation for the pandas to_datetime() function here.

Additional Resources

The following tutorials explain how to perform other common tasks in pandas:

How to Convert Timestamp to Datetime in Pandas
How to Convert Datetime to Date in Pandas
How to Convert DateTime to String in Pandas

Leave a Reply

Your email address will not be published. Required fields are marked *