Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

BUG: correct Period comparisons #7379

Merged
merged 1 commit into from Jun 6, 2014
Jump to file or symbol
Failed to load files and symbols.
+55 −9
Split
View
@@ -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`).
View
@@ -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
View
@@ -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))
@@ -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):