Skip to content

Commit 50214ba

Browse files
committed
DOC: moves examples from whatsnew to indexing
1 parent db77aae commit 50214ba

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
@@ -59,7 +59,7 @@ using the :func:`to_numeric` function (or :func:`to_datetime`, :func:`to_timedel
5959
df['C'] = pd.to_numeric(df['C'], errors='coerce')
6060
df.dtypes
6161

62-
.. _whatsnew_0210.enhancements.column_creation:
62+
.. _whatsnew_0210.enhancements.column-creation:
6363

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

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

7878
.. code-block:: ipython
7979

@@ -84,14 +84,14 @@ which does not raise any obvious exceptions, but also does not create a new colu
8484
1 2.0
8585
2 3.0
8686

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

9090
.. code-block:: ipython
9191

9292
In[4]: df['sum'] = [5., 7., 9.]
9393

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

9696
.. code-block:: ipython
9797

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

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
105+
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>`__.
118106

119107
.. _whatsnew_0210.enhancements.other:
120108

0 commit comments

Comments
 (0)