Sindbad~EG File Manager

Current Path : /usr/local/lib/python3.12/site-packages/pandas/core/reshape/__pycache__/
Upload File :
Current File : //usr/local/lib/python3.12/site-packages/pandas/core/reshape/__pycache__/tile.cpython-312.pyc

�

Mٜg�U���dZddlmZddlmZmZmZmZddlZ	ddl
mZmZm
Z
ddlmZmZmZmZmZmZddlmZmZmZddlmZdd	lmZdd
lmZmZm Z ddl!m"cm#Z$ddl%m&Z&erddl'm(Z(m)Z)							d											dd
�Z*				d					dd�Z+dd�Z,						d													dd�Z-dd�Z.d d�Z/		d!							d"d�Z0d#d�Z1d$d�Z2d%d�Z3d&d�Z4y)'z,
Quantilization functions and related stuff
�)�annotations)�
TYPE_CHECKING�Any�Callable�LiteralN)�	Timedelta�	Timestamp�lib)�ensure_platform_int�
is_bool_dtype�
is_integer�is_list_like�is_numeric_dtype�	is_scalar)�CategoricalDtype�DatetimeTZDtype�ExtensionDtype)�	ABCSeries)�isna)�Categorical�Index�
IntervalIndex)�
dtype_to_unit)�DtypeObj�IntervalLeftRightc	
�X�|}	t|�}
t|
�\}
}tj|�st	|
||�}nIt|t�r|jr-td��t|�}|jstd��t|
|||||||��\}}t||||	�S)a	
    Bin values into discrete intervals.

    Use `cut` when you need to segment and sort data values into bins. This
    function is also useful for going from a continuous variable to a
    categorical variable. For example, `cut` could convert ages to groups of
    age ranges. Supports binning into an equal number of bins, or a
    pre-specified array of bins.

    Parameters
    ----------
    x : array-like
        The input array to be binned. Must be 1-dimensional.
    bins : int, sequence of scalars, or IntervalIndex
        The criteria to bin by.

        * int : Defines the number of equal-width bins in the range of `x`. The
          range of `x` is extended by .1% on each side to include the minimum
          and maximum values of `x`.
        * sequence of scalars : Defines the bin edges allowing for non-uniform
          width. No extension of the range of `x` is done.
        * IntervalIndex : Defines the exact bins to be used. Note that
          IntervalIndex for `bins` must be non-overlapping.

    right : bool, default True
        Indicates whether `bins` includes the rightmost edge or not. If
        ``right == True`` (the default), then the `bins` ``[1, 2, 3, 4]``
        indicate (1,2], (2,3], (3,4]. This argument is ignored when
        `bins` is an IntervalIndex.
    labels : array or False, default None
        Specifies the labels for the returned bins. Must be the same length as
        the resulting bins. If False, returns only integer indicators of the
        bins. This affects the type of the output container (see below).
        This argument is ignored when `bins` is an IntervalIndex. If True,
        raises an error. When `ordered=False`, labels must be provided.
    retbins : bool, default False
        Whether to return the bins or not. Useful when bins is provided
        as a scalar.
    precision : int, default 3
        The precision at which to store and display the bins labels.
    include_lowest : bool, default False
        Whether the first interval should be left-inclusive or not.
    duplicates : {default 'raise', 'drop'}, optional
        If bin edges are not unique, raise ValueError or drop non-uniques.
    ordered : bool, default True
        Whether the labels are ordered or not. Applies to returned types
        Categorical and Series (with Categorical dtype). If True,
        the resulting categorical will be ordered. If False, the resulting
        categorical will be unordered (labels must be provided).

    Returns
    -------
    out : Categorical, Series, or ndarray
        An array-like object representing the respective bin for each value
        of `x`. The type depends on the value of `labels`.

        * None (default) : returns a Series for Series `x` or a
          Categorical for all other inputs. The values stored within
          are Interval dtype.

        * sequence of scalars : returns a Series for Series `x` or a
          Categorical for all other inputs. The values stored within
          are whatever the type in the sequence is.

        * False : returns an ndarray of integers.

    bins : numpy.ndarray or IntervalIndex.
        The computed or specified bins. Only returned when `retbins=True`.
        For scalar or sequence `bins`, this is an ndarray with the computed
        bins. If set `duplicates=drop`, `bins` will drop non-unique bin. For
        an IntervalIndex `bins`, this is equal to `bins`.

    See Also
    --------
    qcut : Discretize variable into equal-sized buckets based on rank
        or based on sample quantiles.
    Categorical : Array type for storing data that come from a
        fixed set of values.
    Series : One-dimensional array with axis labels (including time series).
    IntervalIndex : Immutable Index implementing an ordered, sliceable set.

    Notes
    -----
    Any NA values will be NA in the result. Out of bounds values will be NA in
    the resulting Series or Categorical object.

    Reference :ref:`the user guide <reshaping.tile.cut>` for more examples.

    Examples
    --------
    Discretize into three equal-sized bins.

    >>> pd.cut(np.array([1, 7, 5, 4, 6, 3]), 3)
    ... # doctest: +ELLIPSIS
    [(0.994, 3.0], (5.0, 7.0], (3.0, 5.0], (3.0, 5.0], (5.0, 7.0], ...
    Categories (3, interval[float64, right]): [(0.994, 3.0] < (3.0, 5.0] ...

    >>> pd.cut(np.array([1, 7, 5, 4, 6, 3]), 3, retbins=True)
    ... # doctest: +ELLIPSIS
    ([(0.994, 3.0], (5.0, 7.0], (3.0, 5.0], (3.0, 5.0], (5.0, 7.0], ...
    Categories (3, interval[float64, right]): [(0.994, 3.0] < (3.0, 5.0] ...
    array([0.994, 3.   , 5.   , 7.   ]))

    Discovers the same bins, but assign them specific labels. Notice that
    the returned Categorical's categories are `labels` and is ordered.

    >>> pd.cut(np.array([1, 7, 5, 4, 6, 3]),
    ...        3, labels=["bad", "medium", "good"])
    ['bad', 'good', 'medium', 'medium', 'good', 'bad']
    Categories (3, object): ['bad' < 'medium' < 'good']

    ``ordered=False`` will result in unordered categories when labels are passed.
    This parameter can be used to allow non-unique labels:

    >>> pd.cut(np.array([1, 7, 5, 4, 6, 3]), 3,
    ...        labels=["B", "A", "B"], ordered=False)
    ['B', 'B', 'A', 'A', 'B', 'B']
    Categories (2, object): ['A', 'B']

    ``labels=False`` implies you just want the bins back.

    >>> pd.cut([0, 1, 1, 2], bins=4, labels=False)
    array([0, 1, 1, 3])

    Passing a Series as an input returns a Series with categorical dtype:

    >>> s = pd.Series(np.array([2, 4, 6, 8, 10]),
    ...               index=['a', 'b', 'c', 'd', 'e'])
    >>> pd.cut(s, 3)
    ... # doctest: +ELLIPSIS
    a    (1.992, 4.667]
    b    (1.992, 4.667]
    c    (4.667, 7.333]
    d     (7.333, 10.0]
    e     (7.333, 10.0]
    dtype: category
    Categories (3, interval[float64, right]): [(1.992, 4.667] < (4.667, ...

    Passing a Series as an input returns a Series with mapping value.
    It is used to map numerically to intervals based on bins.

    >>> s = pd.Series(np.array([2, 4, 6, 8, 10]),
    ...               index=['a', 'b', 'c', 'd', 'e'])
    >>> pd.cut(s, [0, 2, 4, 6, 8, 10], labels=False, retbins=True, right=False)
    ... # doctest: +ELLIPSIS
    (a    1.0
     b    2.0
     c    3.0
     d    4.0
     e    NaN
     dtype: float64,
     array([ 0,  2,  4,  6,  8, 10]))

    Use `drop` optional when bins is not unique

    >>> pd.cut(s, [0, 2, 4, 6, 10, 10], labels=False, retbins=True,
    ...        right=False, duplicates='drop')
    ... # doctest: +ELLIPSIS
    (a    1.0
     b    2.0
     c    3.0
     d    3.0
     e    NaN
     dtype: float64,
     array([ 0,  2,  4,  6, 10]))

    Passing an IntervalIndex for `bins` results in those categories exactly.
    Notice that values not covered by the IntervalIndex are set to NaN. 0
    is to the left of the first bin (which is closed on the right), and 1.5
    falls between two bins.

    >>> bins = pd.IntervalIndex.from_tuples([(0, 1), (2, 3), (4, 5)])
    >>> pd.cut([0, 0.5, 1.5, 2.5, 4.5], bins)
    [NaN, (0.0, 1.0], NaN, (2.0, 3.0], (4.0, 5.0]]
    Categories (3, interval[int64, right]): [(0, 1] < (2, 3] < (4, 5]]
    z*Overlapping IntervalIndex is not accepted.z!bins must increase monotonically.)�right�labels�	precision�include_lowest�
duplicates�ordered)
�_preprocess_for_cut�_coerce_to_type�np�iterable�_nbins_to_bins�
isinstancer�is_overlapping�
ValueErrorr�is_monotonic_increasing�
_bins_to_cuts�_postprocess_for_cut)
�x�binsrr�retbinsrr r!r"�original�x_idx�_�facs
             �C/usr/local/lib/python3.12/site-packages/pandas/core/reshape/tile.py�cutr64s���z�H���"�E��u�%�H�E�1�
�;�;�t���e�T�5�1��	�D�-�	(�����I�J�J��T�{���+�+��@�A�A��
�����%���	�I�C�� ��T�7�H�=�=�c�6�|}t|�}t|�\}}t|�rtjdd|dz�n|}	|j�j
�j|	�}
t|t|
�||d|��\}}
t||
||�S)a!
    Quantile-based discretization function.

    Discretize variable into equal-sized buckets based on rank or based
    on sample quantiles. For example 1000 values for 10 quantiles would
    produce a Categorical object indicating quantile membership for each data point.

    Parameters
    ----------
    x : 1d ndarray or Series
    q : int or list-like of float
        Number of quantiles. 10 for deciles, 4 for quartiles, etc. Alternately
        array of quantiles, e.g. [0, .25, .5, .75, 1.] for quartiles.
    labels : array or False, default None
        Used as labels for the resulting bins. Must be of the same length as
        the resulting bins. If False, return only integer indicators of the
        bins. If True, raises an error.
    retbins : bool, optional
        Whether to return the (bins, labels) or not. Can be useful if bins
        is given as a scalar.
    precision : int, optional
        The precision at which to store and display the bins labels.
    duplicates : {default 'raise', 'drop'}, optional
        If bin edges are not unique, raise ValueError or drop non-uniques.

    Returns
    -------
    out : Categorical or Series or array of integers if labels is False
        The return type (Categorical or Series) depends on the input: a Series
        of type category if input is a Series else Categorical. Bins are
        represented as categories when categorical data is returned.
    bins : ndarray of floats
        Returned only if `retbins` is True.

    Notes
    -----
    Out of bounds values will be NA in the resulting Categorical object

    Examples
    --------
    >>> pd.qcut(range(5), 4)
    ... # doctest: +ELLIPSIS
    [(-0.001, 1.0], (-0.001, 1.0], (1.0, 2.0], (2.0, 3.0], (3.0, 4.0]]
    Categories (4, interval[float64, right]): [(-0.001, 1.0] < (1.0, 2.0] ...

    >>> pd.qcut(range(5), 3, labels=["good", "medium", "bad"])
    ... # doctest: +SKIP
    [good, good, medium, bad, bad]
    Categories (3, object): [good < medium < bad]

    >>> pd.qcut(range(5), 4, labels=False)
    array([0, 0, 1, 2, 3])
    r�T)rrr r!)r#r$r
