Skip to content

Commit

Permalink
FIX-#4577: Set attribute of Modin dataframe to updated value (#4588)
Browse files Browse the repository at this point in the history
Signed-off-by: Karthik Velayutham <vkarthik@ponder.io>
  • Loading branch information
pyrito authored and RehanSD committed Jun 24, 2022
1 parent 40c32df commit 87efb34
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
4 changes: 4 additions & 0 deletions modin/pandas/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -2499,6 +2499,10 @@ def __setattr__(self, key, value):
pass
elif key in self and key not in dir(self):
self.__setitem__(key, value)
# Note: return immediately so we don't keep this `key` as dataframe state.
# `__getattr__` will return the columns not present in `dir(self)`, so we do not need
# to manually track this state in the `dir`.
return
elif isinstance(value, pandas.Series):
warnings.warn(
"Modin doesn't allow columns to be created via a new attribute name - see "
Expand Down
34 changes: 33 additions & 1 deletion modin/pandas/test/dataframe/test_iter.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ def test_inplace_series_ops(data):
df_equals(modin_df, pandas_df)


def test___setattr__():
# Note: Tests setting an attribute that is not an existing column label
def test___setattr__not_column():
pandas_df = pandas.DataFrame([1, 2, 3])
modin_df = pd.DataFrame([1, 2, 3])

Expand All @@ -257,6 +258,37 @@ def test___setattr__():

df_equals(modin_df, pandas_df)

# While `new_col` is not a column of the dataframe,
# it should be accessible with __getattr__.
assert modin_df.new_col == pandas_df.new_col


def test___setattr__mutating_column():
# Use case from issue #4577
pandas_df = pandas.DataFrame([[1]], columns=["col0"])
modin_df = pd.DataFrame([[1]], columns=["col0"])

# Replacing a column with a list should mutate the column in place.
pandas_df.col0 = [3]
modin_df.col0 = [3]

df_equals(modin_df, pandas_df)
# Check that the col0 attribute reflects the value update.
df_equals(modin_df.col0, pandas_df.col0)

pandas_df.col0 = pd.Series([5])
modin_df.col0 = pd.Series([5])

# Check that the col0 attribute reflects this update
df_equals(modin_df, pandas_df)

pandas_df.loc[0, "col0"] = 4
modin_df.loc[0, "col0"] = 4

# Check that the col0 attribute reflects update via loc
df_equals(modin_df, pandas_df)
assert modin_df.col0.equals(modin_df["col0"])


@pytest.mark.parametrize("data", test_data_values, ids=test_data_keys)
def test_isin(data):
Expand Down

0 comments on commit 87efb34

Please sign in to comment.