3
���h@ � @ sf d dl Zd dlmZ d dlmZ d dlmZmZm Z m
Z
mZmZ d dl
mZmZ d dlZd dd�ZdS )
� N)�lib)�maybe_downcast_to_dtype)�
ensure_object�is_datetime_or_timedelta_dtype�
is_decimal� is_number�is_numeric_dtype� is_scalar)�
ABCIndexClass� ABCSeries�raisec
C s� |dkrt d��|dkr t d
��d}d}d}t| t�rBd}| j}n�t| t�rfd}| j}|dkr�| j}nvt| ttf�r�tj | d
d�}nXt
| �r�t| �r�t| �S t
| �r�| S d}tj | gd
d�}nt| dd�dkr�td��n| }t|dd�}t|�� r�nft|��r|jtj�}nNt|�}|dk}ytj|t� |d�}W n& t tfk
�rX |dk�rT� Y nX |dk �r<t|j��r<d} |dk�r�tjd } nj|dk�r�t|� �s�tj|�dk�r�tjd } n8|dk�r�tjd } tjtj�j}
| j|
�}| |d� } | dk �r<x<| D ]4}tj|�j|jjk�rt ||�}|j|k�rP �qW |�rVt!j"|| j| j#d�S |�rlt!j$|| j#d�S |�rz|d S |S dS )a�
Convert argument to a numeric type.
The default return dtype is `float64` or `int64`
depending on the data supplied. Use the `downcast` parameter
to obtain other dtypes.
Please note that precision loss may occur if really large numbers
are passed in. Due to the internal limitations of `ndarray`, if
numbers smaller than `-9223372036854775808` (np.iinfo(np.int64).min)
or larger than `18446744073709551615` (np.iinfo(np.uint64).max) are
passed in, it is very likely they will be converted to float so that
they can stored in an `ndarray`. These warnings apply similarly to
`Series` since it internally leverages `ndarray`.
Parameters
----------
arg : scalar, list, tuple, 1-d array, or Series
Argument to be converted.
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 a
numerical dtype (or if the data was numeric to begin with),
downcast that resulting data to the smallest numerical dtype
possible 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 to
numeric values, any errors raised during the downcasting
will be surfaced regardless of the value of the 'errors' input.
In addition, downcasting will only occur if the size
of the resulting data's dtype is strictly larger than
the dtype it is to be cast to, so if none of the dtypes
checked satisfy that specification, no downcasting will be
performed on the data.
Returns
-------
ret
Numeric if parsing succeeded.
Return type depends on input. Series if Series, otherwise ndarray.
See Also
--------
DataFrame.astype : Cast argument to a specified dtype.
to_datetime : Convert argument to datetime.
to_timedelta : Convert argument to timedelta.
numpy.ndarray.astype : Cast a numpy array to a specified type.
DataFrame.convert_dtypes : Convert dtypes.
Examples
--------
Take separate series and convert to numeric, coercing when told to
>>> s = pd.Series(['1.0', '2', -3])
>>> pd.to_numeric(s)
0 1.0
1 2.0
2 -3.0
dtype: float64
>>> pd.to_numeric(s, downcast='float')
0 1.0
1 2.0
2 -3.0
dtype: float32
>>> pd.to_numeric(s, downcast='signed')
0 1
1 2
2 -3
dtype: int8
>>> s = pd.Series(['apple', '1.0', '2', -3])
>>> pd.to_numeric(s, errors='ignore')
0 apple
1 1.0
2 2
3 -3
dtype: object
>>> pd.to_numeric(s, errors='coerce')
0 NaN
1 1.0
2 2.0
3 -3.0
dtype: float64
N�integer�signed�unsigned�floatz#invalid downcasting method provided�ignorer �coercezinvalid error value specifiedFT�O)�dtype�ndim� z/arg must be a list, tuple, 1-d array, or Seriesr )�coerce_numericZIntegerr ZUnsignedIntegerZFloat)�index�name)r )Nr
r r r )r r r )r r )r
r )%�
ValueError�
isinstancer �valuesr
Zasi8�list�tuple�np�arrayr r r r �getattr� TypeErrorr r ZastypeZint64r r Zmaybe_convert_numeric�setr � typecodes�len�minZfloat32�charr �itemsizer �pdZSeriesr �Index)
�arg�errorsZdowncastZ is_seriesZis_indexZ
is_scalarsr Zvalues_dtyper r$ Z
float_32_charZfloat_32_indr � r- �;/tmp/pip-build-5_djhm0z/pandas/pandas/core/tools/numeric.py�
to_numeric s� \
&
r/ )r N)Znumpyr Zpandas._libsr Zpandas.core.dtypes.castr Zpandas.core.dtypes.commonr r r r r r Zpandas.core.dtypes.genericr
r Zpandasr) r/ r- r- r- r. �<module> s |