r%�linspace�	to_series�dropna�quantiler,rr-)r.�qrr0rr!r1r2r3�	quantilesr/r4s            r5�qcutr@s���z�H���"�E��u�%�H�E�1�,6�q�M����A�q�!�a�%�(�q�I��?�?��#�#�%�.�.�y�9�D��
�
�d������
�I�C�� ��T�7�H�=�=r7c�<�t|�r|dkrtd��|jdk(rtd��|j�|j	�f}|\}}t|j�r5tj|�stj|�rtd��||k(r�t|j�rdt|j�}td��j|�}|jj||z
||z|dzd|��}t#|�S||dk7rd	t|�znd	z}||dk7rd	t|�znd	z
}tj |||dzd
��}t#|�St|j�r9t|j�}|jj|||dzd|��}ntj |||dzd
��}||z
d	z}	|r|dxx|	zcc<t#|�S|dxx|	z
cc<t#|�S)
zl
    If a user passed an integer N for bins, convert this to a sequence of N
    equal(ish)-sized bins.
    r9z$`bins` should be a positive integer.rzCannot cut empty arrayz?cannot specify integer `bins` when input data contains infinity)�secondsN)�start�end�periods�freq�unitg����MbP?T)�endpoint���)rr*�size�min�maxr�dtyper%�isinf�_is_dt_or_tdrr�as_unit�_values�_generate_range�absr:r)
