Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: revert collision warning #17298

Merged
merged 5 commits into from
Sep 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions doc/source/indexing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -269,21 +269,6 @@ new column. In 0.21.0 and later, this will raise a ``UserWarning``:
1 2.0
2 3.0

Similarly, it is possible to create a column with a name which collides with one of Pandas's
built-in methods or attributes, which can cause confusion later when attempting to access
that column as an attribute. This behavior now warns:

.. code-block:: ipython

In[4]: df['sum'] = [5., 7., 9.]
UserWarning: Column name 'sum' collides with a built-in method, which will cause unexpected attribute behavior
In[5]: df.sum
Out[5]:
<bound method DataFrame.sum of one sum
0 1.0 5.0
1 2.0 7.0
2 3.0 9.0>

Slicing ranges
--------------

Expand Down
24 changes: 3 additions & 21 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ Improved warnings when attempting to create columns
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

New users are often flummoxed by the relationship between column operations and attribute
access on ``DataFrame`` instances (:issue:`5904` & :issue:`7175`). Two specific instances
of this confusion include attempting to create a new column by setting into an attribute:
access on ``DataFrame`` instances (:issue:`7175`). One specific instance
of this confusion is attempting to create a new column by setting into an attribute:

.. code-block:: ipython

Expand All @@ -86,25 +86,7 @@ This does not raise any obvious exceptions, but also does not create a new colum
1 2.0
2 3.0

The second source of confusion is creating a column whose name collides with a method or
attribute already in the instance namespace:

.. code-block:: ipython

In[4]: df['sum'] = [5., 7., 9.]

This does not permit that column to be accessed as an attribute:

.. code-block:: ipython

In[5]: df.sum
Out[5]:
<bound method DataFrame.sum of one sum
0 1.0 5.0
1 2.0 7.0
2 3.0 9.0>

Both of these now raise a ``UserWarning`` about the potential for unexpected behavior. See :ref:`Attribute Access <indexing.attribute_access>`.
Setting a list-like data structure into a new attribute now raise a ``UserWarning`` about the potential for unexpected behavior. See :ref:`Attribute Access <indexing.attribute_access>`.

.. _whatsnew_0210.enhancements.other:

Expand Down
8 changes: 2 additions & 6 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1905,10 +1905,6 @@ def _slice(self, slobj, axis=0, kind=None):
return result

def _set_item(self, key, value):
if isinstance(key, str) and callable(getattr(self, key, None)):
warnings.warn("Column name '{key}' collides with a built-in "
"method, which will cause unexpected attribute "
"behavior".format(key=key), stacklevel=3)
self._data.set(key, value)
self._clear_item_cache()

Expand Down Expand Up @@ -3441,8 +3437,8 @@ def __setattr__(self, name, value):
object.__setattr__(self, name, value)
except (AttributeError, TypeError):
if isinstance(self, ABCDataFrame) and (is_list_like(value)):
warnings.warn("Pandas doesn't allow Series to be assigned "
"into nonexistent columns - see "
warnings.warn("Pandas doesn't allow columns to be "
"created via a new attribute name - see "
"https://pandas.pydata.org/pandas-docs/"
"stable/indexing.html#attribute-access",
stacklevel=2)
Expand Down
5 changes: 0 additions & 5 deletions pandas/tests/dtypes/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ def test_abc_types(self):


def test_setattr_warnings():
# GH5904 - Suggestion: Warning for DataFrame colname-methodname clash
# GH7175 - GOTCHA: You can't use dot notation to add a column...
d = {'one': pd.Series([1., 2., 3.], index=['a', 'b', 'c']),
'two': pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}
Expand Down Expand Up @@ -78,7 +77,3 @@ def test_setattr_warnings():
# warn when setting column to nonexistent name
df.four = df.two + 2
assert df.four.sum() > df.two.sum()

with tm.assert_produces_warning(UserWarning):
# warn when column has same name as method
df['sum'] = df.two
4 changes: 2 additions & 2 deletions pandas/tests/io/test_pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2011,7 +2011,7 @@ def check(obj, comparator):
df['string'] = 'foo'
df['float322'] = 1.
df['float322'] = df['float322'].astype('float32')
df['boolean'] = df['float322'] > 0
df['bool'] = df['float322'] > 0
df['time1'] = Timestamp('20130101')
df['time2'] = Timestamp('20130102')
check(df, tm.assert_frame_equal)
Expand Down Expand Up @@ -2141,7 +2141,7 @@ def test_table_values_dtypes_roundtrip(self):
df1['string'] = 'foo'
df1['float322'] = 1.
df1['float322'] = df1['float322'].astype('float32')
df1['boolean'] = df1['float32'] > 0
df1['bool'] = df1['float32'] > 0
df1['time1'] = Timestamp('20130101')
df1['time2'] = Timestamp('20130102')

Expand Down