0

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')

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
  • What exactly are you trying to achieve here? datetime is not a *format*, it's a data type. Also, in pandas, there is only the datetime data type built-in, no separate date or time (as in "native" Python). That's why it's showing year/month/date as well, if you parse string to datetime. 1900-01-01 is the default if you don't specify anything. – FObersteiner Feb 28 '23 at 14:23

2 Answers2

0

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
Manoj biroj
  • 288
  • 1
  • 6
0

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