Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Pandas - How to perform floor operation on the DateTimeIndex with milliseconds frequency
To perform floor operation on the DateTimeIndex with milliseconds frequency, use the DateTimeIndex.floor() method. For milliseconds frequency, use the freq parameter with value 'ms'.
The floor operation rounds down datetime values to the nearest specified frequency unit. When applied with milliseconds frequency, it truncates microsecond and nanosecond precision, keeping only up to milliseconds.
Syntax
DateTimeIndex.floor(freq)
Parameters:
- freq ? The frequency level to floor to. For milliseconds, use 'ms'
Creating DateTimeIndex with High Precision
First, let's create a DateTimeIndex with nanosecond precision ?
import pandas as pd
# DatetimeIndex with period 5 and frequency as 40S (40 seconds)
# timezone is Australia/Adelaide
datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5,
tz='Australia/Adelaide', freq='40S')
# Display DateTimeIndex
print("DateTimeIndex...")
print(datetimeindex)
# Display DateTimeIndex frequency
print("\nDateTimeIndex frequency...")
print(datetimeindex.freq)
DateTimeIndex...
DatetimeIndex(['2021-10-18 07:20:32.261811624+10:30',
'2021-10-18 07:21:12.261811624+10:30',
'2021-10-18 07:21:52.261811624+10:30',
'2021-10-18 07:22:32.261811624+10:30',
'2021-10-18 07:23:12.261811624+10:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq='40S')
DateTimeIndex frequency...
<40 * Seconds>
Applying Floor Operation with Milliseconds Frequency
Now, let's apply the floor operation with milliseconds frequency ?
import pandas as pd
# Create DateTimeIndex
datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5,
tz='Australia/Adelaide', freq='40S')
print("Original DateTimeIndex:")
print(datetimeindex)
# Floor operation with milliseconds frequency
floored_index = datetimeindex.floor(freq='ms')
print("\nAfter floor operation with milliseconds frequency:")
print(floored_index)
Original DateTimeIndex:
DatetimeIndex(['2021-10-18 07:20:32.261811624+10:30',
'2021-10-18 07:21:12.261811624+10:30',
'2021-10-18 07:21:52.261811624+10:30',
'2021-10-18 07:22:32.261811624+10:30',
'2021-10-18 07:23:12.261811624+10:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq='40S')
After floor operation with milliseconds frequency:
DatetimeIndex(['2021-10-18 07:20:32.261000+10:30',
'2021-10-18 07:21:12.261000+10:30',
'2021-10-18 07:21:52.261000+10:30',
'2021-10-18 07:22:32.261000+10:30',
'2021-10-18 07:23:12.261000+10:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq=None)
How It Works
The floor operation with milliseconds frequency:
- Keeps year, month, day, hour, minute, second, and millisecond values intact
- Truncates microseconds and nanoseconds (sets them to zero)
- In the example above, '.261811624' becomes '.261000'
- The resulting DateTimeIndex has freq=None as the regular interval is broken
Other Frequency Options
You can use different frequency aliases with the floor operation ?
import pandas as pd
# Create DateTimeIndex
datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=3,
tz='Australia/Adelaide', freq='40S')
print("Original:")
print(datetimeindex)
print("\nFloor to seconds ('S'):")
print(datetimeindex.floor(freq='S'))
print("\nFloor to minutes ('T'):")
print(datetimeindex.floor(freq='T'))
Original:
DatetimeIndex(['2021-10-18 07:20:32.261811624+10:30',
'2021-10-18 07:21:12.261811624+10:30',
'2021-10-18 07:21:52.261811624+10:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq='40S')
Floor to seconds ('S'):
DatetimeIndex(['2021-10-18 07:20:32+10:30', '2021-10-18 07:21:12+10:30',
'2021-10-18 07:21:52+10:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq=None)
Floor to minutes ('T'):
DatetimeIndex(['2021-10-18 07:20:00+10:30', '2021-10-18 07:21:00+10:30',
'2021-10-18 07:21:00+10:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq=None)
Conclusion
Use DateTimeIndex.floor(freq='ms') to round down datetime values to millisecond precision. This operation truncates microseconds and nanoseconds while preserving higher-level time components.
