Skip to content

Commit 9bca2c9

Browse files
committed
Bug fix (GH #23078)
1 parent 159772d commit 9bca2c9

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

doc/source/whatsnew/v0.24.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1319,6 +1319,7 @@ Datetimelike
13191319
- Bug in :class:`DatetimeIndex` and :class:`TimedeltaIndex` where indexing with ``Ellipsis`` would incorrectly lose the index's ``freq`` attribute (:issue:`21282`)
13201320
- 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`)
13211321
- Bug in :func:`to_datetime` where ``box`` and ``utc`` arguments were ignored when passing a :class:`DataFrame` or ``dict`` of unit mappings (:issue:`23760`)
1322+
- Bug in :class:`PeriodIndex` when comparing indexes of different lengths, ValueError is not raised (:issue:`23078`)
13221323

13231324
Timedelta
13241325
^^^^^^^^^

pandas/core/arrays/period.py

+3
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ def wrapper(self, other):
6767
elif isinstance(other, cls):
6868
self._check_compatible_with(other)
6969

70+
if other.ndim > 0 and len(self) != len(other):
71+
raise ValueError('Lengths must match to compare')
72+
7073
if not_implemented:
7174
return NotImplemented
7275
result = op(other.asi8)

pandas/tests/indexes/period/test_period.py

+6
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,12 @@ def test_insert(self):
555555
result = period_range('2017Q1', periods=4, freq='Q').insert(1, na)
556556
tm.assert_index_equal(result, expected)
557557

558+
def test_comp_op(self):
559+
# GH 23078
560+
index = period_range('2017', periods=12, freq="A-DEC")
561+
with pytest.raises(ValueError, match="Lengths must match"):
562+
index <= index[[0]]
563+
558564

559565
def test_maybe_convert_timedelta():
560566
pi = PeriodIndex(['2000', '2001'], freq='D')

0 commit comments

Comments
 (0)