In my pandas dataframe I want to find the difference between dates in months. The function .dt.to_period('M')
results in a MonthEnd object like <11 * MonthEnds>
instead of the month number.
I tried to change the column type with pd.to_numeric()
and to remove the letters with re.sub("[^0-9]", "", 'blablabla123bla')
. Both do not work on a MonthEnd
object.
df['duration_dataset'] = df['date_1'].dt.to_period('M') - df['date_2'].dt.to_period('M')
I expected 11, but the output is <11 * MonthEnds>
.
Here is a minimum dataframe
d = {'date_1': ['2018-03-31','2018-09-30'], 'date_2': ['2017-12-31','2017-12-31']}
df = pd.DataFrame(data=d)
df['date_1'] = pd.to_datetime(df['date_1'], format='%Y-%m-%d')
df['date_2'] = pd.to_datetime(df['date_2'], format='%Y-%m-%d')
df['duration_dataset'] = df['date_1'].dt.to_period('M') - df['date_2'].dt.to_period('M')
df
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…