I have a dataframe where the names of the columns are dates (Year-month) in the form of strings. How can I convert these names in datetime format? I tried doing this:
new_cols = pd.to_datetime(df.columns)
df = df[new_cols]
but I get the error:
KeyError: "DatetimeIndex(
['2000-01-01', '2000-02-01',
'2000-03-01', '2000-04-01',
'2000-05-01', '2000-06-01',
'2000-07-01', '2000-08-01',
'2000-09-01', '2000-10-01',
'2015-11-01', '2015-12-01',
'2016-01-01', '2016-02-01',
'2016-03-01', '2016-04-01',
'2016-05-01', '2016-06-01',
'2016-07-01', '2016-08-01'],
dtype='datetime64[ns]', length=200, freq=None) not in index"
Thanks!