Skip to content
Advertisement

Pandas coverting columns of several dataframes to datetime doesn’t work in a loop

For some reason I cannot convert columns of different dataframes:

cols_to_change = [df1['date1'], df2['date2'], df3['date3']]

for i in cols_to_change:
    i = pd.to_datetime(i, format = '%Y-%m-%d')

although str can be converted to datetime.

But it works ok if I write

df1['date1'] = pd.to_datetime(df1['date1'], format = '%Y-%m-%d')
df2['date2'] = pd.to_datetime(df2['date2'], format = '%Y-%m-%d')

And if I call

pd.to_datetime(df1['date1'].loc[0],  format = '%Y-%m-%d')

it returns a Timestamp. Is this the point?

Advertisement

Answer

pd.to_datetime returns a new Series and you assigned that series to i, which you never used after the conversion.

You need to refer to the original dataframe and column:

cols_to_change = [
    (df1, 'date1'),
    (df2, 'date2'),
    (df3, 'date3')
]

for df, col in cols_to_change:
    df[col] = pd.to_datetime(df[col], format='%Y-%m-%d')
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement