Skip to content

Commit a55fe00

Browse files
DOC: fix to_numeric docstring - add Index/scalar return types, fix dtype_backend description (#65371)
1 parent f05cbcc commit a55fe00

1 file changed

Lines changed: 24 additions & 5 deletions

File tree

pandas/core/tools/numeric.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def to_numeric(
7272
7373
Parameters
7474
----------
75-
arg : scalar, list, tuple, 1-d array, or Series
75+
arg : scalar, list, tuple, 1-d array, Index, or Series
7676
Argument to be converted.
7777
7878
errors : {'raise', 'coerce'}, default 'raise'
@@ -101,7 +101,7 @@ def to_numeric(
101101
performed on the data.
102102
103103
dtype_backend : {'numpy_nullable', 'pyarrow'}
104-
Back-end data type applied to the resultant :class:`DataFrame`
104+
Back-end data type applied to the result
105105
(still experimental). If not specified, the default behavior
106106
is to not use nullable data types. If specified, the behavior
107107
is as follows:
@@ -113,9 +113,16 @@ def to_numeric(
113113
114114
Returns
115115
-------
116-
ret
117-
Numeric if parsing succeeded.
118-
Return type depends on input. Series if Series, otherwise ndarray.
116+
scalar, Series, or Index
117+
For numeric scalars the type is preserved, whereas other scalars
118+
return :class:`int` if possible, otherwise :class:`float`.
119+
For 1-d: :class:`Series` if Series, :class:`Index` if Index,
120+
otherwise an array as specified by ``dtype_backend``.
121+
122+
If ``errors='coerce'``, un-parseable values become ``NaN``
123+
(or ``pd.NA`` if ``dtype_backend`` is not the default).
124+
If ``errors='raise'`` (default),
125+
a :exc:`ValueError` is raised for invalid values.
119126
120127
Raises
121128
------
@@ -175,6 +182,18 @@ def to_numeric(
175182
1 2.1
176183
2 3.0
177184
dtype: Float32
185+
186+
Scalar input returns a scalar:
187+
188+
>>> pd.to_numeric("42")
189+
np.int64(42)
190+
>>> pd.to_numeric("3.14")
191+
np.float64(3.14)
192+
193+
Index input returns an Index:
194+
195+
>>> pd.to_numeric(pd.Index(["1", "2", "3"]))
196+
Index([1, 2, 3], dtype='int64')
178197
"""
179198
if downcast not in (None, "integer", "signed", "unsigned", "float"):
180199
raise ValueError("invalid downcasting method provided")

0 commit comments

Comments
 (0)