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