How do I delete year month and date, display only time on python

Viewed 35

I have to convert the object to DateTime. However, it shows a year, month, and day at the front. So how can I display only time?

f1['Time'] = pd.to_datetime(f1['Time'], format = '%H:%M:%S.%f') f1['Time']

It shows:

0 1900-01-01 01:32:03.897

1 1900-01-01 02:02:34.598

2 1900-01-01 01:34:31.421

What I want is time only, like this:

0 01:32:03.897

1 02:02:34.598

2 01:34:31.421

3 Answers

For 24h format :

print(datetime.datetime.now().strftime("%X"))

For 12h formart with am/pm :

print(datetime.datetime.now().strftime("%I:%M:%S %p"))

Please include your code in your post and not in a screenshot.

But also, did you try using

datetime.time

It seems that you're successfully converting the data to datetime objects. What is being shown in the output is the string representation of the date.

The format string you passed to pd.to_datetime is just for parsing the dates. It does not affect their internal data. If you only need strings, you should call .strftime("%H:%M:%S.%f") for each date object in your collection.

Related