Anku
Anku

Reputation: 215

Python Pandas Period Date difference is in * MonthEnds>, NaT, how to convert it into INT value

I am trying to convert difference between two periods (having NaT values as well) into INT values. Getting error as: AttributeError: 'NatType' object has no attribute 'n'.

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', None , '2019-04','2018-06', 
                               '2018-12', '2019-10']})
df['date_end'] = pd.PeriodIndex(pd.to_datetime(df['date_end'], format='%Y-%m'),
                                freq='M')

df['diff'] = date_start - df['date_end']
df['diff'].apply(lambda x: x.n)

What did I miss?

Upvotes: 0

Views: 406

Answers (1)

user13914826
user13914826

Reputation:

Well, you have a None value in the DataFrame. Try changing that None to another date.

Upvotes: 1

Related Questions