r2�nbinsr�rng�mn�mxrG�tdr/�adjs
          r5r'r'`s���
���E�A�I��?�@�@��z�z�Q���1�2�2��9�9�;��	�	��
$�C�
�F�B������$�"�(�(�2�,�"�(�(�2�,��M�
�	
�
�R�x�����$�!����-�D��1�%�-�-�d�3�B��=�=�0�0��2�g�2��7�E�A�I�D�t�1��D�8��;��1
�R�1�W�%�#�b�'�/�%�7�B��R�1�W�%�#�b�'�/�%�7�B��;�;�r�2�u�q�y�4�@�D�*��;��'����$�
!����-�D��=�=�0�0��b�%�!�)�$�T�1��D��;�;�r�2�u�q�y�4�@�D��B�w�%������G�s�N�G���;��
��H��O�H���;�r7c� �|s
|�td��|dvrtd��t|t�r:|j|�}t	|d��}	tj||	d��}
|
|fStj|�}t|�t|�kr-t|�dk7r|d	k(rtd
t|��d���|}|rdnd
}	|j||��}t|�}|rd|||dk(<t!|�|t|�k(z|dk(z}|j#�}|dur�|�t%|�std��|�t'||||��}nR|r+tt)|��t|�k7rtd��t|�t|�dz
k7rtd��tt+|dd�t�s0t|tt)|��t|�k(r|nd|��}t-j.||d�tj0||dz
�}
|
|fS|dz
}
|rD|
j3t,j4�}
t-j.|
|t,j6�|
|fS#t$r�}
|jjdk(rtd�|
�|jj|jjcxk(rdk(rnntd�|
�|jjdk(rtd�|
��d}
~
wwxYw)Nz.'labels' must be provided if 'ordered = False')�raise�dropzHinvalid value for 'duplicates' parameter, valid options are: raise, dropT)r"F)rM�validate�r[zBin edges must be unique: z@.
You can drop duplicate edges by setting the 'duplicates' kwarg�leftr)�side�mz!bins must be of timedelta64 dtype�MzHCannot use timezone-naive bins with timezone-aware values, or vice-versaz bins must be of datetime64 dtyper9rzJBin labels must either be False, None or passed in as a list-like argument)rr zNlabels must be unique if ordered=True; pass ordered=False for duplicate labelsz9Bin labels must be one fewer than the number of bin edgesrM)�
categoriesr")r*r(r�get_indexerrr�
from_codes�algos�unique�len�repr�searchsorted�	TypeErrorrM�kindrr�anyr�_format_labels�set�getattrr%�putmask�take_nd�astype�float64�nan)r2r/rrrr r!r"�ids�	cat_dtype�result�unique_binsr`�err�na_mask�has_nass                r5r,r,�s���v�~��I�J�J��*�*��V�
�	
��$�
�&����u�%��$�T�4�8�	��'�'��9�u�M���t�|���,�,�t�$�K�
�;��#�d�)�#��D�	�Q���� ��,�T�$�Z�L�9Q�R��
���/4�V�'�D������D��1���c�
"�C�� !��E�T�!�W����5�k�S�C��I�-�.�#��(�;�G��k�k�m�G�
�U����,�v�"6��%��
�
�>�#��i�u�^��F���S��[�)�S��[�8��'��
�
�6�{�c�$�i�!�m�+� �O����'�&�'�4�8�:J�K� ��%(��V��%5��V��%D�6�$���F�	�
�
�3���#����v�s�Q�w�/���4�<���q�����]�]�2�:�:�.�F��J�J�v�w����/��4�<���y�
��;�;���s�"��@�A�s�J�
�[�[�
�
������
7�C�
7�� ���
��[�[�
�
��
$��?�@�c�I���
�s�	I2�2	L
�;B
L�L
c��d}t|j�r
|j}n�t|j�r |jtj
�}nit
|jt�rOt|j�r:|jtjtj��}t|�}t|�|fS)z�
    if the passed data is of datetime/timedelta, bool or nullable int type,
    this method converts it to numeric so that cut or qcut method can
    handle it
    N)rM�na_value)
