Skip to content

Commit 84f04d9

Browse files
committed
DOC: adds column/attribute warnings to whatsnew
1 parent b123423 commit 84f04d9

File tree

1 file changed

+58
-2
lines changed

1 file changed

+58
-2
lines changed

doc/source/whatsnew/v0.21.0.txt

+58-2
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,12 @@ 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
3231
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3332

34-
The `:meth:`~DataFrame.infer_objects` and :meth:`~Series.infer_objects`
33+
The :meth:`~DataFrame.infer_objects` and :meth:`~Series.infer_objects`
3534
methods have been added to perform dtype inference on object columns, replacing
3635
some of the functionality of the deprecated ``convert_objects``
3736
method. See the documentation :ref:`here <basics.object_conversion>`
@@ -57,6 +56,63 @@ using :func:`to_numeric` function (or :func:`to_datetime`, :func:`to_timedelta`)
5756
df['C'] = pd.to_numeric(df['C'], errors='coerce')
5857
df.dtypes
5958

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

62118
Other Enhancements

0 commit comments

Comments
 (0)