Home > front end >  Date and Time Format Conversion in Pandas, Python
Date and Time Format Conversion in Pandas, Python

Time:12-04

Initially, my dataframe had a Month column containing numbers representing the months.

Month
1
2
3
4

I typed df["Month"] = pd.to_datetime(df["Month"]) and I get this...

Month
970-01-01 00:00:00.0000000001
1970-01-01 00:00:00.000000002
1970-01-01 00:00:00.000000003
1970-01-01 00:00:00.000000004

I would like to just retain just the dates and not the time. Any solutions?

CodePudding user response:

get the date from the column using df['Month'].dt.date

CodePudding user response:

Use format='%m' in to_datetime:

df["Month"] = pd.to_datetime(df["Month"], format='%m') 
print (df)
       Month
0 1900-01-01
1 1900-02-01
2 1900-03-01
3 1900-04-01 

CodePudding user response:

you can use timedelt

days=timedelta(months=1)
  • Related