Skip to content

Commit db77aae

Browse files
committed
DOC: adds column/attribute warnings to whatsnew
1 parent 39ad1f0 commit db77aae

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
@@ -27,7 +27,6 @@ New features
2727
- Added ``skipna`` parameter to :func:`~pandas.api.types.infer_dtype` to
2828
support type inference in the presence of missing values (:issue:`17059`).
2929

30-
3130
.. _whatsnew_0210.enhancements.infer_objects:
3231

3332
``infer_objects`` type conversion
@@ -60,6 +59,63 @@ using the :func:`to_numeric` function (or :func:`to_datetime`, :func:`to_timedel
6059
df['C'] = pd.to_numeric(df['C'], errors='coerce')
6160
df.dtypes
6261

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

65121
Other Enhancements

0 commit comments

Comments
 (0)