How can I convert an index to a datetime in Pandas?

How can I convert an index to a datetime in Pandas?

Converting an index to a datetime in Pandas is a simple process that allows for easy manipulation and analysis of time-series data. This can be achieved by using the “to_datetime” function, which converts the index values to a datetime format. This function also allows for customization of the date and time format, making it a versatile tool for handling different types of data. With the converted datetime index, tasks such as selecting specific time periods or resampling the data can be easily performed, making it a valuable tool for data analysis.

Pandas: Convert Index to Datetime


You can use the following syntax to convert an index column of a pandas DataFrame to a datetime format:

df.index = pd.to_datetime(df.index)

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

Example: Convert Index Column to Datetime in Pandas

Suppose we have the following pandas DataFrame that contains information about product sales at some store:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'time': ['4-15-2022 10:15', '5-19-2022 7:14', '8-01-2022 1:14',
                            '6-14-2022 9:45', '10-24-2022 2:58', '12-13-2022 11:03'],
                   'product': ['A', 'B', 'C', 'D', 'E', 'F'],
                   'sales': [12, 25, 23, 18, 14, 10]})

#set 'time' column as index
df = df.set_index('time')

#view DataFrame
print(df)

                 product  sales
time                           
4-15-2022 10:15        A     12
5-19-2022 7:14         B     25
8-01-2022 1:14         C     23
6-14-2022 9:45         D     18
10-24-2022 2:58        E     14
12-13-2022 11:03       F     10

Now suppose we attempt to create a new column that contains the hour of the time in the index column:

#attempt to create new column that contains hour of index column
df['hour'] = df.index.hour

AttributeError: 'Index' object has no attribute 'hour'

We receive an error because the index column is not currently in a datetime format so it doesn’t contain an ‘hour’ attribute.

To avoid this error, we can use the pandas to_datetime() function to convert the index column to a datetime format:

#convert index column to datetime format
df.index = pd.to_datetime(df.index)

#create new column that contains hour of index column
df['hour'] = df.index.hour#view updated DataFrame
print(df)

                    product  sales  hour
time                                    
2022-04-15 10:15:00       A     12    10
2022-05-19 07:14:00       B     25     7
2022-08-01 01:14:00       C     23     1
2022-06-14 09:45:00       D     18     9
2022-10-24 02:58:00       E     14     2
2022-12-13 11:03:00       F     10    11

By using the to_datetime() function, we’re able to convert the index column to a datetime format.

Thus, we’re able to successfully create a new column called hour that contains the hour of the time in the index column without receiving any error.

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

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

Cite this article

stats writer (2024). How can I convert an index to a datetime in Pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-convert-an-index-to-a-datetime-in-pandas/

stats writer. "How can I convert an index to a datetime in Pandas?." PSYCHOLOGICAL SCALES, 25 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-convert-an-index-to-a-datetime-in-pandas/.

stats writer. "How can I convert an index to a datetime in Pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-convert-an-index-to-a-datetime-in-pandas/.

stats writer (2024) 'How can I convert an index to a datetime in Pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-convert-an-index-to-a-datetime-in-pandas/.

[1] stats writer, "How can I convert an index to a datetime in Pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I convert an index to a datetime in Pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top