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 - Convert times to midnight in DateTimeIndex
To convert times to midnight in DateTimeIndex, use the DateTimeIndex.normalize() method in Pandas. This method sets the time component of all datetime values to 00:00:00 while preserving the date and timezone information.
What is normalize()?
The normalize() method converts the time component of datetime values to midnight (00:00:00). This is useful when you want to work with dates only, ignoring the time portion.
Creating a DateTimeIndex
First, let's create a DateTimeIndex with various times ?
import pandas as pd
# Create DateTimeIndex with period 7 and frequency as 10H (10 hours)
# The timezone is Australia/Adelaide
datetimeindex = pd.date_range('2021-10-30 02:30:50', periods=7, tz='Australia/Adelaide', freq='10H')
print("Original DateTimeIndex...")
print(datetimeindex)
Original DateTimeIndex...
DatetimeIndex(['2021-10-30 02:30:50+10:30', '2021-10-30 12:30:50+10:30',
'2021-10-30 22:30:50+10:30', '2021-10-31 08:30:50+10:30',
'2021-10-31 18:30:50+10:30', '2021-11-01 04:30:50+10:30',
'2021-11-01 14:30:50+10:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq='10H')
Converting Times to Midnight
Now let's normalize the DateTimeIndex to convert all times to midnight ?
import pandas as pd
# Create DateTimeIndex
datetimeindex = pd.date_range('2021-10-30 02:30:50', periods=7, tz='Australia/Adelaide', freq='10H')
# Convert time component to midnight (00:00:00)
normalized_index = datetimeindex.normalize()
print("Normalized DateTimeIndex (times converted to midnight)...")
print(normalized_index)
Normalized DateTimeIndex (times converted to midnight)...
DatetimeIndex(['2021-10-30 00:00:00+10:30', '2021-10-30 00:00:00+10:30',
'2021-10-30 00:00:00+10:30', '2021-10-31 00:00:00+10:30',
'2021-10-31 00:00:00+10:30', '2021-11-01 00:00:00+10:30',
'2021-11-01 00:00:00+10:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq=None)
Complete Example
Here's a complete example showing the original and normalized DateTimeIndex ?
import pandas as pd
# Create DateTimeIndex with period 7 and frequency as 10H (10 hours)
datetimeindex = pd.date_range('2021-10-30 02:30:50', periods=7, tz='Australia/Adelaide', freq='10H')
# Display original DateTimeIndex
print("Original DateTimeIndex...")
print(datetimeindex)
# Display DateTimeIndex frequency
print("\nDateTimeIndex frequency...")
print(datetimeindex.freq)
# Normalize - convert time component to midnight (00:00:00)
print("\nNormalized DateTimeIndex (times converted to midnight)...")
print(datetimeindex.normalize())
Original DateTimeIndex...
DatetimeIndex(['2021-10-30 02:30:50+10:30', '2021-10-30 12:30:50+10:30',
'2021-10-30 22:30:50+10:30', '2021-10-31 08:30:50+10:30',
'2021-10-31 18:30:50+10:30', '2021-11-01 04:30:50+10:30',
'2021-11-01 14:30:50+10:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq='10H')
DateTimeIndex frequency...
<10 * Hours>
Normalized DateTimeIndex (times converted to midnight)...
DatetimeIndex(['2021-10-30 00:00:00+10:30', '2021-10-30 00:00:00+10:30',
'2021-10-30 00:00:00+10:30', '2021-10-31 00:00:00+10:30',
'2021-10-31 00:00:00+10:30', '2021-11-01 00:00:00+10:30',
'2021-11-01 00:00:00+10:30'],
dtype='datetime64[ns, Australia/Adelaide]', freq=None)
Key Points
- The
normalize()method preserves the date and timezone information - All time components are set to 00:00:00 (midnight)
- The frequency information is lost after normalization (becomes None)
- Duplicate dates may result when multiple times from the same day are normalized
Conclusion
Use DateTimeIndex.normalize() to convert all times in a DateTimeIndex to midnight. This method is useful for date-only operations while preserving timezone information.
