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
Selected Reading
Python Pandas - Get the frequency for the given Period object
To get the frequency for the given Period object, use the period.freq property. The pandas.Period represents a period of time with an associated frequency that determines the resolution of the time period.
Creating Period Objects
First, import pandas and create Period objects with different frequencies ?
import pandas as pd
# Create Period objects with different frequencies
period1 = pd.Period("2020-09-23 03:55:20")
period2 = pd.Period(freq="T", year=2021, month=2, day=14, hour=2, minute=35)
print("Period1:", period1)
print("Period2:", period2)
Period1: 2020-09-23 03:55:20 Period2: 2021-02-14 02:35
Getting Frequency Information
Use the .freq property to retrieve the frequency of each Period object ?
import pandas as pd
# Create Period objects
period1 = pd.Period("2020-09-23 03:55:20")
period2 = pd.Period(freq="T", year=2021, month=2, day=14, hour=2, minute=35)
# Get the frequency from Period objects
freq1 = period1.freq
freq2 = period2.freq
print("Frequency of Period1:", freq1)
print("Frequency of Period2:", freq2)
Frequency of Period1: <Second> Frequency of Period2: <Minute>
Common Frequency Codes
Different frequency codes can be used when creating Period objects ?
import pandas as pd
# Create periods with different frequency codes
daily_period = pd.Period("2020-01-01", freq="D")
monthly_period = pd.Period("2020-01", freq="M")
yearly_period = pd.Period("2020", freq="Y")
print("Daily period frequency:", daily_period.freq)
print("Monthly period frequency:", monthly_period.freq)
print("Yearly period frequency:", yearly_period.freq)
Daily period frequency: <Day> Monthly period frequency: <MonthEnd> Yearly period frequency: <YearEnd: month=12>
Frequency Codes Summary
| Code | Description | Example |
|---|---|---|
| D | Daily | <Day> |
| M | Month End | <MonthEnd> |
| Y | Year End | <YearEnd: month=12> |
| T or min | Minute | <Minute> |
| S | Second | <Second> |
Conclusion
The .freq property returns the frequency of a Period object. When no frequency is specified during creation, pandas infers it from the input string format.
Advertisements
