
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Pandas - Convert given Timestamp to Period
To convert given Timestamp to Period, use the timestamp.to_period() method. Within that, set the frequency using the freq parameter.
At first, import the required libraries −
import pandas as pd
Set the timestamp object in Pandas
timestamp = pd.Timestamp('2021-09-14T15:12:34.261811624')
Now, convert timestamp to Period. We have set the frequency as Month using the "freq" parameter with value 'M'
timestamp.to_period(freq='M')
Example
Following is the code
import pandas as pd # set the timestamp object in Pandas timestamp = pd.Timestamp('2021-09-14T15:12:34.261811624') # display the Timestamp print("Timestamp...\n", timestamp) # convert timestamp to Period # we have set the frequency as Month using the "freq" parameter with value 'M' print("\nTimestamp to Period...\n", timestamp.to_period(freq='M'))
Output
This will produce the following code
Timestamp... 2021-09-14 15:12:34.261811624 Timestamp to Period... 2021-09
- Related Articles
- Python Pandas - Convert given Timestamp to Period with minutely frequency
- Python Pandas - Convert given Timestamp to Period with weekly frequency
- Python Pandas - Convert given Timestamp to Period with monthly frequency
- Python Pandas - Convert given Timestamp to Period with quarterly frequency
- Python Pandas - Convert given Timestamp to Period with hourly frequency
- Python Pandas - Convert PeriodIndex object to Timestamp
- Python Pandas - Convert Timestamp to another time zone
- Python Pandas - Convert Period to desired frequency
- Python Pandas - How to convert DateTimeIndex to Period
- Python Pandas - Return the Timestamp representation of the Period object
- Python Pandas - Convert naive Timestamp to local time zone
- Python Pandas - Return the Period object as a timestamp with monthly frequency
- Python Pandas - Return the Period object as a timestamp with minutely frequency
- Python Pandas - Return the Period object as a timestamp with daily frequency
- Python Pandas - Return the Period object as a timestamp with yearly frequency

Advertisements