Skip to content

BUG: pd.match not returning passed sentinel #5943

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
Jan 15, 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
1 change: 1 addition & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ Bug Fixes
- Bug in propogating metadata on ``resample`` (:issue:`5862`)
- Fixed string-representation of ``NaT`` to be "NaT" (:issue:`5708`)
- Fixed string-representation for Timestamp to show nanoseconds if present (:issue:`5912`)
- ``pd.match`` not returning passed sentinel

pandas 0.13.0
-------------
Expand Down
12 changes: 10 additions & 2 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import pandas.hashtable as htable
import pandas.compat as compat


def match(to_match, values, na_sentinel=-1):
"""
Compute locations of to_match into values
Expand All @@ -37,7 +36,16 @@ def match(to_match, values, na_sentinel=-1):
values = np.array(values, dtype='O')

f = lambda htype, caster: _match_generic(to_match, values, htype, caster)
return _hashtable_algo(f, values.dtype)
result = _hashtable_algo(f, values.dtype)

if na_sentinel != -1:

# replace but return a numpy array
# use a Series because it handles dtype conversions properly
from pandas.core.series import Series
result = Series(result.ravel()).replace(-1,na_sentinel).values.reshape(result.shape)

return result


def unique(values):
Expand Down
17 changes: 16 additions & 1 deletion pandas/tests/test_algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import pandas.core.algorithms as algos
import pandas.util.testing as tm


class TestMatch(tm.TestCase):
_multiprocess_can_split_ = True

Expand All @@ -20,6 +19,19 @@ def test_ints(self):
expected = np.array([0, 2, 1, 1, 0, 2, -1, 0])
self.assert_(np.array_equal(result, expected))

result = Series(algos.match(to_match, values, np.nan))
expected = Series(np.array([0, 2, 1, 1, 0, 2, np.nan, 0]))
tm.assert_series_equal(result,expected)

s = pd.Series(np.arange(5),dtype=np.float32)
result = algos.match(s, [2,4])
expected = np.array([-1, -1, 0, -1, 1])
self.assert_(np.array_equal(result, expected))

result = Series(algos.match(s, [2,4], np.nan))
expected = Series(np.array([np.nan, np.nan, 0, np.nan, 1]))
tm.assert_series_equal(result,expected)

def test_strings(self):
values = ['foo', 'bar', 'baz']
to_match = ['bar', 'foo', 'qux', 'foo', 'bar', 'baz', 'qux']
Expand All @@ -28,6 +40,9 @@ def test_strings(self):
expected = np.array([1, 0, -1, 0, 1, 2, -1])
self.assert_(np.array_equal(result, expected))

result = Series(algos.match(to_match, values, np.nan))
expected = Series(np.array([1, 0, np.nan, 0, 1, 2, np.nan]))
tm.assert_series_equal(result,expected)

class TestUnique(tm.TestCase):
_multiprocess_can_split_ = True
Expand Down