Pythonista anonymous
Pythonista anonymous

Reputation: 8950

Why can't I use a dataframe's numerical index in a calculation with apply and pandas DateOffset?

I need to create a column in a dataframe, containing dates 3 months from each other.

I tried using df.apply with pandas.DateOffset and the numerical index of the dataframe, but I get this error:

TypeError: cannot perform rmul with this index type: Index

What does it mean? The index is numerical and can be used in other calculations - see how I calculate df['y'] below.

import pandas as pd
import numpy as np


df =pd.DataFrame()
df['something'] = np.arange(4,61)
my_date = pd.to_datetime('1/Jan/2021', format='%d/%b/%Y')

#this doesn't work
df['dates']= df.apply( lambda x: my_date + pd.DateOffset(months = 3 * x.index), axis=1 )

#this works
df['y'] = df.index * 2
df = df.reset_index()
df['dates']= df.apply( lambda x: my_date + pd.DateOffset(months = 3 * x['index']), axis=1 )

Upvotes: 1

Views: 34

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150745

Since you're applying with axis=1, x is a row. And each row is a series indexed by the dataframe's column. So you want name, not index:

df['dates']= df.apply( lambda x: my_date + pd.DateOffset(months = 3 * x.name), axis=1)

Output:

   something      dates
0          4 2021-01-01
1          5 2021-04-01
2          6 2021-07-01
3          7 2021-10-01
4          8 2022-01-01

Upvotes: 1

Related Questions