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

Fix Series.__getitem__ to accept a callable as key #1084

Merged
merged 2 commits into from
Feb 19, 2020
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
3 changes: 2 additions & 1 deletion modin/pandas/series.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import numpy as np
import pandas
from pandas.core.common import is_bool_indexer
from pandas.core.common import apply_if_callable, is_bool_indexer
from pandas.core.dtypes.common import (
is_dict_like,
is_list_like,
Expand Down Expand Up @@ -163,6 +163,7 @@ def __floordiv__(self, right):
return self.floordiv(right)

def _getitem(self, key):
key = apply_if_callable(key, self)
if isinstance(key, Series) and key.dtype == np.bool:
# This ends up being significantly faster than looping through and getting
# each item individually.
Expand Down
9 changes: 9 additions & 0 deletions modin/pandas/test/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ def test_accessing_index_element_as_property():
_ = s.d


@pytest.mark.parametrize("data", test_data_values, ids=test_data_keys)
def test_callable_key_in_getitem(data):
modin_series, pandas_series = create_test_series(data)
df_equals(
modin_series[lambda s: s.index % 2 == 0],
pandas_series[lambda s: s.index % 2 == 0],
)


@pytest.mark.parametrize("data", test_data_values, ids=test_data_keys)
def test_T(data):
modin_series, pandas_series = create_test_series(data)
Expand Down