rOrMrrsr%�int64r(rr�to_numpyrtrur)r.rM�x_arrs   r5r$r$s���"�E��A�G�G������	�q�w�w�	�
�H�H�R�X�X���

�A�G�G�^�	,�1A�!�'�'�1J��
�
����b�f�f�
�=���%�L����8�U�?�r7c�R�t|t�xstj|d�S)N�mM)r(rr
�is_np_dtype)rMs r5rOrOs!���e�_�-�M������1M�Mr7c���	�|rdnd}t|j�rt|j��	d�}�	fd�}nt�|���fd�}�fd�}|D�cgc]
}||���}}|r|r||d�|d<t|j�r t	|�|�j�	�}t
j||��Scc}w)	z%based on the dtype, return our labelsrr_c��|S�N�)r.s r5�<lambda>z _format_labels.<locals>.<lambda>1s��ar7c�B��|td���j��z
S)Nr9)rG)rrP)r.rGs �r5r�z _format_labels.<locals>.<lambda>2s���1�y���6�>�>�t�D�Dr7c���t|��Sr�)�_round_frac�r.rs �r5r�z _format_labels.<locals>.<lambda>5s���k�!�Y�7r7c���|d�zz
S)N�
r�r�s �r5r�z _format_labels.<locals>.<lambda>6s���1�r�y�j�1�1r7r)�closed)rOrMr�_infer_precision�typerPr�from_breaks)
r/rrr r��	formatter�adjust�b�breaksrGs
 `       @r5rnrn"s����,1��f�F��D�J�J���T�Z�Z�(���	�D��$�Y��5�	�7�	�1��$(�
)�D�q�i��l�D�F�
)����6�!�9�%��q�	��D�J�J����d��F�#�+�+�D�1���$�$�V�F�;�;��*s�Cc��t|dd�}|�tj|�}|jdk7rt	d��t|�S)z�
    handles preprocessing for cut where we convert passed
    input to array, strip the index information and store it
    separately
    �ndimNr9z!Input array must be 1 dimensional)rpr%�asarrayr�r*r)r.r�s  r5r#r#DsD���1�f�d�#�D��|��J�J�q�M���v�v��{��<�=�=���8�Or7c���t|t�r(|j||j|j��}|s|St|t
�r!t
|j�r|j}||fS)z�
    handles post processing for the cut method where
    we combine the index information if the originally passed
    datatype was a series
    )�index�name)	r(r�_constructorr�r�rrrMrQ)r4r/r0r1s    r5r-r-Us^���(�I�&��#�#�C�x�~�~�H�M�M�#�R����
��$���#3�D�J�J�#?��|�|����9�r7c	�(�tj|�r|dk(r|Stj|�\}}|dk(rBttjtj
t
|����dz
|z}n|}tj||�S)z7
    Round the fractional part of the given number
    rr9)r%�isfinite�modf�int�floor�log10rS�around)r.r�frac�whole�digitss     r5r�r�gsu���;�;�q�>�Q�!�V����g�g�a�j���e��A�:��"�(�(�2�8�8�C��I�#6�7�8�8�1�<�y�H�F��F��y�y��F�#�#r7c
���t|d�D]_}tj|D�cgc]}t||���c}�}t	j
|�j|jk(s�]|cS|Scc}w)z8
    Infer an appropriate precision for _round_frac
    �)�ranger%r�r�rfrgrJ)�base_precisionr/rr��levelss     r5r�r�vsh���>�2�.�	�����E��1�[��I�6��E�F���<�<���$�$��	�	�1���/����Fs�A1
)TNF�Fr[T)r�boolr0r�rr�r r�r!�strr"r�)NFr�r[)r0r�rr�r!r�)r2rrTr�rr��returnr)TNr�Fr[T)r2rr/rrr�rr�r r�r!r�r"r�)r.rr�ztuple[Index, DtypeObj | None])rMrr�r�)TF)r/rrr�rr�r r�)r�r)r0r�)rr�)r�r�r/rr�r�)5�__doc__�
__future__r�typingrrrr�numpyr%�pandas._libsrr	r
�pandas.core.dtypes.commonrrr
rrr�pandas.core.dtypes.dtypesrrr�pandas.core.dtypes.genericr�pandas.core.dtypes.missingr�pandasrrr�pandas.core.algorithms�core�
algorithmsrf�pandas.core.arrays.datetimeliker�pandas._typingrrr6r@r'r,r$rOrnr#r-r�r�r�r7r5�<module>r�s����#����������
1�+���
'�&�9������� ���X>��X>�
�X>��
X>��X>��X>��X>�|����
N>��	N>�
�N>��
N>�b:�@��� ���d��d�
�d��d�
�d��
d��d��d�N�0N�� �	<�
�<��<��<��	<�D�"�$$�r7

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists