Skip to content

Commit 0c70b9f

Browse files
committed
DOC: Add 2 examples when creating Series and copy is False (pandas-dev#41423).
Original data unchanged when Series is a Copy, but original data is changed when Series is a View.
1 parent f9efd2a commit 0c70b9f

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

pandas/core/series.py

+31-2
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,10 @@ class Series(base.IndexOpsMixin, generic.NDFrame):
223223
name : str, optional
224224
The name to give to the Series.
225225
copy : bool, default False
226-
Copy input data. If False and the Series returns a `view` of the data,
227-
the memory location for the values is shared. If False and the Series returns a `copy`
226+
Copy input data. If False and the Series returns a `copy`
228227
of the data, the memory location for the values is not shared.
228+
If False and the Series returns a `view` on the data,
229+
the memory location for the values is shared.
229230
230231
Examples
231232
--------
@@ -253,6 +254,34 @@ class Series(base.IndexOpsMixin, generic.NDFrame):
253254
Note that the Index is first build with the keys from the dictionary.
254255
After this the Series is reindexed with the given Index values, hence we
255256
get all NaN as a result.
257+
258+
Constructing Series from an array with `copy=False`.
259+
>>> r = [1,2]
260+
>>> ser = pd.Series(r, copy=False)
261+
>>> ser.iloc[0] = 999
262+
>>> r
263+
[1, 2]
264+
>>> ser
265+
0 999
266+
1 2
267+
dtype: int64
268+
269+
The Series returns a `copy` of the original data, so
270+
`r` is unchanged.
271+
272+
Constructing Series from a `numpy.array` with `copy=False`.
273+
>>> r = np.array([1,2])
274+
>>> ser = pd.Series(r, copy=False)
275+
>>> ser.iloc[0] = 999
276+
>>> r
277+
array([999, 2])
278+
>>> ser
279+
0 999
280+
1 2
281+
dtype: int32
282+
283+
The Series returns a `view` on the original data, so
284+
`r` is changed as well.
256285
"""
257286

258287
_typ = "series"

0 commit comments

Comments
 (0)