o
    ŀg+                     @  s   d dl mZ d dlmZmZ d dlZd dlZd dlm	Z	 d dl
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 d d	lmZ d d
lmZmZ d dlm Z  d dl!m"Z" erjd dl#m$Z$m%Z%m&Z& dde	j'fdddZ(dS )    )annotations)TYPE_CHECKINGLiteralN)lib)find_stack_level)check_dtype_backend)maybe_downcast_numeric)	ensure_objectis_bool_dtype
is_decimalis_integer_dtype	is_numberis_numeric_dtype	is_scalaris_string_dtypeneeds_i8_conversion)
ArrowDtype)ABCIndex	ABCSeries)BaseMaskedArray)StringDtype)DateTimeErrorChoicesDtypeBackendnptraiseerrorsr   downcast8Literal['integer', 'signed', 'unsigned', 'float'] | Nonedtype_backendDtypeBackend | lib.NoDefaultc              	   C  sT  |dvrt d|dvrt d|dkrtjdtt d t| d}d}d}t| tr2d	}| j}nPt| t	rHd	}t
| jrD| d
}n>| j}n:t| ttfrWtj| dd}n+t| rtt| rct| S t| ri| S d	}tj| gdd}nt| dddkrtd| }|}d}	t|tr|j}	|j|	  }t|dd}
t|
tr| }	|  }d}t|
rnDt |
dr|tj!}n7t"|}|dv}ztj#|t$ ||tj%upt|
t&o|
j'dk d\}}W n t tfy   |dkr |}Y nw |dur||  }n |tj%ur	|du st|
t&r|
j'dkstj(|j)tj*d}|durt|jrd}|dv r6tj+d }n4|dkrNt,|rHt-|dkrNtj+d }n|dkrjtj+d }ttj.j/}|0|}||d }|dur|D ]}t|}|j1|jj1krt2||}|j|kr nqq|	dus|durt3|js|	du s|dur|j)|	j)kr|}	n|	4 }	t|	tj5sJ tj(|	j)|jd}|||	 < ddl6m7}m8}m9}m:} t;|jr|}nt<|jr|}n|}|||	}|dkst|
tr||= }|r| j>|| j0| j?d S |r!dd!l@mA} ||| j?d"S |r(|d S |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.

        .. versionchanged:: 2.2

        "ignore" is deprecated. Catch exceptions explicitly instead.

    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'}, default 'numpy_nullable'
        Back-end data type applied to the resultant :class:`DataFrame`
        (still experimental). Behaviour is as follows:

        * ``"numpy_nullable"``: returns nullable-dtype-backed :class:`DataFrame`
          (default).
        * ``"pyarrow"``: returns pyarrow-backed nullable :class:`ArrowDtype`
          DataFrame.

        .. 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='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   coercezinvalid error value specifiedr$   zerrors='ignore' is deprecated and will raise in a future version. Use to_numeric without passing `errors` and catch exceptions explicitly instead)
stacklevelFTi8O)dtypendim   z/arg must be a list, tuple, 1-d array, or SeriesNr)   mM)r$   r   pyarrow_numpy)coerce_numericconvert_to_masked_nullabler   )r    r!   Integerr"   r   UnsignedIntegerr#   Float)ArrowExtensionArrayBooleanArrayFloatingArrayIntegerArraypyarrow)indexname)Index)r9   )B
ValueErrorwarningswarnFutureWarningr   r   
isinstancer   valuesr   r   r)   viewlisttuplenparrayr   r   r#   r   getattr	TypeErrorr   _mask_datar   isnadropnato_numpyr   r   is_np_dtypeint64r	   maybe_convert_numericset
no_defaultr   storagezerosshapebool_	typecodeslenminfloat32charr8   itemsizer   r   copyndarraypandas.core.arraysr3   r4   r5   r6   r   r
   __arrow_array___constructorr9   pandasr:   )argr   r   r   	is_seriesis_index
is_scalarsr@   orig_valuesmaskvalues_dtypenew_maskr.   rV   float_32_charfloat_32_indtypecoder)   datar3   r4   r5   r6   klassr:    ro   M/var/www/html/myenv/lib/python3.10/site-packages/pandas/core/tools/numeric.py
to_numeric,   s   z









$





 "

rq   )r   r   r   r   r   r   ))
__future__r   typingr   r   r<   numpyrD   pandas._libsr   pandas.util._exceptionsr   pandas.util._validatorsr   pandas.core.dtypes.castr   pandas.core.dtypes.commonr	   r
   r   r   r   r   r   r   r   pandas.core.dtypes.dtypesr   pandas.core.dtypes.genericr   r   r^   r   pandas.core.arrays.string_r   pandas._typingr   r   r   rQ   rq   ro   ro   ro   rp   <module>   s&    ,	