Reputation: 215
I am trying to find Period date difference. Per Pandas 0.24+ : Subtraction of a Period from another Period will give a DateOffset. instead of an integer (GH21314). But how to get INT results. I have look into similar query: Why can't I subtract one date period from the next and convert to an integer? It says adding ".n" will give results, but that didn't work for me.
I also tried converting both into INT using .astype(int), but getting error: 'Period' object has no attribute 'astype'. I must be missing something, not sure what!
I am using Pandas Version: '1.0.1'.
DDL to generate Dataframe:
import pandas as pd
date_start = pd.Period('10/2018',freq='M')
df = pd.DataFrame({'id': [1, 2, 3, 4, 5, 6],
'date_end': ['2017-01', '2018-08', '2019-04','2018-06',
'2018-12', '2018-10']})
df['date_end'] = pd.PeriodIndex(pd.to_datetime(df['date_end'], format='%Y-%m'),
freq='M')
df['diff'] = date_start - df['date_end']
Thanks!
Upvotes: 4
Views: 1462
Reputation: 2049
You need to access the individual Period[M]
objects, not the series:
df['diff'].n
AttributeError: 'Series' object has no attribute 'n'
df['diff'].apply(lambda x: x.n)
0 21
1 2
2 -6
3 4
4 -2
5 0
Upvotes: 2