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