drop year and month and year from a date format

52 Views Asked by At

so I want to convert this column from str to date format ,i used the following df["Time"]=pd.to_datetime(df["Time"],format='%H:%M')

OUTPUT: 0 1900-01-01 02:29:00 1 1900-01-01 06:52:00 2 1900-01-01 10:23:00 3 1900-01-01 12:59:00 . . . Name: Time, Length: 736, dtype: datetime64[ns]

why does it show me the year,month and day even tho i didnt specify it?how can i remove it?

the string format : "02:29"

df["Time"]=pd.to_datetime(df["Time"],format='%H:%M')

2

There are 2 best solutions below

0
Manoj Kumar Biroj On

format will be considered as your input format not the values you want to consider. Try the below line

df['Time'] = pd.to_datetime(df['Time'],format= '%H:%M' ).dt.time
0
Badhreesh M Rao On

You can use the datetime property dt to get the time after the conversion, like so:

df["Time"]=pd.to_datetime(df["Time"],format='%H:%M').dt.time