Python Pandas - Display the end time of the period for each element in the given PeriodIndex object

PythonServer Side ProgrammingProgramming

To display the end time of the period for each element in the given PeriodIndex object, use the PeriodIndex.end_time property.

At first, import the required libraries −

import pandas as pd

Create a PeriodIndex object. PeriodIndex is an immutable ndarray holding ordinal values indicating regular periods in time. We have set the frequency using the "freq" parameter −

periodIndex = pd.PeriodIndex(['2018-07-25', '2019-10-30', '2020-11-20',
'2021-09-15', '2022-03-12', '2023-06-18'], freq="D")

Display PeriodIndex object −

print("PeriodIndex...\n", periodIndex)

Display the end time −

print("\nEnd Time...\n", periodIndex.end_time)

Example

Following is the code −

import pandas as pd

# Create a PeriodIndex object
# PeriodIndex is an immutable ndarray holding ordinal values indicating regular periods in time
# We have set the frequency using the "freq" parameter
periodIndex = pd.PeriodIndex(['2018-07-25', '2019-10-30', '2020-11-20',
'2021-09-15', '2022-03-12', '2023-06-18'], freq="D")

# Display PeriodIndex object
print("PeriodIndex...\n", periodIndex)

# Display PeriodIndex frequency
print("\nPeriodIndex frequency...\n", periodIndex.freq)

# Display the end time
print("\nEnd Time...\n", periodIndex.end_time)

Output

This will produce the following code −

PeriodIndex...
PeriodIndex(['2018-07-25', '2019-10-30', '2020-11-20', '2021-09-15', '2022-03-12', '2023-06-18'],
dtype='period[D]')

PeriodIndex frequency...
<Day>

End Time...
DatetimeIndex(['2018-07-25 23:59:59.999999999',
               '2019-10-30 23:59:59.999999999',
               '2020-11-20 23:59:59.999999999',
               '2021-09-15 23:59:59.999999999',
               '2022-03-12 23:59:59.999999999',
               '2023-06-18 23:59:59.999999999'],
               dtype='datetime64[ns]', freq=None)
raja
Updated on 20-Oct-2021 11:36:17

Advertisements