Skip to content

Commit 00e0f3e

Browse files
sinhrksjreback
authored andcommitted
BUG: Period and Series/Index comparison raises TypeError
Author: sinhrks <[email protected]> Closes #13200 from sinhrks/period_comp and squashes the following commits: aadf669 [sinhrks] BUG: Period and Series/Index comparison raises TypeError
1 parent 20ea406 commit 00e0f3e

File tree

3 files changed

+129
-6
lines changed

3 files changed

+129
-6
lines changed

doc/source/whatsnew/v0.18.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,5 @@ Bug Fixes
149149

150150
- Bug in ``NaT`` - ``Period`` raises ``AttributeError`` (:issue:`13071`)
151151
- Bug in ``Period`` addition raises ``TypeError`` if ``Period`` is on right hand side (:issue:`13069`)
152+
- Bug in ``Peirod`` and ``Series`` or ``Index`` comparison raises ``TypeError`` (:issue:`13200`)
152153
- Bug in ``pd.set_eng_float_format()`` that would prevent NaN's from formatting (:issue:`11981`)

pandas/src/period.pyx

+3
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,9 @@ cdef class Period(object):
772772
if self.ordinal == tslib.iNaT or other.ordinal == tslib.iNaT:
773773
return _nat_scalar_rules[op]
774774
return PyObject_RichCompareBool(self.ordinal, other.ordinal, op)
775+
# index/series like
776+
elif hasattr(other, '_typ'):
777+
return NotImplemented
775778
else:
776779
if op == Py_EQ:
777780
return NotImplemented

pandas/tseries/tests/test_period.py

+125-6
Original file line numberDiff line numberDiff line change
@@ -3950,24 +3950,30 @@ def test_pi_pi_comp(self):
39503950

39513951
exp = np.array([False, True, False, False])
39523952
self.assert_numpy_array_equal(base == p, exp)
3953+
self.assert_numpy_array_equal(p == base, exp)
39533954

39543955
exp = np.array([True, False, True, True])
39553956
self.assert_numpy_array_equal(base != p, exp)
3957+
self.assert_numpy_array_equal(p != base, exp)
39563958

39573959
exp = np.array([False, False, True, True])
39583960
self.assert_numpy_array_equal(base > p, exp)
3961+
self.assert_numpy_array_equal(p < base, exp)
39593962

39603963
exp = np.array([True, False, False, False])
39613964
self.assert_numpy_array_equal(base < p, exp)
3965+
self.assert_numpy_array_equal(p > base, exp)
39623966

39633967
exp = np.array([False, True, True, True])
39643968
self.assert_numpy_array_equal(base >= p, exp)
3969+
self.assert_numpy_array_equal(p <= base, exp)
39653970

39663971
exp = np.array([True, True, False, False])
39673972
self.assert_numpy_array_equal(base <= p, exp)
3973+
self.assert_numpy_array_equal(p >= base, exp)
39683974

3969-
idx = PeriodIndex(
3970-
['2011-02', '2011-01', '2011-03', '2011-05'], freq=freq)
3975+
idx = PeriodIndex(['2011-02', '2011-01', '2011-03',
3976+
'2011-05'], freq=freq)
39713977

39723978
exp = np.array([False, False, True, False])
39733979
self.assert_numpy_array_equal(base == idx, exp)
@@ -3992,6 +3998,9 @@ def test_pi_pi_comp(self):
39923998
with tm.assertRaisesRegexp(period.IncompatibleFrequency, msg):
39933999
base <= Period('2011', freq='A')
39944000

4001+
with tm.assertRaisesRegexp(period.IncompatibleFrequency, msg):
4002+
Period('2011', freq='A') >= base
4003+
39954004
with tm.assertRaisesRegexp(ValueError, msg):
39964005
idx = PeriodIndex(['2011', '2012', '2013', '2014'], freq='A')
39974006
base <= idx
@@ -4001,6 +4010,9 @@ def test_pi_pi_comp(self):
40014010
with tm.assertRaisesRegexp(period.IncompatibleFrequency, msg):
40024011
base <= Period('2011', freq='4M')
40034012

4013+
with tm.assertRaisesRegexp(period.IncompatibleFrequency, msg):
4014+
Period('2011', freq='4M') >= base
4015+
40044016
with tm.assertRaisesRegexp(period.IncompatibleFrequency, msg):
40054017
idx = PeriodIndex(['2011', '2012', '2013', '2014'], freq='4M')
40064018
base <= idx
@@ -4013,17 +4025,23 @@ def test_pi_nat_comp(self):
40134025
result = idx1 > Period('2011-02', freq=freq)
40144026
exp = np.array([False, False, False, True])
40154027
self.assert_numpy_array_equal(result, exp)
4028+
result = Period('2011-02', freq=freq) < idx1
4029+
self.assert_numpy_array_equal(result, exp)
40164030

40174031
result = idx1 == Period('NaT', freq=freq)
40184032
exp = np.array([False, False, False, False])
40194033
self.assert_numpy_array_equal(result, exp)
4034+
result = Period('NaT', freq=freq) == idx1
4035+
self.assert_numpy_array_equal(result, exp)
40204036

40214037
result = idx1 != Period('NaT', freq=freq)
40224038
exp = np.array([True, True, True, True])
40234039
self.assert_numpy_array_equal(result, exp)
4040+
result = Period('NaT', freq=freq) != idx1
4041+
self.assert_numpy_array_equal(result, exp)
40244042

