Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of X

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
2 min

banner

# Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of X

The Pandas "TypeError Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of X" occurs when the operation you're trying to perform expects a DateTimeIndex, TimedeltaIndex or a PeriodIndex, but a value of a different type is passed.

To solve the error, convert the column to a datetime object and set it as the index of the DataFrame.

Here is an example of how the error occurs.

main.py
import pandas as pd df = pd.DataFrame({ 'values': [1, 2, 3], 'date': ['2023-01-05', '2023-03-25', '2023-01-24'] }) # ⛔️ TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex' print(df.resample('M').sum())

type error only valid with datetimeindex timedelta index or period index

The DataFrame.resample method resamples time-series data so we need to convert the date column to datetime.

main.py
import pandas as pd df = pd.DataFrame({ 'values': [1, 2, 3], 'date': ['2023-01-05', '2023-03-25', '2023-01-24'] }) df['date'] = pd.to_datetime(df['date']) df = df.set_index('date') # values # date # 2023-01-31 4 # 2023-02-28 0 # 2023-03-31 2 print(df.resample('M').sum())

convert column to datetime and set it as df index

The code for this article is available on GitHub

We used the pandas.to_datetime() method to convert the date column to datetime.

The next step is to set the use the DataFrame.set_index() method to set the DataFrame index using the date column.

If the error persists:

  1. Remove the call to set_index().
  2. Set the on parameter when calling resample().
main.py
import pandas as pd df = pd.DataFrame({ 'numbers': [1, 2, 3], 'date': ['2023-01-05', '2023-03-25', '2023-01-24'] }) df['date'] = pd.to_datetime(df['date']) # numbers # date # 2023-01-31 4 # 2023-02-28 0 # 2023-03-31 2 print(df.resample('M', on='date').sum())

set on parameter when calling resample

The code for this article is available on GitHub

The on parameter is the column that should be used instead of the index for resampling.

The column must be datetime-like.

Notice that we still had to convert the date column to datetime before calling resample().

If you need to create a new column containing the results, use the transform() method.

main.py
import pandas as pd df = pd.DataFrame({ 'numbers': [1, 2, 3], 'date': ['2023-01-05', '2023-03-25', '2023-01-24'] }) df['date'] = pd.to_datetime(df['date']) df['monthly'] = df.resample('M', on='date').transform('sum') # numbers date monthly # 0 1 2023-01-05 4 # 1 2 2023-03-25 2 # 2 3 2023-01-24 4 print(df)

using transform method

The code for this article is available on GitHub

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.