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 PeriodIndex object to Timestamp
To convert a PeriodIndex object to Timestamp, use the PeriodIndex.to_timestamp() method. This method converts period-based data to timestamp-based data, which is useful when you need to work with datetime operations.
Understanding PeriodIndex
A PeriodIndex represents regular periods in time (like years, months, quarters). When converted to timestamps, it creates a DatetimeIndex with specific points in time.
Creating a PeriodIndex
First, let's create a PeriodIndex object with yearly frequency ?
import pandas as pd
# Create a PeriodIndex object with yearly frequency
periodIndex = pd.PeriodIndex(['2021-09-25 07:30:35', '2019-10-30 04:15:45',
'2020-07-15 02:55:15', '2022-06-25 09:40:55'], freq="Y")
print("PeriodIndex...")
print(periodIndex)
PeriodIndex... PeriodIndex(['2021', '2019', '2020', '2022'], dtype='period[A-DEC]')
Converting to Timestamp
Use to_timestamp() to convert the PeriodIndex to a DatetimeIndex ?
import pandas as pd
# Create a PeriodIndex object
periodIndex = pd.PeriodIndex(['2021-09-25 07:30:35', '2019-10-30 04:15:45',
'2020-07-15 02:55:15', '2022-06-25 09:40:55'], freq="Y")
# Convert to timestamp
timestamp_index = periodIndex.to_timestamp()
print("Original PeriodIndex:")
print(periodIndex)
print("\nConverted to Timestamp:")
print(timestamp_index)
print("\nData type:", type(timestamp_index))
Original PeriodIndex: PeriodIndex(['2021', '2019', '2020', '2022'], dtype='period[A-DEC]') Converted to Timestamp: DatetimeIndex(['2021-01-01', '2019-01-01', '2020-01-01', '2022-01-01'], dtype='datetime64[ns]', freq=None) Data type: <class 'pandas.core.indexes.datetimes.DatetimeIndex'>
Complete Example
Here's a comprehensive example showing the conversion process ?
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(['2021-09-25 07:30:35', '2019-10-30 04:15:45',
'2020-07-15 02:55:15', '2022-06-25 09:40:55'], freq="Y")
# Display PeriodIndex object
print("PeriodIndex...")
print(periodIndex)
# Display PeriodIndex frequency
print("\nPeriodIndex frequency object...")
print(periodIndex.freq)
# Display PeriodIndex frequency as string
print("\nPeriodIndex frequency object as a string...")
print(periodIndex.freqstr)
# Convert PeriodIndex to timestamp
print("\nPeriodIndex object to timestamp...")
print(periodIndex.to_timestamp())
PeriodIndex... PeriodIndex(['2021', '2019', '2020', '2022'], dtype='period[A-DEC]') PeriodIndex frequency object... <YearEnd: month=12> PeriodIndex frequency object as a string... A-DEC PeriodIndex object to timestamp... DatetimeIndex(['2021-01-01', '2019-01-01', '2020-01-01', '2022-01-01'], dtype='datetime64[ns]', freq=None)
Key Points
- The
to_timestamp()method converts periods to the beginning of the period by default - Original datetime information is simplified based on the frequency (yearly frequency shows only years)
- The result is a
DatetimeIndexobject suitable for time-based operations
Conclusion
Use PeriodIndex.to_timestamp() to convert period-based indices to timestamp-based indices. This conversion enables datetime operations and creates standardized timestamps from period data.
