Reputation: 1
I am trying to format the due date column of my dataframe from strings to dates from the datetime class. It seems to work within my for-loop, however, when I leave the loop, the values in my dataframe do not change.
df.replace()
is obsolete, and .iat
and .at
do not work either. Code below. Thanks!
for x in df['due date']:
if type(x) == str:
date = x.split('/')
month = int(date[0])
day = int(date[1])
year = int(date[2].split(' ')[0])
formattedDate = dt.date(year, month, day)
df.at[x, "due date"] = formattedDate
Upvotes: 0
Views: 417
Reputation: 774
Unless I'm missing something here, you can just pass the column to the built in 'to_datetime' function.
df['due date'] = pd.to_datetime(df['due date'],format="%m/%d/%Y")
That is assuming your date format is something like: 02/24/2021 If you need to change the date format, see the following: strftime and strptime behavior
Upvotes: 1