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
Python Pandas - Return the string alias of the Time series frequency applied on the given Period object
To return the string alias of the Time series frequency applied on the given Period object, use the period.freqstr property. This property provides a human-readable representation of the frequency used by the Period object.
What is period.freqstr?
The freqstr property returns a string alias that describes the frequency of the Period object. It shows information like the base frequency (Second, Day, Year) and any additional parameters.
Creating Period Objects
First, let's 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="Y", year=2021, month=2, day=14, hour=2, minute=35)
# Display the Period objects
print("Period1...\n", period1)
print("Period2...\n", period2)
Period1... 2020-09-23 03:55:20 Period2... 2021
Getting Frequency String Aliases
Now let's extract the frequency string aliases using the freqstr property ?
import pandas as pd
# Create Period objects
period1 = pd.Period("2020-09-23 03:55:20")
period2 = pd.Period(freq="Y", year=2021, month=2, day=14, hour=2, minute=35)
# Get the string alias of the frequency
res1 = period1.freqstr
res2 = period2.freqstr
# Display the frequency aliases
print("String alias of frequency from Period1:", res1)
print("String alias of frequency from Period2:", res2)
String alias of frequency from Period1: S String alias of frequency from Period2: A-DEC
Example with Multiple Frequencies
Here's a comprehensive example showing different frequency types ?
import pandas as pd
# Create Period objects with various frequencies
daily = pd.Period("2020-01-15", freq="D")
monthly = pd.Period("2020-01", freq="M")
quarterly = pd.Period("2020Q1", freq="Q")
yearly = pd.Period("2020", freq="Y")
# Display periods and their frequency aliases
periods = [daily, monthly, quarterly, yearly]
names = ["Daily", "Monthly", "Quarterly", "Yearly"]
for name, period in zip(names, periods):
print(f"{name} Period: {period}")
print(f"Frequency alias: {period.freqstr}")
print("-" * 30)
Daily Period: 2020-01-15 Frequency alias: D ------------------------------ Monthly Period: 2020-01 Frequency alias: M ------------------------------ Quarterly Period: 2020Q1 Frequency alias: Q-DEC ------------------------------ Yearly Period: 2020 Frequency alias: A-DEC ------------------------------
Common Frequency Aliases
| Frequency | Alias | Description |
|---|---|---|
| Second | S | Second frequency |
| Daily | D | Daily frequency |
| Monthly | M | Month end frequency |
| Quarterly | Q-DEC | Quarter end in December |
| Yearly | A-DEC | Year end in December |
Conclusion
The freqstr property is useful for identifying the frequency alias of Period objects. It returns descriptive strings like "S" for seconds, "D" for daily, and "A-DEC" for annual periods ending in December.
