From f4315ac859e7e49f50b51653898d755bcd98e954 Mon Sep 17 00:00:00 2001 From: makbigc Date: Sun, 25 Nov 2018 22:21:10 +0800 Subject: [PATCH] Bug fix (GH #23078) --- doc/source/whatsnew/v0.24.0.rst | 1 + pandas/core/arrays/period.py | 3 +++ pandas/tests/indexes/period/test_period.py | 6 ++++++ 3 files changed, 10 insertions(+) diff --git a/doc/source/whatsnew/v0.24.0.rst b/doc/source/whatsnew/v0.24.0.rst index 0bd695f5a40ea1..4feea46dfc3c5f 100644 --- a/doc/source/whatsnew/v0.24.0.rst +++ b/doc/source/whatsnew/v0.24.0.rst @@ -1366,6 +1366,7 @@ Datetimelike - Bug in :class:`DatetimeIndex` where constructing a :class:`DatetimeIndex` from a :class:`Categorical` or :class:`CategoricalIndex` would incorrectly drop timezone information (:issue:`18664`) - Bug in :class:`DatetimeIndex` and :class:`TimedeltaIndex` where indexing with ``Ellipsis`` would incorrectly lose the index's ``freq`` attribute (:issue:`21282`) - Clarified error message produced when passing an incorrect ``freq`` argument to :class:`DatetimeIndex` with ``NaT`` as the first entry in the passed data (:issue:`11587`) +- Bug in :class:`PeriodIndex` when comparing indexes of different lengths, ValueError is not raised (:issue:`23078`) Timedelta ^^^^^^^^^ diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index 60febc5f5636d0..42a4a02923bbd5 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -68,6 +68,9 @@ def wrapper(self, other): elif isinstance(other, cls): self._check_compatible_with(other) + if other.ndim > 0 and len(self) != len(other): + raise ValueError('Lengths must match to compare') + if not_implemented: return NotImplemented result = op(other.asi8) diff --git a/pandas/tests/indexes/period/test_period.py b/pandas/tests/indexes/period/test_period.py index 5d78333016f74a..4cf8fb371e84fc 100644 --- a/pandas/tests/indexes/period/test_period.py +++ b/pandas/tests/indexes/period/test_period.py @@ -561,6 +561,12 @@ def test_insert(self): result = period_range('2017Q1', periods=4, freq='Q').insert(1, na) tm.assert_index_equal(result, expected) + def test_comp_op(self): + # GH 23078 + index = period_range('2017', periods=12, freq="A-DEC") + with pytest.raises(ValueError, match="Lengths must match"): + index <= index[[0]] + def test_maybe_convert_timedelta(): pi = PeriodIndex(['2000', '2001'], freq='D')