In this article, we’ll get to know how we can create the DatetimeIndex and access the Date and Time details separately using some date and time methods.
We can create the DatetimeIndex with the following syntax:
Syntax:
Parameters:
- The first parameter takes Timestamp values in a list that is separated by a comma.
- The second parameter is optional, which specifies the data type like datetime64[ns].
- Freq is also the optional parameter which gets the Datetime in hours/weeks frequency.
Example 1: With Freq=’H’
Create the DatetimeIndex that holds 2 timestamps with frequency as “H”. Make sure that you need to specify the correct frequency with respect to the timestamp.
# Create the DatetimeIndex that holds 2 timestamps with frequency as 'H'.
datetime_index = pandas.DatetimeIndex(['2022-12-31 04:11:00', '2022-12-31 05:11:00'],
dtype='datetime64[ns]',freq='H')
print(datetime_index)
Output:
Now, you have the DatetimeIndex with Hours as Frequency.
Let’s see another example that takes the frequency as Week.
Example 2: With Freq=’W’
# Create the DatetimeIndex that holds 4 timestamps with frequency as 'W'.
datetime_index = pandas.DatetimeIndex(['2023-1-1 04:11:00', '2023-1-8 04:11:00','2023-1-15 04:11:00','2023-1-22 04:11:00'],freq='W')
print(datetime_index)
Output:
Now, you have the DatetimeIndex with Weeks as Frequency.
Example 3: Get the Date Details
The DatetimeIndex has some built-in methods that get the Date details like Year, Month, Date, day, etc.
Look at the following example to return the year, month, name of the month, day, and date.
# Create the DatetimeIndex that holds 4 timestamps with frequency as 'W'.
datetime_index = pandas.DatetimeIndex(['2023-1-1 04:11:00', '2023-1-8 04:11:00','2023-1-15 04:11:00','2023-1-22 04:11:00'],freq='W')
# Return the Year from the above datetime_index
print("Year: ",datetime_index.year)
# Return the month from the above datetime_index
print("Month:",datetime_index.month)
# Return the month_name from the above datetime_index
print("Name of the month:",datetime_index.month_name)
# Return the day from the above datetime_index
print("Day: ",datetime_index.day)
# Return the date from the above datetime_index
print("Date: ",datetime_index.date)
Output:
Explanation:
- The year returns the year from the DatetimeIndex.
- The month returns the month number (Like January is 1, February is 2,…) from the DatetimeIndex.
- The day returns the day from the DatetimeIndex.
- The date returns the Date in the (YYYY, MM, DD) format.
Example 4: Get the Time Details
Look at the following example to return the hours, minutes, seconds, and time separately.
# Create the DatetimeIndex that holds 4 timestamps with frequency as 'W'.
datetime_index = pandas.DatetimeIndex(['2023-1-1 04:11:00', '2023-1-8 04:11:00', '2023-1-15 04:11:00', '2023-1-22 04:11:00'], freq='W')
# Return the hour from the above datetime_index
print("Hour: ",datetime_index.hour)
# Return the minute from the above datetime_index
print("Minute: ",datetime_index.minute)
# Return the second from the above datetime_index
print("Second: ",datetime_index.second)
# Return the time from the above datetime_index
print("Time: ",datetime_index.time)
Output:
Explanation:
- The hour returns the hours from the DatetimeIndex.
- The minute returns the minutes from the DatetimeIndex.
- The second returns the seconds from the DatetimeIndex.
- The time returns the Time in the (H,M) format.
Example 5: Year Considerations
- If we want to check the status if any of the day is the start of the year, use is_year_start.
- If we want to check the status if any of the day is the end of the year, use is_year_end.
- If we want to check the status if the year is a leap year or not, use is_leap_year.
# Create the DatetimeIndex that holds 5 timestamps.
datetime_index = pandas.DatetimeIndex(['2021-1-1 07:13:00', '2022-12-31 05:11:00','2022-11-11 05:11:00','2020-12-31 06:11:00','2017-5-5 05:11:00'])
print(datetime_index)
print()
# Check the status if any of the day is start of the year
print(datetime_index.is_year_start)
print()
# Check the status if any of the day is end of the year
print(datetime_index.is_year_end)
print()
# Check the status if the year is leap year or not.
print(datetime_index.is_leap_year)
Output:
Explanation:
True is returned for the following datetime indices:
- “2021-01-01 07:13:00” starts with the year.
- “2022-12-31 05:11:00” and “2020-12-31 06:11:00” ends in the year.
- “2020-12-31 06:11:00” is the leap year.
Conclusion
Now, we came to a point that DatetimeIndex is used to create Index for the datetime data and we can extract the date and time details using different properties. All the examples were well explained. Make sure to understand all the parameters which are explained with respect to DatetimeIndex.