I have a pandas dataframe that contains a date column where the dates are stored as strings:
0 2021-12-04
1 2021-12-01
2 2021-11-29
3 2021-11-15
4 2021-11-06
Name: date, dtype: object
I have a solution that uses variable assignment:
df['date'] = df.apply(lambda x: datetime.strptime(x['date'], '%Y-%m-%d'), axis=1)
But since this transformation is part of a data pipeline, I want to use the assign
method. I tried:
df.assign(date=df['date'].apply(datetime.strptime('%Y-%m-%d')))
But this produces an error: KeyError: 'date'
.
I suspect this is because the values from the date
column aren't being passed to datetime.strptime('%Y-%m-%d')
. What is the best way to solve this error?
CodePudding user response:
Use pd.to_datetime
instead of apply
lambda
datetime.strptime
:
df.assign(date=pd.to_datetime(df['date'], format='%Y-%m-%d'))