You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
New users are often flummoxed by the relationship between column operations and attribute
66
+
access on ``DataFrame`` instances (:issue:`5904` & :issue:`7175`). Two specific instances
67
+
of this confusion include attempting to create a new column by setting into an attribute:
68
+
69
+
.. code-block:: ipython
70
+
71
+
In[1]: df = pd.DataFrame({'one': [1., 2., 3.]})
72
+
In[2]: df.two = [4, 5, 6]
73
+
74
+
which does not raise any obvious exceptions, but also does not create a new column:
75
+
76
+
.. code-block:: ipython
77
+
78
+
In[3]: df
79
+
Out[3]:
80
+
one
81
+
0 1.0
82
+
1 2.0
83
+
2 3.0
84
+
85
+
and creating a column whose name collides with a method or attribute already in the instance
86
+
namespace:
87
+
88
+
.. code-block:: ipython
89
+
90
+
In[4]: df['sum'] = [5., 7., 9.]
91
+
92
+
which does not permit that column to be accessed as an attribute:
93
+
94
+
.. code-block:: ipython
95
+
96
+
In[5]: df.sum
97
+
Out[5]:
98
+
<bound method DataFrame.sum of one sum
99
+
0 1.0 5.0
100
+
1 2.0 7.0
101
+
2 3.0 9.0>
102
+
103
+
Both of these now raise a ``UserWarning`` about the potential for unexpected behavior. Upon executing input 2, you can now expect to see:
104
+
105
+
.. code-block:: ipython
106
+
107
+
In[2]: df.two = [4, 5, 6]
108
+
UserWarning: Pandas doesn't allow Series to be assigned into nonexistent columns - see https://pandas.pydata.org/pandas-docs/stable/indexing.html#attribute-access
109
+
110
+
and the example in input 4 will now produce:
111
+
112
+
.. code-block:: ipython
113
+
114
+
In[4]: df['sum'] = [5., 7., 9.]
115
+
UserWarning: Column name 'sum' collides with a built-in method, which will cause unexpected attribute behavior
0 commit comments