I have a df
in which I want to change certain columns type to date
from datetime
:
field category 2022-01-10 00:00:00 2022-01-17 00:00:00 2022-01-24 00:00:00
A 10 500 700 500
B 15 60 70 50
I am trying to achieve this:
field category 2022-01-10 2022-01-17 2022-01-24
A 10 500 700 500
B 15 60 70 50
Because during concat with other df
the column type changes from 2021-11-19
to 2021-11-19 00:00:00
.
From this answer I tried::
df.loc[:,pd.to_datetime('2022-01-10 00:00:00') : pd.to_datetime('2022-06-20 00:00:00')].columns =
pd.to_datetime(df.loc[:,pd.to_datetime('2022-01-10 00:00:00') :
pd.to_datetime('2022-06-20 00:00:00')].columns)
.strftime('%Y-%m-%d')
But this does not change the type of the column which I cant understand why because
pd.to_datetime(df.loc[:,pd.to_datetime('2022-01-10 00:00:00') : pd.to_datetime('2022-06-20 00:00:00')].columns).strftime('%Y-%m-%d')
Return:
Index(['2022-01-10', '2022-01-17', '2022-01-24')]