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
Selected Reading
Python Pandas - How to Round the DateTimeIndex with milliseconds frequency
To round the DateTimeIndex with milliseconds frequency, use the DateTimeIndex.round() method. For milliseconds frequency, use the freq parameter with value 'ms'.
Creating a DateTimeIndex
First, import pandas and create a DateTimeIndex with nanosecond precision ?
import pandas as pd
# Create DatetimeIndex with period 5 and frequency as 28 seconds
# timezone is Australia/Adelaide
datetimeindex = pd.date_range('2021-09-29 07:20:32.261811624', periods=5,
tz='Australia/Adelaide', freq='28s')
print("Original DateTimeIndex:")
print(datetimeindex)
Original DateTimeIndex:
DatetimeIndex(['2021-09-29 07:20:32.261811624+09:30',
'2021-09-29 07:21:00.261811624+09:30',
'2021-09-29 07:21:28.261811624+09:30',
'2021-09-29 07:21:56.261811624+09:30',
'2021-09-29 07:22:24.261811624+09:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq='28S')
Rounding to Milliseconds Frequency
Use the round() method with freq='ms' to round timestamps to the nearest millisecond ?
import pandas as pd
# Create DatetimeIndex with nanosecond precision
datetimeindex = pd.date_range('2021-09-29 07:20:32.261811624', periods=5,
tz='Australia/Adelaide', freq='28s')
# Round operation to milliseconds frequency
rounded_index = datetimeindex.round(freq='ms')
print("Rounded to milliseconds:")
print(rounded_index)
Rounded to milliseconds:
DatetimeIndex(['2021-09-29 07:20:32.262000+09:30',
'2021-09-29 07:21:00.262000+09:30',
'2021-09-29 07:21:28.262000+09:30',
'2021-09-29 07:21:56.262000+09:30',
'2021-09-29 07:22:24.262000+09:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq=None)
Complete Example
Here's a comprehensive example showing the original DateTimeIndex, its frequency, and the rounded result ?
import pandas as pd
# DatetimeIndex with period 5 and frequency as 28 seconds
# timezone is Australia/Adelaide
datetimeindex = pd.date_range('2021-09-29 07:20:32.261811624', periods=5,
tz='Australia/Adelaide', freq='28s')
# Display DateTimeIndex
print("DateTimeIndex...")
print(datetimeindex)
# Display DateTimeIndex frequency
print("\nDateTimeIndex frequency...")
print(datetimeindex.freq)
# Round operation on DateTimeIndex date with milliseconds frequency
# For milliseconds frequency, we have used 'ms'
print("\nPerforming round operation with milliseconds frequency...")
print(datetimeindex.round(freq='ms'))
DateTimeIndex...
DatetimeIndex(['2021-09-29 07:20:32.261811624+09:30',
'2021-09-29 07:21:00.261811624+09:30',
'2021-09-29 07:21:28.261811624+09:30',
'2021-09-29 07:21:56.261811624+09:30',
'2021-09-29 07:22:24.261811624+09:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq='28S')
DateTimeIndex frequency...
<28 * Seconds>
Performing round operation with milliseconds frequency...
DatetimeIndex(['2021-09-29 07:20:32.262000+09:30',
'2021-09-29 07:21:00.262000+09:30',
'2021-09-29 07:21:28.262000+09:30',
'2021-09-29 07:21:56.262000+09:30',
'2021-09-29 07:22:24.262000+09:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq=None)
Key Points
- The original timestamps have nanosecond precision (261811624 nanoseconds)
- Rounding to milliseconds converts this to 262000 microseconds (262 milliseconds)
- The
freqparameter becomesNoneafter rounding as the regular frequency is lost - Timezone information is preserved during the rounding operation
Conclusion
Use DateTimeIndex.round(freq='ms') to round timestamps to millisecond precision. This removes microsecond and nanosecond components while preserving timezone information.
Advertisements
