You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/source/indexing.rst
+31-4Lines changed: 31 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -227,10 +227,6 @@ as an attribute:
227
227
dfa.A
228
228
panel.one
229
229
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
-
234
230
.. ipython:: python
235
231
236
232
sa.a =5
@@ -267,6 +263,37 @@ You can also assign a ``dict`` to a row of a ``DataFrame``:
267
263
x.iloc[1] =dict(x=9, y=99)
268
264
x
269
265
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
@@ -70,7 +70,7 @@ of this confusion include attempting to create a new column by setting into an a
70
70
In[1]: df = pd.DataFrame({'one': [1., 2., 3.]})
71
71
In[2]: df.two = [4, 5, 6]
72
72
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:
74
74
75
75
.. code-block:: ipython
76
76
@@ -81,14 +81,14 @@ which does not raise any obvious exceptions, but also does not create a new colu
81
81
1 2.0
82
82
2 3.0
83
83
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:
86
86
87
87
.. code-block:: ipython
88
88
89
89
In[4]: df['sum'] = [5., 7., 9.]
90
90
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:
92
92
93
93
.. code-block:: ipython
94
94
@@ -99,19 +99,7 @@ which does not permit that column to be accessed as an attribute:
99
99
1 2.0 7.0
100
100
2 3.0 9.0>
101
101
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>`__.
0 commit comments