Last updated: Apr 12, 2024
Reading time·2 min
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.
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())
The
DataFrame.resample
method resamples time-series data so we need to convert the date
column to
datetime
.
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())
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:
set_index()
.on
parameter when calling resample()
.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())
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.
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)
You can learn more about the related topics by checking out the following tutorials: