Table of Contents
The process of converting epoch values to datetime in Pandas involves using the built-in function “to_datetime” to convert the epoch values, which represent the number of seconds since January 1st, 1970, to a more readable date and time format. This can be useful for analyzing and visualizing time series data in a more intuitive manner.
Pandas: 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
print(df)
date sales
0 1655439422 120
1 1655638422 150
2 1664799422 224
3 1668439411 290
4 1669939422 340
5 1669993948 184Currently 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 DataFrameprint(df)
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 .
The following tutorials explain how to perform other common tasks in pandas:
Cite this article
stats writer (2024). How can I convert epoch values to datetime in Pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-convert-epoch-values-to-datetime-in-pandas/
stats writer. "How can I convert epoch values to datetime in Pandas?." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-convert-epoch-values-to-datetime-in-pandas/.
stats writer. "How can I convert epoch values to datetime in Pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-convert-epoch-values-to-datetime-in-pandas/.
stats writer (2024) 'How can I convert epoch values to datetime in Pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-convert-epoch-values-to-datetime-in-pandas/.
[1] stats writer, "How can I convert epoch values to datetime in Pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I convert epoch values to datetime in Pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
