'pd.read_csv - dates in pandas multiindex column names

I import a csv file into a pandas dataframe.

df=pd.read_csv('data.csv',index_col=[0],header=[0,1])

My data has a column multiindex with two levels. Level(0) contains strings and level(1) contains dates. By default, these dates become strings when imported.

I would like to convert level(1) column names to date format either when I import the data (but I cannot figure out the right way to do that when reading the documentation) or subsequently, if not possible during the import phase.

However, if I do:

cdq.columns.levels[1]=cdq.columns.levels[1].astype('datetime64[ns]')

I get the message that 'FrozenList' does not support mutable operations.

Is there a way to do that?

d = {'Ticker':['ABBN.SW','ABBN.SW','ABBN.SW','ABBN.SW'],
'Date':['31/12/2021 00:00','30/09/2021 00:00','30/06/2021 00:00','31/03/2021 00:00'],
'investments':[-480000000,251000000,892000000,162000000],
'changeToLiabilities':[298000000,52000000,267000000,42000000]}

pd.DataFrame(d)

    Ticker              Date  investments  changeToLiabilities
0  ABBN.SW  31/12/2021 00:00   -480000000            298000000
1  ABBN.SW  30/09/2021 00:00    251000000             52000000
2  ABBN.SW  30/06/2021 00:00    892000000            267000000
3  ABBN.SW  31/03/2021 00:00    162000000             42000000


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source