I have a dataframe where year and month are hidden in the multi-index
. I want to create a datetime index as additional column (or separate series with same index).
price
mean mom_2
foo bar year month
997182819645 11 2010 1 1.1900 3.000000
2 2.2625 4.001769
I thought of adding the two levels of indices together as strings, and then read in that sequence into pd.to_datetime()
. However, adding the two indices, I faced problems. I can add them up as integers just fine, but if I want to add them up as strings, I face some error:
In[193]: df.index.get_level_values('year').values.astype(str)
Out[193]:
array(['2010', '2010', '2010', ..., '2014', '2014', '2014'],
dtype='<U21')
In[194]: df.index.get_level_values('month').values.astype(str)
Out[194]:
array(['1', '2', '3', ..., '10', '11', '12'],
dtype='<U21')
In[195]: df.index.get_level_values('month').values.astype(str) + df.index.get_level_values('year').values.astype(str)
TypeError: ufunc 'add' did not contain a loop with signature matching types
dtype('<U21') dtype('<U21') dtype('<U21')
How can I add create the datetime index here?