Reputation: 1129
I keep seeing cases of datetime for this question but none only with the time.
I have a time column like this:
0 16:19:01.600000
1 16:20:02.700000
2 16:20:18
3 16:21:03.400000
4 16:21:49.200000
To round the time to seconds so it is 16:19:02, 16:20:03, and so on, I've tried to first convert the time column to datetime with the following code:
# Converting time to datetime (again)
pd.to_datetime(trip_28["time"], format="%H:%M:%S")
which returns:
unconverted data remains: .600000
Then the intention is to use:
# Rounding the time column to seconds
trip_28["time"] = trip_28["time"].dt.floor('1s')
How can I make this work?
Upvotes: 0
Views: 335
Reputation: 120409
Convert to datetime and round with freq='S'
:
>>> pd.to_datetime(df['Date']).dt.round(freq='S')
0 2021-08-03 16:19:02
1 2021-08-03 16:20:03
2 2021-08-03 16:20:18
3 2021-08-03 16:21:03
4 2021-08-03 16:21:49
Name: Date, dtype: datetime64[ns]
Upvotes: 1
Reputation: 794
Try pd.DatetimeIndex.round with the frequency set to S
From the docs
pd.Series(rng).dt.round("H")
0 2018-01-01 12:00:00
1 2018-01-01 12:00:00
2 2018-01-01 12:00:00
Upvotes: 1