Skip to content

Commit

Permalink
FIX-modin-project#1556: Fix support for nested assignment with loc/…
Browse files Browse the repository at this point in the history
…`iloc` (modin-project#1788)

Signed-off-by: Devin Petersohn <devin.petersohn@gmail.com>
  • Loading branch information
devin-petersohn authored and aregm committed Sep 16, 2020
1 parent 222b4a9 commit aad9ba2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
15 changes: 9 additions & 6 deletions modin/pandas/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ def _validate_dtypes_min_max(self, axis, numeric_only):
def _validate_dtypes(self, numeric_only=False):
pass

def _update_inplace(self, new_query_compiler):
super(Series, self)._update_inplace(new_query_compiler=new_query_compiler)
# Propagate changes back to parent so that column in dataframe had the same contents
if self._parent is not None:
if self._parent_axis == 0:
self._parent.loc[self.name] = self
else:
self._parent[self.name] = self

def _create_or_update_from_compiler(self, new_query_compiler, inplace=False):
"""Returns or updates a DataFrame given new query_compiler"""
assert (
Expand Down Expand Up @@ -329,12 +338,6 @@ def __setitem__(self, key, value):
self._create_or_update_from_compiler(
self._query_compiler.setitem(1, key, value), inplace=True
)
# Propagate changes back to parent so that column in dataframe had the same contents
if self._parent is not None:
if self._parent_axis == 0:
self._parent.loc[self.name] = self
else:
self._parent[self.name] = self

def __sub__(self, right):
return self.sub(right)
Expand Down
30 changes: 30 additions & 0 deletions modin/pandas/test/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -4593,6 +4593,21 @@ def test_loc_assignment(self):
pandas_df.loc["row3"]["col2"] = 32
df_equals(modin_df, pandas_df)

@pytest.mark.parametrize("data", test_data_values, ids=test_data_keys)
def test_loc_nested_assignment(self, data):
modin_df = pd.DataFrame(data)
pandas_df = pandas.DataFrame(data)
key1 = modin_df.columns[0]
key2 = modin_df.columns[1]

modin_df[key1].loc[0] = 500
pandas_df[key1].loc[0] = 500
df_equals(modin_df, pandas_df)

modin_df[key2].loc[0] = None
pandas_df[key2].loc[0] = None
df_equals(modin_df, pandas_df)

def test_iloc_assignment(self):
modin_df = pd.DataFrame(
index=["row1", "row2", "row3"], columns=["col1", "col2"]
Expand All @@ -4614,6 +4629,21 @@ def test_iloc_assignment(self):
pandas_df.iloc[2]["col2"] = 32
df_equals(modin_df, pandas_df)

@pytest.mark.parametrize("data", test_data_values, ids=test_data_keys)
def test_iloc_nested_assignment(self, data):
modin_df = pd.DataFrame(data)
pandas_df = pandas.DataFrame(data)
key1 = modin_df.columns[0]
key2 = modin_df.columns[1]

modin_df[key1].iloc[0] = 500
pandas_df[key1].iloc[0] = 500
df_equals(modin_df, pandas_df)

modin_df[key2].iloc[0] = None
pandas_df[key2].iloc[0] = None
df_equals(modin_df, pandas_df)

@pytest.mark.parametrize("data", test_data_values, ids=test_data_keys)
def test_pop(self, request, data):
modin_df = pd.DataFrame(data)
Expand Down

0 comments on commit aad9ba2

Please sign in to comment.