Skip to content

Commit 6216954

Browse files
committed
DOC: adds column/attribute warnings to whatsnew
1 parent 1533748 commit 6216954

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

Diff for: doc/source/whatsnew/v0.21.0.txt

+57
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,63 @@ New features
2525
- Added ``__fspath__`` method to :class:`~pandas.HDFStore`, :class:`~pandas.ExcelFile`,
2626
and :class:`~pandas.ExcelWriter` to work properly with the file system path protocol (:issue:`13823`)
2727

28+
.. _whatsnew_0210.enhancements.column_creation:
29+
30+
Improved warnings when attempting to create columns
31+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
32+
33+
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
84+
2885
.. _whatsnew_0210.enhancements.other:
2986

3087
Other Enhancements

0 commit comments

Comments
 (0)