Skip to content

BUG: correct Period comparisons #7379

New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 6, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/source/v0.14.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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`).
48 changes: 46 additions & 2 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]])
Expand Down Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions pandas/tseries/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 1 addition & 3 deletions pandas/tseries/tests/test_period.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down