 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Pandas - Convert given Timestamp to Period with weekly frequency
To convert given Timestamp to Period, use the timestamp.to_period() method. Within that, set the frequency using the freq parameter. For weekly frequency, set freq as W.
At first, import the required libraries −
import pandas as pd
Create the timestamp object in Pandas
timestamp = pd.Timestamp(2021, 9, 18, 11, 50, 20, 33)
Convert timestamp to Period. We have set the frequency as weekly using the "freq" parameter with value 'W'
timestamp.to_period(freq='W')
Example
Following is the code
 import pandas as pd # set the timestamp object in Pandas timestamp = pd.Timestamp(2021, 9, 18, 11, 50, 20, 33) # display the Timestamp print("Timestamp...\n", timestamp) # convert timestamp to Period # we have set the frequency as weekly using the "freq" parameter with value 'W' print("\nTimestamp to Period with weekly frequency...\n", timestamp.to_period(freq='W'))  Output
This will produce the following code
Timestamp... 2021-09-18 11:50:20.000033 Timestamp to Period with weekly frequency... 2021-09-13/2021-09-19
Advertisements
 