4025-
idx2 = PeriodIndex(
4026-
['2011-02', '2011-01', '2011-04', 'NaT'], freq=freq)
4043+
idx2 = PeriodIndex(['2011-02', '2011-01', '2011-04',
4044+
'NaT'], freq=freq)
40274045
result = idx1 < idx2
40284046
exp = np.array([True, False, False, False])
40294047
self.assert_numpy_array_equal(result, exp)
@@ -4044,11 +4062,12 @@ def test_pi_nat_comp(self):
40444062
exp = np.array([False, False, True, False])
40454063
self.assert_numpy_array_equal(result, exp)
40464064

4047-
diff = PeriodIndex(
4048-
['2011-02', '2011-01', '2011-04', 'NaT'], freq='4M')
4065+
diff = PeriodIndex(['2011-02', '2011-01', '2011-04',
4066+
'NaT'], freq='4M')
40494067
msg = "Input has different freq=4M from PeriodIndex"
40504068
with tm.assertRaisesRegexp(period.IncompatibleFrequency, msg):
40514069
idx1 > diff
4070+
40524071
with tm.assertRaisesRegexp(period.IncompatibleFrequency, msg):
40534072
idx1 == diff
40544073

@@ -4185,6 +4204,106 @@ def test_ops_series_period(self):
41854204
tm.assert_series_equal(s2 - s, exp)
41864205
tm.assert_series_equal(s - s2, -exp)
41874206

4207+
def test_comp_series_period_scalar(self):
4208+
# GH 13200
4209+
for freq in ['M', '2M', '3M']:
4210+
base = Series([Period(x, freq=freq) for x in
4211+
['2011-01', '2011-02', '2011-03', '2011-04']])
4212+
p = Period('2011-02', freq=freq)
4213+
4214+
exp = pd.Series([False, True, False, False])
4215+
tm.assert_series_equal(base == p, exp)
4216+
tm.assert_series_equal(p == base, exp)
4217+
4218+
exp = pd.Series([True, False, True, True])
4219+
tm.assert_series_equal(base != p, exp)
4220+
tm.assert_series_equal(p != base, exp)
4221+
4222+
exp = pd.Series([False, False, True, True])
4223+
tm.assert_series_equal(base > p, exp)
4224+
tm.assert_series_equal(p < base, exp)
4225+
4226+
exp = pd.Series([True, False, False, False])
4227+
tm.assert_series_equal(base < p, exp)
4228+
tm.assert_series_equal(p > base, exp)
4229+
4230+
exp = pd.Series([False, True, True, True])
4231+
tm.assert_series_equal(base >= p, exp)
4232+
tm.assert_series_equal(p <= base, exp)
4233+
4234+
exp = pd.Series([True, True, False, False])
4235+
tm.assert_series_equal(base <= p, exp)
4236+
tm.assert_series_equal(p >= base, exp)
4237+
4238+
# different base freq
4239+
msg = "Input has different freq=A-DEC from Period"
4240+
with tm.assertRaisesRegexp(period.IncompatibleFrequency, msg):
4241+
base <= Period('2011', freq='A')
4242+
4243+
with tm.assertRaisesRegexp(period.IncompatibleFrequency, msg):
4244+
Period('2011', freq='A') >= base
4245+
4246+
def test_comp_series_period_series(self):
4247+
# GH 13200
4248+
for freq in ['M', '2M', '3M']:
4249+
base = Series([Period(x, freq=freq) for x in
4250+
['2011-01', '2011-02', '2011-03', '2011-04']])
4251+
4252+
s = Series([Period(x, freq=freq) for x in
4253+
['2011-02', '2011-01', '2011-03', '2011-05']])
4254+
4255+
exp = Series([False, False, True, False])
4256+
tm.assert_series_equal(base == s, exp)
4257+
4258+
exp = Series([True, True, False, True])
4259+
tm.assert_series_equal(base != s, exp)
4260+
4261+
exp = Series([False, True, False, False])
4262+
tm.assert_series_equal(base > s, exp)
4263+
4264+
exp = Series([True, False, False, True])
4265+
tm.assert_series_equal(base < s, exp)
4266+
4267+
exp = Series([False, True, True, False])
4268+
tm.assert_series_equal(base >= s, exp)
4269+
4270+
exp = Series([True, False, True, True])
4271+
tm.assert_series_equal(base <= s, exp)
4272+
4273+
s2 = Series([Period(x, freq='A') for x in
4274+
['2011', '2011', '2011', '2011']])
4275+
4276+
# different base freq
4277+
msg = "Input has different freq=A-DEC from Period"
4278+
with tm.assertRaisesRegexp(period.IncompatibleFrequency, msg):
4279+
base <= s2
4280+
4281+
def test_comp_series_period_object(self):
4282+
# GH 13200
4283+
base = Series([Period('2011', freq='A'), Period('2011-02', freq='M'),
4284+
Period('2013', freq='A'), Period('2011-04', freq='M')])
4285+
4286+
s = Series([Period('2012', freq='A'), Period('2011-01', freq='M'),
4287+
Period('2013', freq='A'), Period('2011-05', freq='M')])
4288+
4289+
exp = Series([False, False, True, False])
4290+
tm.assert_series_equal(base == s, exp)
4291+
4292+
exp = Series([True, True, False, True])
4293+
tm.assert_series_equal(base != s, exp)
4294+
4295+
exp = Series([False, True, False, False])
4296+
tm.assert_series_equal(base > s, exp)
4297+
4298+
exp = Series([True, False, False, True])
4299+
tm.assert_series_equal(base < s, exp)
4300+
4301+
exp = Series([False, True, True, False])
4302+
tm.assert_series_equal(base >= s, exp)
4303+
4304+
exp = Series([True, False, True, True])
4305+
tm.assert_series_equal(base <= s, exp)
4306+
41884307
def test_ops_frame_period(self):
41894308
# GH 13043
41904309
df = pd.DataFrame({'A': [pd.Period('2015-01', freq='M'),

0 commit comments

Comments
 (0)