From 436d93d39222cc6413c89921761b5ccf993c6b99 Mon Sep 17 00:00:00 2001 From: Phillip Cloud Date: Fri, 6 Jun 2014 14:38:22 -0400 Subject: [PATCH] API: Period should return NotImplemented as per the Python doc if no comparison is implemented --- doc/source/v0.14.1.txt | 6 ++++ pandas/tests/test_frame.py | 48 +++++++++++++++++++++++++++-- pandas/tseries/period.py | 6 ++-- pandas/tseries/tests/test_period.py | 4 +-- 4 files changed, 55 insertions(+), 9 deletions(-) diff --git a/doc/source/v0.14.1.txt b/doc/source/v0.14.1.txt index 3e06a705487df..89ef5aead027b 100644 --- a/doc/source/v0.14.1.txt +++ b/doc/source/v0.14.1.txt @@ -35,6 +35,10 @@ API changes ``float`` (:issue:`7242`) - `StringMethods`` now work on empty Series (:issue:`7242`) +- ``Period`` objects no longer raise a ``TypeError`` when compared using ``==`` + with another object that *isn't* a ``Period``. See :issue:`7376`. Instead + when comparing a ``Period`` with another object using ``==`` if the other + object isn't a ``Period`` ``False`` is returned. .. _whatsnew_0141.prior_deprecations: @@ -123,3 +127,5 @@ Bug Fixes - Bug where a string column name assignment to a ``DataFrame`` with a ``Float64Index`` raised a ``TypeError`` during a call to ``np.isnan`` (:issue:`7366`). +- Bug where ``NDFrame.replace()`` didn't correctly replace objects with + ``Period`` values (:issue:`7379`). diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index c4475715386b9..7354c57498561 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -8392,6 +8392,52 @@ def test_replace_swapping_bug(self): expect = pd.DataFrame({'a': ['Y', 'N', 'Y']}) tm.assert_frame_equal(res, expect) + def test_replace_period(self): + d = {'fname': + {'out_augmented_AUG_2011.json': pd.Period(year=2011, month=8, freq='M'), + 'out_augmented_JAN_2011.json': pd.Period(year=2011, month=1, freq='M'), + 'out_augmented_MAY_2012.json': pd.Period(year=2012, month=5, freq='M'), + 'out_augmented_SUBSIDY_WEEK.json': pd.Period(year=2011, month=4, freq='M'), + 'out_augmented_AUG_2012.json': pd.Period(year=2012, month=8, freq='M'), + 'out_augmented_MAY_2011.json': pd.Period(year=2011, month=5, freq='M'), + 'out_augmented_SEP_2013.json': pd.Period(year=2013, month=9, freq='M')}} + + df = pd.DataFrame(['out_augmented_AUG_2012.json', + 'out_augmented_SEP_2013.json', + 'out_augmented_SUBSIDY_WEEK.json', + 'out_augmented_MAY_2012.json', + 'out_augmented_MAY_2011.json', + 'out_augmented_AUG_2011.json', + 'out_augmented_JAN_2011.json'], columns=['fname']) + tm.assert_equal(set(df.fname.values), set(d['fname'].keys())) + expected = DataFrame({'fname': [d['fname'][k] + for k in df.fname.values]}) + result = df.replace(d) + tm.assert_frame_equal(result, expected) + + def test_replace_datetime(self): + d = {'fname': + {'out_augmented_AUG_2011.json': pd.Timestamp('2011/08'), + 'out_augmented_JAN_2011.json': pd.Timestamp('2011/01'), + 'out_augmented_MAY_2012.json': pd.Timestamp('2012/05'), + 'out_augmented_SUBSIDY_WEEK.json': pd.Timestamp('2011/04'), + 'out_augmented_AUG_2012.json': pd.Timestamp('2012/08'), + 'out_augmented_MAY_2011.json': pd.Timestamp('2011/05'), + 'out_augmented_SEP_2013.json': pd.Timestamp('2013/09')}} + + df = pd.DataFrame(['out_augmented_AUG_2012.json', + 'out_augmented_SEP_2013.json', + 'out_augmented_SUBSIDY_WEEK.json', + 'out_augmented_MAY_2012.json', + 'out_augmented_MAY_2011.json', + 'out_augmented_AUG_2011.json', + 'out_augmented_JAN_2011.json'], columns=['fname']) + tm.assert_equal(set(df.fname.values), set(d['fname'].keys())) + expected = DataFrame({'fname': [d['fname'][k] + for k in df.fname.values]}) + result = df.replace(d) + tm.assert_frame_equal(result, expected) + def test_combine_multiple_frames_dtypes(self): # GH 2759 @@ -11245,7 +11291,6 @@ def test_rank(self): exp = df.astype(float).rank(1) assert_frame_equal(result, exp) - def test_rank2(self): from datetime import datetime df = DataFrame([[1, 3, 2], [1, 2, 3]]) @@ -11303,7 +11348,6 @@ def test_rank2(self): exp = DataFrame({"a":[ 3.5, 1. , 3.5, 5. , 6. , 7. , 2. ]}) assert_frame_equal(df.rank(), exp) - def test_rank_na_option(self): _skip_if_no_scipy() from scipy.stats import rankdata diff --git a/pandas/tseries/period.py b/pandas/tseries/period.py index 5516c4634ea58..4dc9ff88b328a 100644 --- a/pandas/tseries/period.py +++ b/pandas/tseries/period.py @@ -138,12 +138,10 @@ def __eq__(self, other): raise ValueError("Cannot compare non-conforming periods") return (self.ordinal == other.ordinal and _gfc(self.freq) == _gfc(other.freq)) - else: - raise TypeError(other) - return False + return NotImplemented def __ne__(self, other): - return not self.__eq__(other) + return not self == other def __hash__(self): return hash((self.ordinal, self.freq)) diff --git a/pandas/tseries/tests/test_period.py b/pandas/tseries/tests/test_period.py index 169939c2f288a..81387c3736481 100644 --- a/pandas/tseries/tests/test_period.py +++ b/pandas/tseries/tests/test_period.py @@ -2455,10 +2455,8 @@ def test_equal(self): def test_equal_Raises_Value(self): self.assertRaises(ValueError, self.january1.__eq__, self.day) - def test_equal_Raises_Type(self): - self.assertRaises(TypeError, self.january1.__eq__, 1) - def test_notEqual(self): + self.assertNotEqual(self.january1, 1) self.assertNotEqual(self.january1, self.february) def test_greater(self):