I would like to understand if I can convert a string column to a PeriodIndex (for instance year), preserving the string (suffix).
I have the following DataFrame:
company date ... revenue taxes
Facebook 2017-01-01 00:00:00 Total ... 1796.00 0.00
Facebook 2018-07-01 00:00:00 Total ... 7423.20 -11.54
Facebook Total - ... 1704.00 0.00
Google 2017-12-01 00:00:00 Total ... 1938.60 -1938.60
Google 2018-12-01 00:00:00 Total ... 1403.47 -102.01
Google 2018-01-01 00:00:00 Total ... 2028.00 -76.38
Google Total - ... 800.00 -256.98
I'm trying to apply the PeriodIndex to date:
df['date'] = pd.PeriodIndex(df['date'].values, freq='Y')
However, nothing happens because Pandas can't convert it to a string. I can't remove the word Total from my DataFrame.
This is what I expect to achieve:
company date ... revenue taxes
Facebook 2017 Total ... 1796.00 0.00
Facebook 2018 Total ... 7423.20 -11.54
Facebook Total - ... 1704.00 0.00
Google 2017 Total ... 1938.60 -1938.60
Google 2018 Total ... 1403.47 -102.01
Google 2018 Total ... 2028.00 -76.38
Google Total - ... 800.00 -256.98
Any way I can get around with this?
Thanks!