Python | Панды DatetimeIndex.normalize ()

Опубликовано: 28 Марта, 2022

Python - отличный язык для анализа данных, в первую очередь из-за фантастической экосистемы пакетов Python, ориентированных на данные. Pandas - один из таких пакетов, который значительно упрощает импорт и анализ данных.

Pandas DatetimeIndex.normalize() function convert times to midnight. The time component of the date-timeise converted to midnight i.e. 00:00:00. This is useful in cases, when the time does not matter. Length is unaltered. The timezones are unaffected.

Syntax: DatetimeIndex.normalize()

Return: DatetimeIndex or Series

Example #1: Use DatetimeIndex.normalize() function to normalize time.

Output :

Now we want all the time values present in the DatetimeIndex object to be normalized i.e., it gets converted to midnight time.

# normalize the time.
idx.normalize()

Output :

As we can see in the output, the function has converted all the time values in the object to midnight time.
 
Example #2: Use DatetimeIndex.normalize() function to normalize time.

# importing pandas as pd
import pandas as pd
  
# Create the DatetimeIndex
# Here "Q" represents quarter end frequency 
idx = pd.DatetimeIndex(start ="2000-01-15 08:00", freq ="Q"
                           periods = 4, tz ="Asia/Calcutta")
  
# Print the DatetimeIndex
print(didx)

Выход :

Now we want all the time values present in the DatetimeIndex object to be normalized i.e., it gets converted to midnight time.

# normalize the time.
idx.normalize()

Выход :

Как видно из выходных данных, функция преобразовала все значения времени в объекте в полночь.

Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.

Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.