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
34
+
access on ``DataFrame`` instances (:issue:`5904` & :issue:`7175`). Two specific instances
35
+
of this confusion include attempting to create a new column by setting into an attribute:
36
+
37
+
.. code-block:: ipython
38
+
39
+
In[1]: df = pd.DataFrame({'one': [1., 2., 3.]})
40
+
In[2]: df.two = [4, 5, 6]
41
+
42
+
which does not raise any obvious exceptions, but also does not create a new column:
43
+
44
+
.. code-block:: ipython
45
+
46
+
In[3]: df
47
+
Out[3]:
48
+
one
49
+
0 1.0
50
+
1 2.0
51
+
2 3.0
52
+
53
+
and creating a column whose name collides with a method or attribute already in the instance
54
+
namespace:
55
+
56
+
.. code-block:: ipython
57
+
58
+
In[4]: df['sum'] = [5., 7., 9.]
59
+
60
+
which does not permit that column to be accessed as an attribute:
61
+
62
+
.. code-block:: ipython
63
+
64
+
In[5]: df.sum
65
+
Out[5]:
66
+
<bound method DataFrame.sum of one sum
67
+
0 1.0 5.0
68
+
1 2.0 7.0
69
+
2 3.0 9.0>
70
+
71
+
Both of these now raise a ``UserWarning`` about the potential for unexpected behavior. Upon executing input 2, you can now expect to see:
72
+
73
+
.. code-block:: ipython
74
+
75
+
In[2]: df.two = [4, 5, 6]
76
+
UserWarning: Pandas doesn't allow Series to be assigned into nonexistent columns - see https://pandas.pydata.org/pandas-docs/stable/indexing.html#attribute-access
77
+
78
+
and the example in input 4 will now produce:
79
+
80
+
.. code-block:: ipython
81
+
82
+
In[4]: df['sum'] = [5., 7., 9.]
83
+
UserWarning: Column name 'sum' collides with a built-in method, which will cause unexpected attribute behavior
0 commit comments