Skip to content

Commit 86e1a4e

Browse files
FIX-#1775: Fix support for callable in loc/iloc (#1776)
Signed-off-by: Devin Petersohn <devin.petersohn@gmail.com>
1 parent 49f462c commit 86e1a4e

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

modin/pandas/indexing.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ class _LocIndexer(_LocationIndexerBase):
236236
"""An indexer for modin_df.loc[] functionality"""
237237

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

363365
def __getitem__(self, key):
366+
if callable(key):
367+
return self.__getitem__(key(self.df))
364368
row_loc, col_loc, ndim, self.row_scaler, self.col_scaler = _parse_tuple(key)
365369
self._check_dtypes(row_loc)
366370
self._check_dtypes(col_loc)

modin/pandas/test/test_dataframe.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4389,6 +4389,12 @@ def test_iloc(self, request, data):
43894389
modin_df.iloc[:, 0] = modin_df.iloc[:, 1]
43904390
pandas_df.iloc[:, 0] = pandas_df.iloc[:, 1]
43914391
df_equals(modin_df, pandas_df)
4392+
4393+
# From issue #1775
4394+
df_equals(
4395+
modin_df.iloc[lambda df: df.index.get_indexer_for(df.index[:5])],
4396+
pandas_df.iloc[lambda df: df.index.get_indexer_for(df.index[:5])],
4397+
)
43924398
else:
43934399
with pytest.raises(IndexError):
43944400
modin_df.iloc[0, 1]
@@ -4484,6 +4490,12 @@ def test_loc(self, request, data):
44844490
pandas_df_copy.loc[[1, 2]] = 42
44854491
df_equals(modin_df_copy, pandas_df_copy)
44864492

4493+
# From issue #1775
4494+
df_equals(
4495+
modin_df.loc[lambda df: df.iloc[:, 0].isin(list(range(1000)))],
4496+
pandas_df.loc[lambda df: df.iloc[:, 0].isin(list(range(1000)))],
4497+
)
4498+
44874499
# From issue #1374
44884500
with pytest.raises(KeyError):
44894501
modin_df.loc["NO_EXIST"]

0 commit comments

Comments
 (0)