Movatterモバイル変換


[0]ホーム

URL:


Navigation

Table Of Contents

Search

Enter search terms or a module, class or function name.

pandas.to_numeric

pandas.to_numeric(arg,errors='raise',downcast=None)[source]

Convert argument to a numeric type.

Parameters:

arg : list, tuple, 1-d array, or Series

errors : {‘ignore’, ‘raise’, ‘coerce’}, default ‘raise’

  • If ‘raise’, then invalid parsing will raise an exception
  • If ‘coerce’, then invalid parsing will be set as NaN
  • If ‘ignore’, then invalid parsing will return the input

downcast : {‘integer’, ‘signed’, ‘unsigned’, ‘float’} , default None

If not None, and if the data has been successfully cast to anumerical dtype (or if the data was numeric to begin with),downcast that resulting data to the smallest numerical dtypepossible according to the following rules:

  • ‘integer’ or ‘signed’: smallest signed int dtype (min.: np.int8)
  • ‘unsigned’: smallest unsigned int dtype (min.: np.uint8)
  • ‘float’: smallest float dtype (min.: np.float32)

As this behaviour is separate from the core conversion tonumeric values, any errors raised during the downcastingwill be surfaced regardless of the value of the ‘errors’ input.

In addition, downcasting will only occur if the sizeof the resulting data’s dtype is strictly larger thanthe dtype it is to be cast to, so if none of the dtypeschecked satisfy that specification, no downcasting will beperformed on the data.

New in version 0.19.0.

Returns:

ret : numeric if parsing succeeded.

Return type depends on input. Series if Series, otherwise ndarray

Examples

Take separate series and convert to numeric, coercing when told to

>>>importpandasaspd>>>s=pd.Series(['1.0','2',-3])>>>pd.to_numeric(s)0    1.01    2.02   -3.0dtype: float64>>>pd.to_numeric(s,downcast='float')0    1.01    2.02   -3.0dtype: float32>>>pd.to_numeric(s,downcast='signed')0    11    22   -3dtype: int8>>>s=pd.Series(['apple','1.0','2',-3])>>>pd.to_numeric(s,errors='ignore')0    apple1      1.02        23       -3dtype: object>>>pd.to_numeric(s,errors='coerce')0    NaN1    1.02    2.03   -3.0dtype: float64

Navigation

Scroll To Top
[8]ページ先頭

©2009-2025 Movatter.jp