Skip to content

BUG: Unable to create PeriodIndex with freq of QuarterEnd with startingMonth != 12 #55784

@cancan101

Description

@cancan101

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd

# These work:
print(pd.Period(year=2014, quarter=3, freq=pd.offsets.QuarterEnd(startingMonth=12))) # -> 2014Q3
print(pd.Period(year=2014, quarter=3, freq=pd.offsets.QuarterEnd(startingMonth=2))) # -> 2014Q3

print(pd.Period(year=2014, quarter=3, freq=pd.offsets.QuarterEnd(startingMonth=12)).freqstr) # -> Q-DEC
print(pd.Period(year=2014, quarter=3, freq=pd.offsets.QuarterEnd(startingMonth=2)).freqstr) # -> Q-FEB

# this works
print(pd.PeriodIndex(year=[2014], quarter=[3], freq=pd.offsets.QuarterEnd(startingMonth=12))) # -> PeriodIndex(['2014Q3'], dtype='period[Q-DEC]')

# this does not work:
pd.PeriodIndex(year=[2014], quarter=[3], freq=pd.offsets.QuarterEnd(startingMonth=2))

Issue Description

I am able to create Periods with a freq of pd.offsets.QuarterEnd(startingMonth=2) but not a PeriodIndex:

AssertionError                            Traceback (most recent call last)
Cell In[9], line 12
      9 print(pd.PeriodIndex(year=[2014], quarter=[3], freq=pd.offsets.QuarterEnd(startingMonth=12))) # -> PeriodIndex(['2014Q3'], dtype='period[Q-DEC]')
     11 # this does not work:
---> 12 pd.PeriodIndex(year=[2014], quarter=[3], freq=pd.offsets.QuarterEnd(startingMonth=2))

File ~/.pyenv/versions/3.12.0/envs/scratch/lib/python3.12/site-packages/pandas/core/indexes/period.py:244, in PeriodIndex.__new__(cls, data, ordinal, freq, dtype, copy, name, **fields)
    240 if not fields:
    241     # test_pickle_compat_construction
    242     cls._raise_scalar_data_error(None)
--> 244 data, freq2 = PeriodArray._generate_range(None, None, None, freq, fields)
    245 # PeriodArray._generate range does validation that fields is
    246 # empty when really using the range-based constructor.
    247 freq = freq2

File ~/.pyenv/versions/3.12.0/envs/scratch/lib/python3.12/site-packages/pandas/core/arrays/period.py:341, in PeriodArray._generate_range(cls, start, end, periods, freq, fields)
    339     subarr, freq = _get_ordinal_range(start, end, periods, freq)
    340 elif field_count > 0:
--> 341     subarr, freq = _range_from_fields(freq=freq, **fields)
    342 else:
    343     raise ValueError("Not enough parameters to construct Period range")

File ~/.pyenv/versions/3.12.0/envs/scratch/lib/python3.12/site-packages/pandas/core/arrays/period.py:1249, in _range_from_fields(year, month, quarter, day, hour, minute, second, freq)
   1247     base = libperiod.freq_to_dtype_code(freq)
   1248     if base != FreqGroup.FR_QTR.value:
-> 1249         raise AssertionError("base must equal FR_QTR")
   1251 freqstr = freq.freqstr
   1252 year, quarter = _make_field_arrays(year, quarter)

AssertionError: base must equal FR_QTR

Expected Behavior

This is printed:

PeriodIndex(['2014Q3'], dtype='period[Q-FEB]')

Installed Versions

INSTALLED VERSIONS

commit : a60ad39
python : 3.12.0.final.0
python-bits : 64
OS : Darwin
OS-release : 22.1.0
Version : Darwin Kernel Version 22.1.0: Sun Oct 9 20:19:12 PDT 2022; root:xnu-8792.41.9~3/RELEASE_ARM64_T6020
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.1.2
numpy : 1.26.1
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 68.2.2
pip : 23.3.1
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.17.2
pandas_datareader : None
bs4 : 4.12.2
bottleneck : None
dataframe-api-compat: None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.8.0
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.11.3
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugNeeds TriageIssue that has not been reviewed by a pandas team memberPeriodPeriod data type

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions