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-#1775: Add support for callable in loc/iloc #1776

Merged
merged 1 commit into from
Jul 24, 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
4 changes: 4 additions & 0 deletions modin/pandas/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ class _LocIndexer(_LocationIndexerBase):
"""An indexer for modin_df.loc[] functionality"""

def __getitem__(self, key):
if callable(key):
return self.__getitem__(key(self.df))
row_loc, col_loc, ndim, self.row_scaler, self.col_scaler = _parse_tuple(key)
if isinstance(row_loc, slice) and row_loc == slice(None):
# If we're only slicing columns, handle the case with `__getitem__`
Expand Down Expand Up @@ -361,6 +363,8 @@ class _iLocIndexer(_LocationIndexerBase):
"""An indexer for modin_df.iloc[] functionality"""

def __getitem__(self, key):
if callable(key):
return self.__getitem__(key(self.df))
row_loc, col_loc, ndim, self.row_scaler, self.col_scaler = _parse_tuple(key)
self._check_dtypes(row_loc)
self._check_dtypes(col_loc)
Expand Down
12 changes: 12 additions & 0 deletions modin/pandas/test/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -4394,6 +4394,12 @@ def test_iloc(self, request, data):
modin_df.iloc[:, 0] = modin_df.iloc[:, 1]
pandas_df.iloc[:, 0] = pandas_df.iloc[:, 1]
df_equals(modin_df, pandas_df)

# From issue #1775
df_equals(
modin_df.iloc[lambda df: df.index.get_indexer_for(df.index[:5])],
pandas_df.iloc[lambda df: df.index.get_indexer_for(df.index[:5])],
)
else:
with pytest.raises(IndexError):
modin_df.iloc[0, 1]
Expand Down Expand Up @@ -4489,6 +4495,12 @@ def test_loc(self, request, data):
pandas_df_copy.loc[[1, 2]] = 42
df_equals(modin_df_copy, pandas_df_copy)

# From issue #1775
df_equals(
modin_df.loc[lambda df: df.iloc[:, 0].isin(list(range(1000)))],
pandas_df.loc[lambda df: df.iloc[:, 0].isin(list(range(1000)))],
)

# From issue #1374
with pytest.raises(KeyError):
modin_df.loc["NO_EXIST"]
Expand Down