Skip to content

Commit cbc30a2

Browse files
committed
DOC: adds column/attribute warnings to whatsnew
1 parent 8738103 commit cbc30a2

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed

doc/source/whatsnew/v0.21.0.txt

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ 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-
2928
.. _whatsnew_0210.enhancements.infer_objects:
3029

3130
``infer_objects`` type conversion
@@ -58,6 +57,63 @@ using the :func:`to_numeric` function (or :func:`to_datetime`, :func:`to_timedel
5857
df['C'] = pd.to_numeric(df['C'], errors='coerce')
5958
df.dtypes
6059

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

63119
Other Enhancements

0 commit comments

Comments
 (0)