Issue
I have a dataframe with a Date column whose content is: years-months-days of type string. I want to convert this to Date type (not Datetime). The goal is to insert this dataframe into a bigquery table with the Date type.
I tried:
df['Date '] = [datetime.strptime(str(i), '%Y-%m-%d') for i in df['Date ']]
But this still returns Datetime format Years-Months-Days Hours:Minutes:Seconds (2022-01-03 00:00:00)
Someone have an idea to convert this string to type Date:Y-m-d (not Datetime)
Solution
Try pandas.to_datetime() and extract the date:
import pandas as pd
df = pd.DataFrame({'Dates':['2020-01-22', '2021-05-06', '2022-01-01']})
df.Dates.apply(lambda x: pd.to_datetime(x).date())
outputs:
0 2020-01-22
1 2021-05-06
2 2022-01-01
Name: Dates, dtype: object
Answered By - TitoOrt
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.