Skip to content

Commit 2c1c0aa

Browse files
committed
DOC: moves examples from whatsnew to indexing
1 parent 84f04d9 commit 2c1c0aa

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
@@ -56,7 +56,7 @@ using :func:`to_numeric` function (or :func:`to_datetime`, :func:`to_timedelta`)
5656
df['C'] = pd.to_numeric(df['C'], errors='coerce')
5757
df.dtypes
5858

59-
.. _whatsnew_0210.enhancements.column_creation:
59+
.. _whatsnew_0210.enhancements.column-creation:
6060

6161
Improved warnings when attempting to create columns
6262
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -70,7 +70,7 @@ of this confusion include attempting to create a new column by setting into an a
7070
In[1]: df = pd.DataFrame({'one': [1., 2., 3.]})
7171
In[2]: df.two = [4, 5, 6]
7272

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

7575
.. code-block:: ipython
7676

@@ -81,14 +81,14 @@ which does not raise any obvious exceptions, but also does not create a new colu
8181
1 2.0
8282
2 3.0
8383

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

8787
.. code-block:: ipython
8888

8989
In[4]: df['sum'] = [5., 7., 9.]
9090

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

9393
.. code-block:: ipython
9494

@@ -99,19 +99,7 @@ which does not permit that column to be accessed as an attribute:
9999
1 2.0 7.0
100100
2 3.0 9.0>
101101

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
102+
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>`__.
115103

116104
.. _whatsnew_0210.enhancements.other:
117105

0 commit comments

Comments
 (0)