Apply to_datetime with parameters in pandas

Viewed 324

I would like to select columns and apply to_datetime with parameters. I have a working method using lambda but would like to understand why.

Setup

d = {'date': ['5/2/2000'], 'date2': ['3/02/2000'], 'date3': ['1-2-2000']}
df = pd.DataFrame(data=d)
print(df.dtypes)

This works and gives the desired output.

df[['date', 'date2', 'date3']] = df[['date', 'date2', 'date3']].apply(lambda x: pd.to_datetime(x, dayfirst=True))

output
    date        date2       date3
0   2000-02-05  2000-02-03  2000-02-01

This works but does not take parameters so does not work with UK dates.

df[['date', 'date2', 'date3']] = df[['date', 'date2', 'date3']].apply(pd.to_datetime)

output (incorrect)
    date        date2       date3
0   2000-05-02  2000-03-02  2000-01-02

This does not work but why?

df[['date', 'date2', 'date3']] = df[['date', 'date2', 'date3']].apply(pd.to_datetime(dayfirst=True))

error

TypeError: to_datetime() missing 1 required positional argument: 'arg'

Why can I not apply to_datetime with parameters? Is this always the case with apply?

1 Answers

Put the dayfirst=True as a parameter to .apply (look at the **kwargs parameter):

df[["date", "date2", "date3"]] = df[["date", "date2", "date3"]].apply(
    pd.to_datetime, dayfirst=True
)

print(df)

Prints:

        date      date2      date3
0 2000-02-05 2000-02-03 2000-02-01
Related