Pandas Period.to_timestamp() Function:
The Period.to_timestamp() function of the Pandas module gives the Timestamp representation of the Period object at the target/set frequency at the given end (how) of the Period.
Syntax:
Period.to_timestamp()
Parameters:
freq: It is the target frequency. If self.freq is a week or longer, the default is ‘D,’ otherwise, it is ‘S.’
how: This is optional. It specifies whether the Start or end of the timespan. ‘S’, ‘E’. Case insensitive can be used as an alias. ‘Start,’ ‘Finish,’ ‘Begin,’ and ‘End’
Return Value:
The Timestamp is returned by the Period.to_timestamp() function of the Pandas module.
- Python Pandas Period.dayofyear Attribute
- Python Pandas Timestamp.to_datetime64 () Function
- Python Pandas Period.end_time Attribute
Pandas Period.to_timestamp() Function in Python
Example1
Here, the Period.to_timestamp() function returns the given period object as a timestamp in the provided frequency i.e, ‘M'(Monthly frequency).
Approach:
- Import pandas module using the import keyword.
- Pass some random frequency(here S= secondly frequency), year, month, day, hour, minute, second as the arguments to the Period() function of the pandas module to get the Period object
- Store it in a variable.
- Print the given period object
- Pass freq =’M’ as an argument to the to_timestamp() function and apply it on the given period object to get the Timestamp representation in the given frequency of the Period object.
- Here ‘M’= Monthly frequency.
- The Exit of the Program.
Below is the implementation:
# Import pandas module using the import keyword.
import pandas as pd
# Pass some random frequency(here S= secondly frequency), year, month, day,
# hour, minute, second as the arguments to the Period() function
# of the pandas module to get the Period object
# Store it in a variable.
period_obj = pd.Period(freq ='S', year = 2015, month = 5, day = 16,
hour = 12, minute = 28, second = 10)
# Print the given period object
print("The given period object:")
print(period_obj)
print()
# Pass freq ='M' as an argument to the to_timestamp() function and apply it on the
# given period object to get the Timestamp representation in the given frequency
# of the Period object.
# Here 'M'= Monthly frequency
print("The Timestamp representation in the given frequency of the Period object:")
period_obj.to_timestamp(freq ='M')
Output:
The given period object:
2015-05-16 12:28:10
The Timestamp representation in the given frequency of the Period object:
Timestamp('2015-05-31 00:00:00')
Example2
Here, it returns the given period object as a timestamp in the Minutely(T) frequency.
Approach:
- Import pandas module using the import keyword.
- Pass some random frequency(here S= secondly frequency), year, month, day, hour, minute, second as the arguments to the Period() function of the pandas module to get the Period object
- Store it in a variable.
- Print the given period object
- Pass freq =’T’ as an argument to the to_timestamp() function and apply it on the given period object to get the Timestamp representation in the given frequency of the Period object.
- Here ‘T’= Minutely frequency
- The Exit of the Program.
Below is the implementation:
# Import pandas module using the import keyword.
import pandas as pd
# Pass some random frequency(here S= secondly frequency), year, month, day,
# hour, minute, second as the arguments to the Period() function
# of the pandas module to get the Period object
# Store it in a variable.
period_obj = pd.Period(freq ='S', year = 2021, month = 3, day = 14,
hour = 16, minute = 40, second = 25)
# Print the given period object
print("The given period object:")
print(period_obj)
print()
# Pass freq ='T' as an argument to the to_timestamp() function and apply it on the
# given period object to get the Timestamp representation in the given frequency
# of the Period object.
# Here 'T'= Minutely frequency
print("The Timestamp representation in the Minutely frequency of the Period object:")
period_obj.to_timestamp(freq ='T')
Output:
The given period object:
2021-03-14 16:40:25
The Timestamp representation in the Minutely frequency of the Period object:
Timestamp('2021-03-14 16:40:00')