Skip to content

Commit

Permalink
FIX-#4720: Fix incorrect warning when setting frame.columns or frame …
Browse files Browse the repository at this point in the history
…index (#4721)

Signed-off-by: Brock <jbrockmendel@gmail.com>
  • Loading branch information
jbrockmendel authored and YarShev committed Sep 6, 2022
1 parent 6547987 commit 1cb3070
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion modin/pandas/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -2504,7 +2504,7 @@ def __setattr__(self, key, value):
# `__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 is_list_like(value):
elif is_list_like(value) and key not in ["index", "columns"]:
warnings.warn(
"Modin doesn't allow columns to be created via a new attribute name - see "
+ "https://pandas.pydata.org/pandas-docs/stable/indexing.html#attribute-access",
Expand Down
22 changes: 21 additions & 1 deletion modin/pandas/test/dataframe/test_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@
import pandas
import matplotlib
import modin.pandas as pd
from modin.utils import to_pandas
from modin.utils import (
to_pandas,
get_current_execution,
)
from numpy.testing import assert_array_equal
import io
import warnings

from modin.pandas.test.utils import (
df_equals,
Expand Down Expand Up @@ -1171,3 +1175,19 @@ def test_hasattr_sparse(is_sparse_data):
else create_test_dfs(test_data["float_nan_data"])
)
eval_general(modin_df, pandas_df, lambda df: hasattr(df, "sparse"))


def test_setattr_axes():
# Test that setting .index or .columns does not warn
df = pd.DataFrame([[1, 2], [3, 4]])
with warnings.catch_warnings():
if get_current_execution() != "BaseOnPython":
# In BaseOnPython, setting columns raises a warning because get_axis
# defaults to pandas.
warnings.simplefilter("error")
df.index = ["foo", "bar"]
df.columns = [9, 10]

# Check that ensure_index was called
pandas.testing.assert_index_equal(df.index, pandas.Index(["foo", "bar"]))
pandas.testing.assert_index_equal(df.columns, pandas.Index([9, 10]))

0 comments on commit 1cb3070

Please sign in to comment.