Convert datetime Column to Index of pandas DataFrame in Python (3 Examples)
In this Python programming tutorial, you’ll learn how to use date and time columns as index of a pandas DataFrame.
Preparing the Examples
Before we start, we first have to load the pandas library, as you can see below:
import pandas as pd # Import pandas library in Python |
import pandas as pd # Import pandas library in Python
data_x = pd.DataFrame({'date':['02-07-2016', # Generate example data '09-05-2022', '04-08-2023', '03-04-2024'], 'time':['18:46:00', '09:54:00', '17:13:00', '11:39:00'], 'x1':[1, 7, 5, 4], 'x2':['a', 'd', 'f', 'y']}) print(data_x) # Print example DataFrame # date time x1 x2 # 0 02-07-2016 18:46:00 1 a # 1 09-05-2022 09:54:00 7 d # 2 04-08-2023 17:13:00 5 f # 3 03-04-2024 11:39:00 4 y |
data_x = pd.DataFrame({'date':['02-07-2016', # Generate example data '09-05-2022', '04-08-2023', '03-04-2024'], 'time':['18:46:00', '09:54:00', '17:13:00', '11:39:00'], 'x1':[1, 7, 5, 4], 'x2':['a', 'd', 'f', 'y']}) print(data_x) # Print example DataFrame # date time x1 x2 # 0 02-07-2016 18:46:00 1 a # 1 09-05-2022 09:54:00 7 d # 2 04-08-2023 17:13:00 5 f # 3 03-04-2024 11:39:00 4 y
Example 1: Convert DatetimeIndex from Divided Date & Time Column by Applying the format Parameter
To set DatetimeIndex from separated date and time columns, we use the format parameter followed by the set_index function:
data_x1 = data_x.copy() # Duplicate data data_x1['datetime'] = pd.to_datetime(data_x1['date'] + # Generate datetime column by applying the format parameter data_x1['time'], format = '%m-%d-%Y%H:%M:%S') data_x1 = data_x1.set_index('datetime') # Using the set_index function print(data_x1) # Show DataFrame with changed index # date time x1 x2 # datetime # 2016-02-07 18:46:00 02-07-2016 18:46:00 1 a # 2022-09-05 09:54:00 09-05-2022 09:54:00 7 d # 2023-04-08 17:13:00 04-08-2023 17:13:00 5 f # 2024-03-04 11:39:00 03-04-2024 11:39:00 4 y |
data_x1 = data_x.copy() # Duplicate data data_x1['datetime'] = pd.to_datetime(data_x1['date'] + # Generate datetime column by applying the format parameter data_x1['time'], format = '%m-%d-%Y%H:%M:%S') data_x1 = data_x1.set_index('datetime') # Using the set_index function print(data_x1) # Show DataFrame with changed index # date time x1 x2 # datetime # 2016-02-07 18:46:00 02-07-2016 18:46:00 1 a # 2022-09-05 09:54:00 09-05-2022 09:54:00 7 d # 2023-04-08 17:13:00 04-08-2023 17:13:00 5 f # 2024-03-04 11:39:00 03-04-2024 11:39:00 4 y
del data_x1['time'] # Delete column time del data_x1['date'] # Delete column date print(data_x1) # Show adapted DataFrame # x1 x2 # datetime # 2016-02-07 18:46:00 1 a # 2022-09-05 09:54:00 7 d # 2023-04-08 17:13:00 5 f # 2024-03-04 11:39:00 4 y |
del data_x1['time'] # Delete column time del data_x1['date'] # Delete column date print(data_x1) # Show adapted DataFrame # x1 x2 # datetime # 2016-02-07 18:46:00 1 a # 2022-09-05 09:54:00 7 d # 2023-04-08 17:13:00 5 f # 2024-03-04 11:39:00 4 y
Example 2: Convert DatetimeIndex from Divided Date & Time Column by Applying the + Operator
Another possibility to create a datetime column and set it as DatetimeIndex is by using the + operator combined with the to_datetime function followed by the set_index function:
data_x2 = data_x.copy() # Duplicate data data_x2['datetime'] = pd.to_datetime(data_x2['date'] + # Generate datetime column by applying the + operator ' ' + data_x2['time']) data_x2 = data_x2.set_index('datetime') # Using the + Operator print(data_x2) # Show DataFrame with changed index # date time x1 x2 # datetime # 2016-02-07 18:46:00 02-07-2016 18:46:00 1 a # 2022-09-05 09:54:00 09-05-2022 09:54:00 7 d # 2023-04-08 17:13:00 04-08-2023 17:13:00 5 f # 2024-03-04 11:39:00 03-04-2024 11:39:00 4 y |
data_x2 = data_x.copy() # Duplicate data data_x2['datetime'] = pd.to_datetime(data_x2['date'] + # Generate datetime column by applying the + operator ' ' + data_x2['time']) data_x2 = data_x2.set_index('datetime') # Using the + Operator print(data_x2) # Show DataFrame with changed index # date time x1 x2 # datetime # 2016-02-07 18:46:00 02-07-2016 18:46:00 1 a # 2022-09-05 09:54:00 09-05-2022 09:54:00 7 d # 2023-04-08 17:13:00 04-08-2023 17:13:00 5 f # 2024-03-04 11:39:00 03-04-2024 11:39:00 4 y
del data_x2['time'] # Delete column time del data_x2['date'] # Delete column date print(data_x2) # Show Adapted DataFrame # x1 x2 # datetime # 2016-02-07 18:46:00 1 a # 2022-09-05 09:54:00 7 d # 2023-04-08 17:13:00 5 f # 2024-03-04 11:39:00 4 y |
del data_x2['time'] # Delete column time del data_x2['date'] # Delete column date print(data_x2) # Show Adapted DataFrame # x1 x2 # datetime # 2016-02-07 18:46:00 1 a # 2022-09-05 09:54:00 7 d # 2023-04-08 17:13:00 5 f # 2024-03-04 11:39:00 4 y
Example 3: Convert DatetimeIndex from Already Existing datetime Column
In case, we already have an existing datetime column, we simply apply the set_index function to get our DatetimeIndex:
data_x3 = pd.DataFrame({'datetime':pd.to_datetime(['09-04-2016 21:12:00', # Creating example data '08-07-2017 23:08:00', '06-04-2018 11:32:00', '05-01-2019 12:53:00']), 'x1':[1, 7, 5, 4], 'x2':['a', 'd', 'f', 'y']}) print(data_x3) # Show example DataFrame # datetime x1 x2 # 0 2016-09-04 21:12:00 1 a # 1 2017-08-07 23:08:00 7 d # 2 2018-06-04 11:32:00 5 f # 3 2019-05-01 12:53:00 4 y |
data_x3 = pd.DataFrame({'datetime':pd.to_datetime(['09-04-2016 21:12:00', # Creating example data '08-07-2017 23:08:00', '06-04-2018 11:32:00', '05-01-2019 12:53:00']), 'x1':[1, 7, 5, 4], 'x2':['a', 'd', 'f', 'y']}) print(data_x3) # Show example DataFrame # datetime x1 x2 # 0 2016-09-04 21:12:00 1 a # 1 2017-08-07 23:08:00 7 d # 2 2018-06-04 11:32:00 5 f # 3 2019-05-01 12:53:00 4 y
data_x3 = data_x3.set_index('datetime') # Using set_index function print(data_x3) # Show new DataFrame with changed index # x1 x2 # datetime # 2016-09-04 21:12:00 1 a # 2017-08-07 23:08:00 7 d # 2018-06-04 11:32:00 5 f # 2019-05-01 12:53:00 4 y |
data_x3 = data_x3.set_index('datetime') # Using set_index function print(data_x3) # Show new DataFrame with changed index # x1 x2 # datetime # 2016-09-04 21:12:00 1 a # 2017-08-07 23:08:00 7 d # 2018-06-04 11:32:00 5 f # 2019-05-01 12:53:00 4 y
Further Resources & Related Tutorials
Here, you may find some further resources on topics such as importing data, data objects, and counting:
- Return GroupBy Object Back to New pandas DataFrame in Python
- Load & Import Particular Columns from CSV File as pandas DataFrame in Python
- Locate Specific Value in pandas DataFrame in Python
- Count Distinct Values by Group of pandas DataFrame Column in Python
- All Python Programming Tutorials
Note: This article was created in collaboration with Matthias Bäuerlen. Matthias is a programmer who helps to create tutorials on the Python programming language. You might find more info about Matthias and his other articles on his profile page.