Skip to content

Commit

Permalink
FIX-#5380: Fix warning about setting _cache attribute. (#5381)
Browse files Browse the repository at this point in the history
Signed-off-by: mvashishtha <mahesh@ponder.io>
  • Loading branch information
mvashishtha authored Dec 8, 2022
1 parent 219edb5 commit 8da4342
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
8 changes: 5 additions & 3 deletions modin/pandas/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
from_pandas,
from_non_pandas,
broadcast_item,
SET_DATAFRAME_ATTRIBUTE_WARNING,
)
from . import _update_engine
from .iterator import PartitionIterator
Expand Down Expand Up @@ -2662,7 +2663,9 @@ def __setattr__(self, key, value):
# - `_query_compiler`, which Modin initializes before it appears in
# __dict__
# - `_siblings`, which Modin initializes before it appears in __dict__
if key in ["_query_compiler", "_siblings"] or key in self.__dict__:
# - `_cache`, which pandas.cache_readonly uses to cache properties
# before it appears in __dict__.
if key in ("_query_compiler", "_siblings", "_cache") or key in self.__dict__:
pass
elif key in self and key not in dir(self):
self.__setitem__(key, value)
Expand All @@ -2672,8 +2675,7 @@ def __setattr__(self, key, value):
return
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",
SET_DATAFRAME_ATTRIBUTE_WARNING,
UserWarning,
)
object.__setattr__(self, key, value)
Expand Down
15 changes: 11 additions & 4 deletions modin/pandas/test/dataframe/test_iter.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
create_test_dfs,
test_data,
)
from modin.pandas.utils import SET_DATAFRAME_ATTRIBUTE_WARNING
from modin.config import NPartitions
from modin.test.test_utils import warns_that_defaulting_to_pandas

Expand Down Expand Up @@ -232,6 +233,14 @@ def test___repr__():
assert repr(pandas_df) == repr(modin_df)


def test___repr__does_not_raise_attribute_column_warning():
# See https://github.com/modin-project/modin/issues/5380
df = pd.DataFrame([1])
with warnings.catch_warnings():
warnings.filterwarnings(action="error", message=SET_DATAFRAME_ATTRIBUTE_WARNING)
repr(df)


@pytest.mark.parametrize("data", test_data_values, ids=test_data_keys)
def test_inplace_series_ops(data):
pandas_df = pandas.DataFrame(data)
Expand Down Expand Up @@ -294,16 +303,14 @@ def test___setattr__mutating_column():
# and adds the provided list as a new attribute and not a column.
with pytest.warns(
UserWarning,
match="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",
match=SET_DATAFRAME_ATTRIBUTE_WARNING,
):
modin_df.col1 = [4]

with warnings.catch_warnings():
warnings.filterwarnings(
action="error",
message="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",
message=SET_DATAFRAME_ATTRIBUTE_WARNING,
)
modin_df.col1 = [5]
modin_df.new_attr = 6
Expand Down
5 changes: 5 additions & 0 deletions modin/pandas/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
{returns}
"""

SET_DATAFRAME_ATTRIBUTE_WARNING = (
"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"
)


def from_non_pandas(df, index, columns, dtype):
"""
Expand Down

0 comments on commit 8da4342

Please sign in to comment.