Skip to content

Commit a189877

Browse files
Backport PR #62179 on branch 2.3.x (DOC: Add change in .values to the string migration guide) (#62199)
Co-authored-by: Richard Shadrach <[email protected]>
1 parent 287fd13 commit a189877

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

doc/source/user_guide/migration-3-strings.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,37 @@ the :meth:`~pandas.Series.str.decode` method now has a ``dtype`` parameter to be
315315
able to specify object dtype instead of the default of string dtype for this use
316316
case.
317317

318+
:meth:`Series.values` now returns an :class:`~pandas.api.extensions.ExtensionArray`
319+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
320+
321+
With object dtype, using ``.values`` on a Series will return the underlying NumPy array.
322+
323+
.. code-block:: python
324+
325+
>>> ser = pd.Series(["a", "b", np.nan], dtype="object")
326+
>>> type(ser.values)
327+
<class 'numpy.ndarray'>
328+
329+
However with the new string dtype, the underlying ExtensionArray is returned instead.
330+
331+
.. code-block:: python
332+
333+
>>> ser = pd.Series(["a", "b", pd.NA], dtype="str")
334+
>>> ser.values
335+
<ArrowStringArray>
336+
['a', 'b', nan]
337+
Length: 3, dtype: str
338+
339+
If your code requires a NumPy array, you should use :meth:`Series.to_numpy`.
340+
341+
.. code-block:: python
342+
343+
>>> ser = pd.Series(["a", "b", pd.NA], dtype="str")
344+
>>> ser.to_numpy()
345+
['a' 'b' nan]
346+
347+
In general, you should always prefer :meth:`Series.to_numpy` to get a NumPy array or :meth:`Series.array` to get an ExtensionArray over using :meth:`Series.values`.
348+
318349
Notable bug fixes
319350
~~~~~~~~~~~~~~~~~
320351

0 commit comments

Comments
 (0)