From aad9ba289cb18d893c92fe54fc2ce7b81c201cfa Mon Sep 17 00:00:00 2001 From: Devin Petersohn Date: Fri, 24 Jul 2020 08:52:14 -0700 Subject: [PATCH] FIX-#1556: Fix support for nested assignment with `loc`/`iloc` (#1788) Signed-off-by: Devin Petersohn --- modin/pandas/series.py | 15 +++++++++------ modin/pandas/test/test_dataframe.py | 30 +++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/modin/pandas/series.py b/modin/pandas/series.py index 5c2ef11d840..fae48fc14cb 100644 --- a/modin/pandas/series.py +++ b/modin/pandas/series.py @@ -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 ( @@ -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) diff --git a/modin/pandas/test/test_dataframe.py b/modin/pandas/test/test_dataframe.py index a18f3557cdd..6484c908732 100644 --- a/modin/pandas/test/test_dataframe.py +++ b/modin/pandas/test/test_dataframe.py @@ -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"] @@ -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)