Jimmy Zhao
Jimmy Zhao

Reputation: 319

How to save a PeriodArray to list?

I have scripts like below:

df['quarter'] = pd.PeriodIndex(df.creation_datetime, freq='Q')

df['quarter'] looks like 2021Q3 in format. I hope to get the unique values of date time and store them into a list. How to achieve something like that? I check the PeriodArray documentation but find no clue.

time_seq = df.quarter.unique().values.to_list()

This just gave me errors like:

AttributeError: 'PeriodArray' object has no attribute 'to_list'

Upvotes: 0

Views: 871

Answers (1)

Jimmy Zhao
Jimmy Zhao

Reputation: 319

I solved this problem by:

df.quarter = df.quarter.astype(str)
time_quarter = list(df.quarter.unique())

It works!

Upvotes: 1

Related Questions