Python Pandas - Return the Period object as a timestamp with daily frequency


To return the Period object as a timestamp with daily frequency, use the period.to_timestamp() method and set the freq parameter as ‘D’.

At first, import the required libraries −

import pandas as pd

The pandas.Period represents a period of time. Creating a Period object

period = pd.Period(freq="S", year = 2021, month = 11, day = 26, hour = 11, minute = 45, second = 55)

Display the Period object

print("Period...\n", period)

Return the Timestamp representation of the Period object. We have set the frequency using the "freq" parameter. The frequency is set as 'D' i.e. daily

print("\nPeriod to Timestamp with daily frequency...\n", period.to_timestamp(freq='D'))

Example

Following is the code

import pandas as pd

# The pandas.Period represents a period of time
# Creating a Period object
period = pd.Period(freq="S", year = 2021, month = 11, day = 26, hour = 11, minute = 45, second = 55)

# display the Period object
print("Period...\n", period)

# Return the Timestamp representation of the Period object
# We have set the frequency using the "freq" parameter
# The frequency is set as 'D' i.e. daily
print("\nPeriod to Timestamp with daily frequency...\n", period.to_timestamp(freq='D'))

Output

This will produce the following code

Period...
2021-11-26 11:45:55

Period to Timestamp with daily frequency...
2021-11-26 00:00:00

Updated on: 20-Oct-2021

100 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements