pythonintermediate

Pandas PeriodIndex for Fiscal Calendars

Use PeriodIndex for fiscal period arithmetic, aggregations, and comparisons beyond datetime.

python
import pandas as pd
import numpy as np

idx = pd.period_range('2024Q1', periods=8, freq='Q')
df = pd.DataFrame({'revenue': np.random.randint(50000, 150000, 8)}, index=idx)

df['yoy_growth'] = df['revenue'].pct_change(4)  # compare same quarter prior year
df['is_h2'] = df.index.month > 6

print(df)
print('Annual total:', df.resample('A')['revenue'].sum())

Use Cases

  • fiscal reporting
  • quarterly analytics
  • period comparisons

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.