numeric.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import numpy as np
  2. from pandas._libs import lib
  3. from pandas.core.dtypes.cast import maybe_downcast_to_dtype
  4. from pandas.core.dtypes.common import (
  5. ensure_object, is_datetime_or_timedelta_dtype, is_decimal, is_number,
  6. is_numeric_dtype, is_scalar)
  7. from pandas.core.dtypes.generic import ABCIndexClass, ABCSeries
  8. import pandas as pd
  9. def to_numeric(arg, errors='raise', downcast=None):
  10. """
  11. Convert argument to a numeric type.
  12. The default return dtype is `float64` or `int64`
  13. depending on the data supplied. Use the `downcast` parameter
  14. to obtain other dtypes.
  15. Parameters
  16. ----------
  17. arg : list, tuple, 1-d array, or Series
  18. errors : {'ignore', 'raise', 'coerce'}, default 'raise'
  19. - If 'raise', then invalid parsing will raise an exception
  20. - If 'coerce', then invalid parsing will be set as NaN
  21. - If 'ignore', then invalid parsing will return the input
  22. downcast : {'integer', 'signed', 'unsigned', 'float'} , default None
  23. If not None, and if the data has been successfully cast to a
  24. numerical dtype (or if the data was numeric to begin with),
  25. downcast that resulting data to the smallest numerical dtype
  26. possible according to the following rules:
  27. - 'integer' or 'signed': smallest signed int dtype (min.: np.int8)
  28. - 'unsigned': smallest unsigned int dtype (min.: np.uint8)
  29. - 'float': smallest float dtype (min.: np.float32)
  30. As this behaviour is separate from the core conversion to
  31. numeric values, any errors raised during the downcasting
  32. will be surfaced regardless of the value of the 'errors' input.
  33. In addition, downcasting will only occur if the size
  34. of the resulting data's dtype is strictly larger than
  35. the dtype it is to be cast to, so if none of the dtypes
  36. checked satisfy that specification, no downcasting will be
  37. performed on the data.
  38. .. versionadded:: 0.19.0
  39. Returns
  40. -------
  41. ret : numeric if parsing succeeded.
  42. Return type depends on input. Series if Series, otherwise ndarray
  43. See Also
  44. --------
  45. pandas.DataFrame.astype : Cast argument to a specified dtype.
  46. pandas.to_datetime : Convert argument to datetime.
  47. pandas.to_timedelta : Convert argument to timedelta.
  48. numpy.ndarray.astype : Cast a numpy array to a specified type.
  49. Examples
  50. --------
  51. Take separate series and convert to numeric, coercing when told to
  52. >>> s = pd.Series(['1.0', '2', -3])
  53. >>> pd.to_numeric(s)
  54. 0 1.0
  55. 1 2.0
  56. 2 -3.0
  57. dtype: float64
  58. >>> pd.to_numeric(s, downcast='float')
  59. 0 1.0
  60. 1 2.0
  61. 2 -3.0
  62. dtype: float32
  63. >>> pd.to_numeric(s, downcast='signed')
  64. 0 1
  65. 1 2
  66. 2 -3
  67. dtype: int8
  68. >>> s = pd.Series(['apple', '1.0', '2', -3])
  69. >>> pd.to_numeric(s, errors='ignore')
  70. 0 apple
  71. 1 1.0
  72. 2 2
  73. 3 -3
  74. dtype: object
  75. >>> pd.to_numeric(s, errors='coerce')
  76. 0 NaN
  77. 1 1.0
  78. 2 2.0
  79. 3 -3.0
  80. dtype: float64
  81. """
  82. if downcast not in (None, 'integer', 'signed', 'unsigned', 'float'):
  83. raise ValueError('invalid downcasting method provided')
  84. is_series = False
  85. is_index = False
  86. is_scalars = False
  87. if isinstance(arg, ABCSeries):
  88. is_series = True
  89. values = arg.values
  90. elif isinstance(arg, ABCIndexClass):
  91. is_index = True
  92. values = arg.asi8
  93. if values is None:
  94. values = arg.values
  95. elif isinstance(arg, (list, tuple)):
  96. values = np.array(arg, dtype='O')
  97. elif is_scalar(arg):
  98. if is_decimal(arg):
  99. return float(arg)
  100. if is_number(arg):
  101. return arg
  102. is_scalars = True
  103. values = np.array([arg], dtype='O')
  104. elif getattr(arg, 'ndim', 1) > 1:
  105. raise TypeError('arg must be a list, tuple, 1-d array, or Series')
  106. else:
  107. values = arg
  108. try:
  109. if is_numeric_dtype(values):
  110. pass
  111. elif is_datetime_or_timedelta_dtype(values):
  112. values = values.astype(np.int64)
  113. else:
  114. values = ensure_object(values)
  115. coerce_numeric = False if errors in ('ignore', 'raise') else True
  116. values = lib.maybe_convert_numeric(values, set(),
  117. coerce_numeric=coerce_numeric)
  118. except Exception:
  119. if errors == 'raise':
  120. raise
  121. # attempt downcast only if the data has been successfully converted
  122. # to a numerical dtype and if a downcast method has been specified
  123. if downcast is not None and is_numeric_dtype(values):
  124. typecodes = None
  125. if downcast in ('integer', 'signed'):
  126. typecodes = np.typecodes['Integer']
  127. elif downcast == 'unsigned' and np.min(values) >= 0:
  128. typecodes = np.typecodes['UnsignedInteger']
  129. elif downcast == 'float':
  130. typecodes = np.typecodes['Float']
  131. # pandas support goes only to np.float32,
  132. # as float dtypes smaller than that are
  133. # extremely rare and not well supported
  134. float_32_char = np.dtype(np.float32).char
  135. float_32_ind = typecodes.index(float_32_char)
  136. typecodes = typecodes[float_32_ind:]
  137. if typecodes is not None:
  138. # from smallest to largest
  139. for dtype in typecodes:
  140. if np.dtype(dtype).itemsize <= values.dtype.itemsize:
  141. values = maybe_downcast_to_dtype(values, dtype)
  142. # successful conversion
  143. if values.dtype == dtype:
  144. break
  145. if is_series:
  146. return pd.Series(values, index=arg.index, name=arg.name)
  147. elif is_index:
  148. # because we want to coerce to numeric if possible,
  149. # do not use _shallow_copy_with_infer
  150. return pd.Index(values, name=arg.name)
  151. elif is_scalars:
  152. return values[0]
  153. else:
  154. return values