Skip to content
Advertisement

Pandas: Assign Datetime object to time intervals

I’m trying to create a new variable in which datetime64[ns] objects are assigned to 5 minute intervals. The new interval variable should span every 5 minute period from 00:00 to 23:55. The criteria for assignment is whether the time of the datetime64[ns] object falls within the corresponding 5 min interval. My actual data has numerous dates in the DateTime variable, but these different dates shouldn’t be taken into account – only the time element matters for assignment.

I’ve simulated this below. This example focuses on the time period circa 23:30 to 23:45, but it should exemplify what I’m trying to achieve for all the intervals from 00:00 to 23:55. I have included two random dates to illustrate how the dates should not have any bearing.

JavaScript

For the sake of clarity I’m aiming for this result:

JavaScript

I’ve read the pandas documentation thoroughly and some questions on here that very loosely relate, but I can’t seem to get anything to achieve the right result.


Update

These are my library and system versions:

JavaScript

This is the error in full. Here you can see that with my actual data I’m working with a datetime64[ns] Series called question_time.

JavaScript

The issue seems to be with the SettingWithCopyError. I tried resetting all my variables and now I am getting this same warning with another operation as well.

Advertisement

Answer

Not sure of a better method but you can construct 2 TimeDeltaIndices and subtract this from your value, I use modulus op % to calc the number of minutes to subtract:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement