jb1225
jb1225

Reputation: 41

Trying to generate just time in string format using strftime from a datetime column in pandas

I have a datetime column in pandas that I'm trying to use to generate a string in HH:MM:SS.fff format. The result that I get from the following code provides the date in YYYY-MM-DD format and then the time in my desired format. I can't figure out why the following code automatically produces the date along with the time.

print(meta['DATETIME'].dtype)
print(meta['DATETIME'].dt.strftime('%H:%m:%s.%f').str[:-3].values)

# Returns:
datetime64[ns]
['2021-11-15 11:12:48.660' '2021-11-15 11:12:58.660']

Upvotes: 0

Views: 248

Answers (1)

rudolfovic
rudolfovic

Reputation: 3286

Works fine for me:

>>> data = pd.Series([pd.to_datetime('2021/1/1 14:32')]
>>> print(data).dt.strftime('%H:%M:%S.%f').str[:-3].values)
['14:32:00.000']

Upvotes: 1

Related Questions