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