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
@@ -75,7 +75,7 @@ of this confusion include attempting to create a new column by setting into an a
75
75
In[1]: df = pd.DataFrame({'one': [1., 2., 3.]})
76
76
In[2]: df.two = [4, 5, 6]
77
77
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:
79
79
80
80
.. code-block:: ipython
81
81
@@ -86,14 +86,14 @@ which does not raise any obvious exceptions, but also does not create a new colu
86
86
1 2.0
87
87
2 3.0
88
88
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:
91
91
92
92
.. code-block:: ipython
93
93
94
94
In[4]: df['sum'] = [5., 7., 9.]
95
95
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:
97
97
98
98
.. code-block:: ipython
99
99
@@ -104,19 +104,7 @@ which does not permit that column to be accessed as an attribute:
104
104
1 2.0 7.0
105
105
2 3.0 9.0>
106
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
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>`__.
0 commit comments