杰瑞科技汇

Python PeriodRange如何高效使用?

Of course! The term "Python period range" most commonly refers to pandas.period_range(). This is a function in the powerful Pandas library for creating a fixed-frequency PeriodIndex, which is essentially a sequence of time periods (like months, quarters, or years).

Python PeriodRange如何高效使用?-图1
(图片来源网络,侵删)

Let's break it down with clear explanations and examples.

What is pandas.period_range()?

It's a function that generates a PeriodIndex. Unlike a DatetimeIndex which represents specific points in time (timestamps), a PeriodIndex represents a span or interval of time.

  • DatetimeIndex: A single moment, e.g., 2025-01-01 00:00:00.
  • PeriodIndex: A duration, e.g., January 2025, Q1-2025, or the year 2025.

This is extremely useful for time series analysis where you want to group data into meaningful periods (like months or years) and perform calculations on them.


Syntax and Parameters

The basic syntax is:

Python PeriodRange如何高效使用?-图2
(图片来源网络,侵删)
pandas.period_range(start=None, end=None, periods=None, freq=None, name=None)

Key Parameters:

  • start: The first period in the range. (e.g., '2025-01' for a monthly frequency).
  • end: The last period in the range.
  • periods: The number of periods to generate.
  • freq: The frequency of the periods. This is the most important parameter. (e.g., 'D' for day, 'M' for month-end, 'Q' for quarter-end).
  • name: An optional name for the resulting index.

You must provide two out of the three parameters: start, end, and periods.


Common Frequency Aliases (freq)

The freq parameter uses specific string aliases. Here are the most common ones:

Alias Description Example Period
A Year-end 2025
Q Quarter-end 2025Q1
M Month-end 2025-01
D Day 2025-01-01
H Hour 2025-01-01 12
T or min Minute 2025-01-01 12:30
S Second 2025-01-01 12:30:00

You can also add suffixes to modify the behavior:

Python PeriodRange如何高效使用?-图3
(图片来源网络,侵删)
  • S for start of period (e.g., 'MS' for Month-Start).
  • E for end of period (e.g., 'M' or 'ME' for Month-End).

Code Examples

Let's see period_range in action.

Example 1: Monthly Periods

Let's create a range of monthly periods for the year 2025.

import pandas as pd
# Create a monthly period range for 2025
# We provide start, end, and freq
monthly_periods = pd.period_range(start='2025-01', end='2025-12', freq='M')
print(monthly_periods)

Output:

PeriodIndex(['2025-01', '2025-02', '2025-03', '2025-04', '2025-05', '2025-06',
             '2025-07', '2025-08', '2025-09', '2025-10', '2025-11', '2025-12'],
            dtype='period[M]', freq='M')

Example 2: Using periods Instead of end

Here, we'll generate 7 quarterly periods starting from Q3 2025.

# Create a quarterly range, specifying start and periods
quarterly_periods = pd.period_range(start='2025Q3', periods=7, freq='Q')
print(quarterly_periods)

Output:

PeriodIndex(['2025Q3', '2025Q4', '2025Q1', '2025Q2', '2025Q3', '2025Q4',
             '2025Q1'],
            dtype='period[Q-DEC]', freq='Q-DEC')

Note: The freq is shown as Q-DEC because it defaults to calendar year-end quarters.

Example 3: Daily Periods

Let's create a 5-day period range.

# Create a daily range
daily_periods = pd.period_range(start='2025-10-25', periods=5, freq='D')
print(daily_periods)

Output:

PeriodIndex(['2025-10-25', '2025-10-26', '2025-10-27', '2025-10-28',
             '2025-10-29'],
            dtype='period[D]', freq='D')

Example 4: Naming the Index

It's good practice to name your index.

# Create a yearly range with a name
yearly_periods = pd.period_range(start='2025', end='2025', freq='A', name='fiscal_year')
print(yearly_periods)

Output:

PeriodIndex(['2025', '2025', '2025', '2025', '2025'], dtype='period[A-DEC]', name='fiscal_year', freq='A-DEC')

How to Use a PeriodIndex

A PeriodIndex is most often used as the index of a Pandas Series or DataFrame.

Example: Time Series Analysis

Let's say we have sales data for each month of 2025. Using a PeriodIndex is perfect for this.

import pandas as pd
# 1. Create the PeriodIndex
sales_index = pd.period_range(start='2025-01', end='2025-12', freq='M')
# 2. Create a Series with the index
sales_data = [120, 135, 125, 150, 160, 155, 170, 180, 175, 190, 185, 200]
sales_series = pd.Series(sales_data, index=sales_index, name='Monthly Sales')
print("Sales Series:")
print(sales_series)
print("\n" + "="*30 + "\n")
# 3. Perform operations on the periods
# Access data for a specific period
print("Sales for March 2025:")
print(sales_series['2025-03'])
print("\n" + "="*30 + "\n")
# Select data for a specific year
print("All sales for the year 2025:")
print(sales_series['2025'])
print("\n" + "="*30 + "\n")
# Resample or aggregate data (e.g., calculate total annual sales)
# This is where PeriodIndex shines!
annual_sales = sales_series.resample('A').sum()
print("Total Annual Sales for 2025:")
print(annual_sales)

Output:

Sales Series:
2025-01    120
2025-02    135
2025-03    125
2025-04    150
2025-05    160
2025-06    155
2025-07    170
2025-08    180
2025-09    175
2025-10    190
2025-11    185
2025-12    200
Name: Monthly Sales, dtype: int64
==============================
Sales for March 2025:
125
==============================
All sales for the year 2025:
2025-01    120
2025-02    135
2025-03    125
2025-04    150
2025-05    160
2025-06    155
2025-07    170
2025-08    180
2025-09    175
2025-10    190
2025-11    185
2025-12    200
Name: Monthly Sales, dtype: int64
==============================
Total Annual Sales for 2025:
2025    1945
Name: Monthly Sales, dtype: int64

Period vs. PeriodIndex

  • pd.Period: Represents a single time period. Think of it as one element from the PeriodIndex.
  • pd.period_range(): Creates a collection of these periods, an Index object.
# A single Period object
single_period = pd.Period('2025-Q2', freq='Q')
print(f"Single Period: {single_period}")
print(f"Start time of the period: {single_period.start_time}")
print(f"End time of the period: {single_period.end_time}")
# A PeriodIndex (a collection of Periods)
period_index = pd.period_range('2025-Q1', '2025-Q4', freq='Q')
print(f"\nPeriodIndex:\n{period_index}")

Summary

Feature pandas.period_range()
Purpose Create a sequence of time periods.
Object Type Returns a PeriodIndex.
Core Use Case Indexing time series data where the data is aggregated by a period (month, quarter, year).
Key Parameter freq: Defines the period's length (e.g., 'M', 'Q', 'A').
Main Advantage Makes it easy and intuitive to slice, group, and aggregate time series data by time periods.
分享:
扫描分享到社交APP
上一篇
下一篇