Skip to content

Commit ef9b818

Browse files
committed
Merge pull request #7379 from cpcloud/replace-no-match-7376
BUG: correct Period comparisons
2 parents f582e98 + 436d93d commit ef9b818

File tree

4 files changed

+55
-9
lines changed

4 files changed

+55
-9
lines changed

doc/source/v0.14.1.txt

+6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ API changes
3535
``float`` (:issue:`7242`)
3636

3737
- `StringMethods`` now work on empty Series (:issue:`7242`)
38+
- ``Period`` objects no longer raise a ``TypeError`` when compared using ``==``
39+
with another object that *isn't* a ``Period``. See :issue:`7376`. Instead
40+
when comparing a ``Period`` with another object using ``==`` if the other
41+
object isn't a ``Period`` ``False`` is returned.
3842

3943
.. _whatsnew_0141.prior_deprecations:
4044

@@ -123,3 +127,5 @@ Bug Fixes
123127
- Bug where a string column name assignment to a ``DataFrame`` with a
124128
``Float64Index`` raised a ``TypeError`` during a call to ``np.isnan``
125129
(:issue:`7366`).
130+
- Bug where ``NDFrame.replace()`` didn't correctly replace objects with
131+
``Period`` values (:issue:`7379`).

pandas/tests/test_frame.py

+46-2
Original file line numberDiff line numberDiff line change
@@ -8392,6 +8392,52 @@ def test_replace_swapping_bug(self):
83928392
expect = pd.DataFrame({'a': ['Y', 'N', 'Y']})
83938393
tm.assert_frame_equal(res, expect)
83948394

8395+
def test_replace_period(self):
8396+
d = {'fname':
8397+
{'out_augmented_AUG_2011.json': pd.Period(year=2011, month=8, freq='M'),
8398+
'out_augmented_JAN_2011.json': pd.Period(year=2011, month=1, freq='M'),
8399+
'out_augmented_MAY_2012.json': pd.Period(year=2012, month=5, freq='M'),
8400+
'out_augmented_SUBSIDY_WEEK.json': pd.Period(year=2011, month=4, freq='M'),
8401+
'out_augmented_AUG_2012.json': pd.Period(year=2012, month=8, freq='M'),
8402+
'out_augmented_MAY_2011.json': pd.Period(year=2011, month=5, freq='M'),
8403+
'out_augmented_SEP_2013.json': pd.Period(year=2013, month=9, freq='M')}}
8404+
8405+
df = pd.DataFrame(['out_augmented_AUG_2012.json',
8406+
'out_augmented_SEP_2013.json',
8407+
'out_augmented_SUBSIDY_WEEK.json',
8408+
'out_augmented_MAY_2012.json',
8409+
'out_augmented_MAY_2011.json',
8410+
'out_augmented_AUG_2011.json',
8411+
'out_augmented_JAN_2011.json'], columns=['fname'])
8412+
tm.assert_equal(set(df.fname.values), set(d['fname'].keys()))
8413+
expected = DataFrame({'fname': [d['fname'][k]
8414+
for k in df.fname.values]})
8415+
result = df.replace(d)
8416+
tm.assert_frame_equal(result, expected)
8417+
8418+
def test_replace_datetime(self):
8419+
d = {'fname':
8420+
{'out_augmented_AUG_2011.json': pd.Timestamp('2011/08'),
8421+
'out_augmented_JAN_2011.json': pd.Timestamp('2011/01'),
8422+
'out_augmented_MAY_2012.json': pd.Timestamp('2012/05'),
8423+
'out_augmented_SUBSIDY_WEEK.json': pd.Timestamp('2011/04'),
8424+
'out_augmented_AUG_2012.json': pd.Timestamp('2012/08'),
8425+
'out_augmented_MAY_2011.json': pd.Timestamp('2011/05'),
8426+
'out_augmented_SEP_2013.json': pd.Timestamp('2013/09')}}
8427+
8428+
df = pd.DataFrame(['out_augmented_AUG_2012.json',
8429+
'out_augmented_SEP_2013.json',
8430+
'out_augmented_SUBSIDY_WEEK.json',
8431+
'out_augmented_MAY_2012.json',
8432+
'out_augmented_MAY_2011.json',
8433+
'out_augmented_AUG_2011.json',
8434+
'out_augmented_JAN_2011.json'], columns=['fname'])
8435+
tm.assert_equal(set(df.fname.values), set(d['fname'].keys()))
8436+
expected = DataFrame({'fname': [d['fname'][k]
8437+
for k in df.fname.values]})
8438+
result = df.replace(d)
8439+
tm.assert_frame_equal(result, expected)
8440+
83958441
def test_combine_multiple_frames_dtypes(self):
83968442

83978443
# GH 2759
@@ -11245,7 +11291,6 @@ def test_rank(self):
1124511291
exp = df.astype(float).rank(1)
1124611292
assert_frame_equal(result, exp)
1124711293

11248-
1124911294
def test_rank2(self):
1125011295
from datetime import datetime
1125111296
df = DataFrame([[1, 3, 2], [1, 2, 3]])
@@ -11303,7 +11348,6 @@ def test_rank2(self):
1130311348
exp = DataFrame({"a":[ 3.5, 1. , 3.5, 5. , 6. , 7. , 2. ]})
1130411349
assert_frame_equal(df.rank(), exp)
1130511350

11306-
1130711351
def test_rank_na_option(self):
1130811352
_skip_if_no_scipy()
1130911353
from scipy.stats import rankdata

pandas/tseries/period.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,10 @@ def __eq__(self, other):
138138
raise ValueError("Cannot compare non-conforming periods")
139139
return (self.ordinal == other.ordinal
140140
and _gfc(self.freq) == _gfc(other.freq))
141-
else:
142-
raise TypeError(other)
143-
return False
141+
return NotImplemented
144142

145143
def __ne__(self, other):
146-
return not self.__eq__(other)
144+
return not self == other
147145

148146
def __hash__(self):
149147
return hash((self.ordinal, self.freq))

pandas/tseries/tests/test_period.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -2455,10 +2455,8 @@ def test_equal(self):
24552455
def test_equal_Raises_Value(self):
24562456
self.assertRaises(ValueError, self.january1.__eq__, self.day)
24572457

2458-
def test_equal_Raises_Type(self):
2459-
self.assertRaises(TypeError, self.january1.__eq__, 1)
2460-
24612458
def test_notEqual(self):
2459+
self.assertNotEqual(self.january1, 1)
24622460
self.assertNotEqual(self.january1, self.february)
24632461

24642462
def test_greater(self):

0 commit comments

Comments
 (0)