I am trying to use PeriodIndex to create quarters, days, years but recently the function stopped working for some reason. Below is a min examples:
dict = [{'Date': '12/23/18'},
{'Date': '12/24/18'},
{'Date': '12/22/18'},
{'Date': '12/24/18'},
{'Date': '12/22/18'},
{'Date': '12/24/18'}]
df = pd.DataFrame(dict)
df['Date2'] = pd.to_datetime(df['Date']).dt.date
df['year'] = pd.PeriodIndex(df['Date2'], freq='A')
I get this error:
TypeError: Incorrect dtype
This used to work before but I am not sure why it does not work anymore.
Edt: I just noticed that the above commands work perfectly fine on my other computer (same version of Python, Win 10).
You can add .values
to your date. So df['year'] = pd.PeriodIndex(df['Date2'].values, freq='A')
This will work!
I run into the same problem after reinstaling and upgrading Pandas. Newer versions of Pandas accept date strings that are read as a np.array using .values
(as the author of this GitHub post also found).
What a coincidence, I just ran into this problem again today and you happened to have suggested the values approach just right now. Worked like a charm, thanks!!!
Cool coincidence, and great to hear! :)