Skip to content

Commit f3fdd19

Browse files
committed
DOC: moves examples from whatsnew to indexing
1 parent 562c164 commit f3fdd19

File tree

2 files changed

+37
-22
lines changed

2 files changed

+37
-22
lines changed

doc/source/indexing.rst

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,6 @@ as an attribute:
227227
dfa.A
228228
panel.one
229229
230-
You can use attribute access to modify an existing element of a Series or column of a DataFrame, but be careful;
231-
if you try to use attribute access to create a new column, it creates a new attribute rather than a
232-
new column. This behavior will incur a ``UserWarning`` in 0.21.0 and later.
233-
234230
.. ipython:: python
235231
236232
sa.a = 5
@@ -267,6 +263,37 @@ You can also assign a ``dict`` to a row of a ``DataFrame``:
267263
x.iloc[1] = dict(x=9, y=99)
268264
x
269265
266+
You can use attribute access to modify an existing element of a Series or column of a DataFrame, but be careful;
267+
if you try to use attribute access to create a new column, it creates a new attribute rather than a
268+
new column. In 0.21.0 and later, this will raise a ``UserWarning``:
269+
270+
.. code-block:: ipython
271+
272+
In[1]: df = pd.DataFrame({'one': [1., 2., 3.]})
273+
In[2]: df.two = [4, 5, 6]
274+
UserWarning: Pandas doesn't allow Series to be assigned into nonexistent columns - see https://pandas.pydata.org/pandas-docs/stable/indexing.html#attribute-access
275+
In[3]: df
276+
Out[3]:
277+
one
278+
0 1.0
279+
1 2.0
280+
2 3.0
281+
282+
Similarly, it is possible to create a column with a name which collides with one of Pandas's
283+
built-in methods or attributes, which can cause confusion later when attempting to access
284+
that column as an attribute. This behavior now warns:
285+
286+
.. code-block:: ipython
287+
288+
In[4]: df['sum'] = [5., 7., 9.]
289+
UserWarning: Column name 'sum' collides with a built-in method, which will cause unexpected attribute behavior
290+
In[5]: df.sum
291+
Out[5]:
292+
<bound method DataFrame.sum of one sum
293+
0 1.0 5.0
294+
1 2.0 7.0
295+
2 3.0 9.0>
296+
270297
Slicing ranges
271298
--------------
272299

doc/source/whatsnew/v0.21.0.txt

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ using the :func:`to_numeric` function (or :func:`to_datetime`, :func:`to_timedel
6161
df['C'] = pd.to_numeric(df['C'], errors='coerce')
6262
df.dtypes
6363

64-
.. _whatsnew_0210.enhancements.column_creation:
64+
.. _whatsnew_0210.enhancements.column-creation:
6565

6666
Improved warnings when attempting to create columns
6767
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -75,7 +75,7 @@ of this confusion include attempting to create a new column by setting into an a
7575
In[1]: df = pd.DataFrame({'one': [1., 2., 3.]})
7676
In[2]: df.two = [4, 5, 6]
7777

78-
which does not raise any obvious exceptions, but also does not create a new column:
78+
This does not raise any obvious exceptions, but also does not create a new column:
7979

8080
.. code-block:: ipython
8181

@@ -86,14 +86,14 @@ which does not raise any obvious exceptions, but also does not create a new colu
8686
1 2.0
8787
2 3.0
8888

89-
and creating a column whose name collides with a method or attribute already in the instance
90-
namespace:
89+
The second source of confusion is creating a column whose name collides with a method or
90+
attribute already in the instance namespace:
9191

9292
.. code-block:: ipython
9393

9494
In[4]: df['sum'] = [5., 7., 9.]
9595

96-
which does not permit that column to be accessed as an attribute:
96+
This does not permit that column to be accessed as an attribute:
9797

9898
.. code-block:: ipython
9999

@@ -104,19 +104,7 @@ which does not permit that column to be accessed as an attribute:
104104
1 2.0 7.0
105105
2 3.0 9.0>
106106

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
107+
Both of these now raise a ``UserWarning`` about the potential for unexpected behavior. See `Attribute Access <https://pandas.pydata.org/pandas-docs/stable/indexing.html#attribute-access>`__.
120108

121109
.. _whatsnew_0210.enhancements.other:
122110

0 commit comments

Comments
 (0)