U
    MZf(                     @  s   d dl mZ d dlmZ d dlZd dlmZ d dlm	Z	m
Z
mZ d dlmZ d dlmZ d dlmZmZmZmZmZmZmZmZmZmZ d d	lmZmZ d dlZd d
l m!Z! d dl"m#Z# ddej$fddddddZ%dS )    )annotations)LiteralN)lib)DateTimeErrorChoicesDtypeBackendnpt)check_dtype_backend)maybe_downcast_numeric)
ensure_objectis_bool_dtypeis_datetime_or_timedelta_dtype
is_decimalis_integer_dtype	is_numberis_numeric_dtype	is_scalaris_string_dtypeneeds_i8_conversion)ABCIndex	ABCSeries)BaseMaskedArray)StringDtyperaiser   z:Literal[('integer', 'signed', 'unsigned', 'float')] | NonezDtypeBackend | lib.NoDefault)errorsdowncastdtype_backendc              	   C  s2  |dkrt d|dkr t dt| d}d}d}t| trJd}| j}nt| trvd}t| jrn| d}q| j}nvt| t	t
frtj| dd	}nXt| rt| rt| S t| r| S d}tj| gdd	}nt| d
ddkrtdn| }|}d}	t|tr|j}	|j|	  }t|dd}
t|
tjr@| }	|  }d}t|
rPnt|
rh|tj}njt|}|dk}z.tj |t! ||tj"k	pt|
t#d\}}W n* t tfk
r   |dkrȂ |}Y nX |dk	r||  }n4|tj"k	r|dks
t|
t#rtj$|j%tj&d	}|dk	r t|jr d}|dkrLtj'd }nh|dkr|t(|rpt)|dkr|tj'd }n8|dkrtj'd }ttj*j+}|,|}||d }|dk	r |D ]<}t|}|j-|jj-krt.||}|j|kr q q|	dk	s|dk	rt/|js|	dksB|dk	rH|j%|	j%krH|}	n|	0 }	t|	tj1sbt2tj$|	j%|jd	}|||	 < ddl3m4}m5}m6}m7} t8|jr|}nt9|jr|}n|}|||	}|dkst|
tjr||: }|r| j;|| j,| j<dS |rtj=|| j<dS |r*|d S |S dS )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 be 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 : str, default None
        Can be 'integer', 'signed', 'unsigned', or 'float'.
        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.
    dtype_backend : {"numpy_nullable", "pyarrow"}, defaults to NumPy backed DataFrames
        Which dtype_backend to use, e.g. whether a DataFrame should have NumPy
        arrays, nullable dtypes are used for all dtypes that have a nullable
        implementation when "numpy_nullable" is set, pyarrow is used for all
        dtypes if "pyarrow" is set.

        The dtype_backends are still experimential.

        .. versionadded:: 2.0

    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

    Downcasting of nullable integer and floating dtypes is supported:

    >>> s = pd.Series([1, 2, 3], dtype="Int64")
    >>> pd.to_numeric(s, downcast="integer")
    0    1
    1    2
    2    3
    dtype: Int8
    >>> s = pd.Series([1.0, 2.1, 3.0], dtype="Float64")
    >>> pd.to_numeric(s, downcast="float")
    0    1.0
    1    2.1
    2    3.0
    dtype: Float32
    )Nintegersignedunsignedfloatz#invalid downcasting method provided)ignorer   Zcoercezinvalid error value specifiedFTi8O)dtypendim   z/arg must be a list, tuple, 1-d array, or SeriesNr#   )r    r   )coerce_numericZconvert_to_masked_nullabler   )r   r   ZIntegerr   r   ZUnsignedIntegerr   ZFloat)ArrowExtensionArrayBooleanArrayFloatingArrayIntegerArrayZpyarrow)indexname)r,   )>
ValueErrorr   
isinstancer   valuesr   r   r#   viewlisttuplenparrayr   r   r   r   getattr	TypeErrorr   Z_mask_datapdZ
ArrowDtypeZisnaZdropnaZto_numpyr   r   Zint64r
   r   Zmaybe_convert_numericset
no_defaultr   zerosshapeZbool_	typecodeslenminZfloat32charr+   itemsizer	   r   copyZndarrayAssertionErrorpandas.core.arraysr'   r(   r)   r*   r   r   Z__arrow_array__Z_constructorr,   ZIndex)argr   r   r   Z	is_seriesZis_indexZ
is_scalarsr/   Zorig_valuesmaskZvalues_dtypeZnew_maskr&   r=   Zfloat_32_charZfloat_32_indtypecoder#   datar'   r(   r)   r*   klass rJ   =/tmp/pip-unpacked-wheel-nbcvw55c/pandas/core/tools/numeric.py
to_numeric&   s    z









$






 "

rL   )&
__future__r   typingr   Znumpyr3   Zpandas._libsr   Zpandas._typingr   r   r   Zpandas.util._validatorsr   Zpandas.core.dtypes.castr	   Zpandas.core.dtypes.commonr
   r   r   r   r   r   r   r   r   r   Zpandas.core.dtypes.genericr   r   Zpandasr8   rD   r   Zpandas.core.arrays.string_r   r:   rL   rJ   rJ   rJ   rK   <module>   s   0