Skip to content
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

PERF: GH2003 Series.isin for categorical dtypes #20522

Merged
merged 21 commits into from
Apr 25, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/whatsnew/v0.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ Other Enhancements
``SQLAlchemy`` dialects supporting multivalue inserts include: ``mysql``, ``postgresql``, ``sqlite`` and any dialect with ``supports_multivalues_insert``. (:issue:`14315`, :issue:`8953`)
- :func:`read_html` now accepts a ``displayed_only`` keyword argument to controls whether or not hidden elements are parsed (``True`` by default) (:issue:`20027`)
- zip compression is supported via ``compression=zip`` in :func:`DataFrame.to_pickle`, :func:`Series.to_pickle`, :func:`DataFrame.to_csv`, :func:`Series.to_csv`, :func:`DataFrame.to_json`, :func:`Series.to_json`. (:issue:`17778`)
- Performance enhancement for :func:`Series.isin` in the case of categorical dtypes (:issue:`20003`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this to the "Performance Improvements" section? (starts around line 783).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure


.. _whatsnew_0230.api_breaking:

Expand Down
11 changes: 9 additions & 2 deletions pandas/core/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,15 @@ def isin(comps, values):
if not isinstance(values, (ABCIndex, ABCSeries, np.ndarray)):
values = construct_1d_object_array_from_listlike(list(values))

comps, dtype, _ = _ensure_data(comps)
values, _, _ = _ensure_data(values, dtype=dtype)
if not is_categorical_dtype(comps):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reverse this logic here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I am working on asv benchmark

comps, dtype, _ = _ensure_data(comps)
values, _, _ = _ensure_data(values, dtype=dtype)
else:
cats = comps.cat.categories
comps = comps.cat.codes.values
mask = isna(values)
values = cats.get_indexer(values)
values = values[mask | (values >= 0)]

# faster for larger cases to use np.in1d
f = lambda x, y: htable.ismember_object(x, values)
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3507,7 +3507,11 @@ def isin(self, values):
5 False
Name: animal, dtype: bool
"""
result = algorithms.isin(com._values_from_object(self), values)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if the _values_from_object can be moved to algorithms.isin? Then this could just be result = algorithms.isin(self, values)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes let's try to do this, @Ma3aXaKa can you make this change

if is_categorical_dtype(self.dtype):
result = algorithms.isin(self, values)
else:
result = algorithms.isin(com._values_from_object(self), values)

return self._constructor(result, index=self.index).__finalize__(self)

def between(self, left, right, inclusive=True):
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/series/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1255,6 +1255,17 @@ def test_isin_empty(self, empty):
result = s.isin(empty)
tm.assert_series_equal(expected, result)

def test_isin_cats(self):
Copy link
Contributor

@TomAugspurger TomAugspurger Mar 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can go in pandas/tests/categorical/test_algos.py.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK.

s = Series(["a", "b", np.nan]).astype("category")

result = s.isin(["a", np.nan])
expected = Series([True, False, True])
tm.assert_series_equal(expected, result)

result = s.isin(["a", "c"])
expected = Series([True, False, False])
tm.assert_series_equal(expected, result)

def test_timedelta64_analytics(self):
from pandas import date_range

Expand Down