Skip to content

Commit 562c164

Browse files
committed
DOC: adds column/attribute warnings to whatsnew
1 parent 02cb426 commit 562c164

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

32-
3332
.. _whatsnew_0210.enhancements.infer_objects:
3433

3534
``infer_objects`` type conversion
@@ -62,6 +61,63 @@ using the :func:`to_numeric` function (or :func:`to_datetime`, :func:`to_timedel
6261
df['C'] = pd.to_numeric(df['C'], errors='coerce')
6362
df.dtypes
6463

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

67123
Other Enhancements

0 commit comments

Comments
 (0)