diff --git a/doc/source/whatsnew/v3.1.0.rst b/doc/source/whatsnew/v3.1.0.rst index 7d286cd7493ae..2652f135df902 100644 --- a/doc/source/whatsnew/v3.1.0.rst +++ b/doc/source/whatsnew/v3.1.0.rst @@ -563,6 +563,7 @@ Period - Bug in :meth:`Period.strftime` where unknown format directives (e.g. ``"%Q"``) silently produced platform-dependent output and crashed the Python process on Windows; an ``Invalid format string`` ``ValueError`` is now raised on all platforms (:issue:`53562`) - Bug in :meth:`Period.to_timestamp` and :meth:`PeriodIndex.to_timestamp` returning incorrect timestamps when the target frequency normalized to nanoseconds (e.g. ``"1ns"``) or when converting a nanosecond ``Period`` to a coarser target frequency (:issue:`63760`) - Bug in :meth:`Period.to_timestamp` and :meth:`PeriodIndex.to_timestamp` with ``how="end"`` losing nanosecond precision when the target frequency normalized to nanoseconds (e.g. ``"1ns"``); the target frequency is now also validated when ``how="end"`` (:issue:`63760`) +- Bug in :meth:`PeriodIndex.from_fields` raising ``AssertionError`` for a quarterly ``freq`` not anchored on December (e.g. ``QuarterEnd(startingMonth=2)``), even though the equivalent scalar :class:`Period` works (:issue:`55784`) - Plotting diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index 685ad369e9e02..18ce80184bf2e 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -1586,7 +1586,7 @@ def _range_from_fields( else: freq = to_offset(freq, is_period=True) base = libperiod.freq_to_dtype_code(freq) - if base != FreqGroup.FR_QTR.value: + if FreqGroup.from_period_dtype_code(base) != FreqGroup.FR_QTR: raise AssertionError("base must equal FR_QTR") freqstr = freq.freqstr diff --git a/pandas/tests/indexes/period/test_constructors.py b/pandas/tests/indexes/period/test_constructors.py index 1506db70825c3..577a14056c035 100644 --- a/pandas/tests/indexes/period/test_constructors.py +++ b/pandas/tests/indexes/period/test_constructors.py @@ -212,6 +212,16 @@ def test_constructor_field_arrays(self): exp = period_range("2007-01", periods=3, freq="M") tm.assert_index_equal(idx, exp) + @pytest.mark.parametrize("starting_month", range(1, 13)) + def test_from_fields_quarterly_non_december_anchor(self, starting_month): + # GH#55784 from_fields rejected any quarterly freq not anchored on + # December, even though the equivalent scalar Period works. + freq = offsets.QuarterEnd(startingMonth=starting_month) + result = PeriodIndex.from_fields(year=[2014], quarter=[3], freq=freq) + expected = Period(year=2014, quarter=3, freq=freq) + assert result[0] == expected + assert result.dtype == PeriodDtype(freq) + def test_constructor_nano(self): idx = period_range( start=Period(ordinal=1, freq="ns"),