pandas dtype period [Q-DEC] changes to object

Viewed 74

I have a df with one dtype period[Q-DEC] column.

Column    Non-Null Count  Dtype        
---  ------    --------------  -----        
 0   CIK       7436 non-null   int64        
 1   LINKDATE  7436 non-null   period[Q-DEC]

Grouping the df as

df.sort_values(['CIK','LINKDATE']).groupby(['LINKDATE','CIK'],as_index=False).apply(lambda x: x.iloc[-1]).reset_index(drop=True)

turns LINKDATE into an object. How can I change the object back to period[Q-DEC]. I have tried pd.to_datetime(df['LINKDATE']) but I get this error:

TypeError: <class 'pandas._libs.tslibs.period.Period'> is not convertible to datetime

UPDATE: I need to convert the object to string format using strftime('%FQ%q'), then to period[Q-DEC] using pd.to_datetime(df.LINKDATE).to_period("Q")

0 Answers
Related