How to set date column as index in Pandas Python [4 Methods]

Do you want to set the date column as the index in Pandas DataFrame? In this Python tutorial, I will tell you different ways to set date column as index in Pandas Python with some illustrative examples.

To set a date column as an index in a Pandas DataFrame in Python, you can use set_index() after converting the date to datetime, specify the index column directly when reading data with functions like pd.read_csv(), utilize DatetimeIndex for enhanced control over date formats, or perform an in-place modification by setting the date column as the index and then removing it from the DataFrame.

Set date column as index in Pandas Python

There are four different ways to set date column as index in Pandas Python:

  1. Using set_index()
  2. Specifying the Index Column when Reading Data
  3. Using DatetimeIndex
  4. In-Place Modification

Let’s see them one by one using some illustrative examples:

1. Set date as index Pandas using set_index() function

We can convert the date column to a datetime format using pd.to_datetime() and then use the set_index() method on the DataFrame, specifying the date column. This set date column as index in Pandas Python.

import pandas as pd

data = {'date': ['07/04/2023', '07/05/2023', '07/06/2023'],
        'sales': [200, 150, 250]}
df = pd.DataFrame(data)
df['date'] = pd.to_datetime(df['date'], format='%m/%d/%Y')
df.set_index('date', inplace=True)
print(df)

Output:

            sales
date             
2023-07-04    200
2023-07-05    150
2023-07-06    250

After implementing the code in the Pycharm editor, the screenshot is mentioned below.

How to set date column as index in Pandas Python

2. Set date column as index Pandas by specifying the index column when reading data

While reading data into a DataFrame (e.g., using pd.read_csv()), directly specify the date column as the index by using the index_col and parse_dates parameters. This approach helps us to set date column as index in Pandas Python as we load the data.

import pandas as pd

file_path = r'C:\Users\kumar\OneDrive\Desktop\Book1.csv'
try:
    df = pd.read_csv(file_path, parse_dates=['date'], index_col='date')
    print(df)
except FileNotFoundError:
    print(f"File not found. Please check the file path: {file_path}")
except Exception as e:
    print(f"An error occurred: {e}")

Output:

            Sales  Customers Specials
date                                 
2023-01-04    450        200       No
2023-02-04    520        250      Yes
2023-03-04    480        230       No
2023-04-04    500        240       No
2023-05-04    550        260      Yes
2023-06-04    530        250       No
2023-07-04    600        300      Yes

A screenshot is mentioned below, after implementing the code in the Pycharm editor.

how to make date column index pandas in Python
make date column index pandas Python

3. How to set date as index in Pandas using DatetimeIndex

We can create a DatetimeIndex from the date column and assign it to the DataFrame’s index to set date column as index in Pandas Python. We can use the del keyword to remove the date column after this.

import pandas as pd

data = {'date': ['2023-07-01', '2023-07-02', '2023-07-03'],
        'temperature': [76, 78, 75]}
df = pd.DataFrame(data)
df.index = pd.DatetimeIndex(df['date'])
del df['date']
print(df)

Output:

            temperature
date                   
2023-07-01           76
2023-07-02           78
2023-07-03           75

After executing the code in Pycharm, one can see the output in the below screenshot.

pandas set date column as index in Python

4. How to set date column as index in Pandas by in-place modification

After converting the date column to datetime, set it as the DataFrame’s index and then delete the original date column. This method modifies the DataFrame in place, removing the date column after setting it as the index in Python.

import pandas as pd

data = {'date': ['2023-08-01', '2023-08-02', '2023-08-03'],
        'visitors': [120, 150, 130]}
df = pd.DataFrame(data)
df['date'] = pd.to_datetime(df['date'])
df.index = df['date']
del df['date']
print(df)

Output:

            visitors
date                
2023-08-01       120
2023-08-02       150
2023-08-03       130

Below is a screenshot displaying the output following the implementation of the code in the Pycharm editor.

pandas set date as index in Python

Conclusion

Here, I have explained four effective methods to set date column as index in Pandas Python: using set_index(), specifying the index column when reading data, employing DatetimeIndex, and applying in-place modification. Each method caters to different scenarios, offering versatility and precision in handling date-indexed data in Python.

You may also like to read: