o
    ŀg                     @  s~   d Z 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 er.ddlmZ ddlmZ d"ddZd#ddZd$d d!ZdS )%zH
Module containing utilities for NDFrame.sample() and .GroupBy.sample()
    )annotations)TYPE_CHECKINGN)lib)ABCDataFrame	ABCSeries)AxisInt)NDFrameobjr   axisr   return
np.ndarrayc              
   C  s  t |tr|| j| }t |tr;t | tr7|dkr3z| | }W n ty2 } ztd|d}~ww tdtdt | trD| j}n| j	}||ddj
}t|| j| kr[tdt|rdtd	|dk  rntd
t|}| r| }d||< |S )z
    Process and validate the `weights` argument to `NDFrame.sample` and
    `.GroupBy.sample`.

    Returns `weights` as an ndarray[np.float64], validated except for normalizing
    weights (because that must be done groupwise in groupby sampling).
    r   z+String passed to weights not a valid columnNzLStrings can only be passed to weights when sampling from rows on a DataFramez@Strings cannot be passed as weights when sampling from a Series.float64)dtypez5Weights and axis to be sampled must be of same lengthz*weight vector may not include `inf` valuesz.weight vector many not include negative values)
isinstancer   reindexaxesstrr   KeyError
ValueError_constructor_constructor_sliced_valueslenshaper   has_infsanynpisnancopy)r	   weightsr
   errfuncmissing r#   F/var/www/html/myenv/lib/python3.10/site-packages/pandas/core/sample.pypreprocess_weights   sH   
	




r%   n
int | Nonefracfloat | Nonereplaceboolc                 C  s   | du r|du rd} | S | dur|durt d| dur0| dk r$t d| d dkr.t d| S |dus6J |dkr@|s@t d|dk rHt d| S )	z
    Process and validate the `n` and `frac` arguments to `NDFrame.sample` and
    `.GroupBy.sample`.

    Returns None if `frac` should be used (variable sampling sizes), otherwise returns
    the constant sampling size.
    N   z0Please enter a value for `frac` OR `n`, not bothr   z=A negative number of rows requested. Please provide `n` >= 0.z$Only integers accepted as `n` valueszJReplace has to be set to `True` when upsampling the population `frac` > 1.z@A negative number of rows requested. Please provide `frac` >= 0.)r   )r&   r(   r*   r#   r#   r$   process_sampling_sizeP   s.   r-   obj_lenintsizer   np.ndarray | Nonerandom_state+np.random.RandomState | np.random.Generatorc                 C  sH   |dur|  }|dkr|| }ntd|j| |||djtjddS )ac  
    Randomly sample `size` indices in `np.arange(obj_len)`

    Parameters
    ----------
    obj_len : int
        The length of the indices being considered
    size : int
        The number of values to choose
    replace : bool
        Allow or disallow sampling of the same row more than once.
    weights : np.ndarray[np.float64] or None
        If None, equal probability weighting, otherwise weights according
        to the vector normalized
    random_state: np.random.RandomState or np.random.Generator
        State used for the random sampling

    Returns
    -------
    np.ndarray[np.intp]
    Nr   z$Invalid weights: weights sum to zero)r0   r*   pF)r   )sumr   choiceastyper   intp)r.   r0   r*   r   r2   
weight_sumr#   r#   r$   sampleu   s   
r:   )r	   r   r
   r   r   r   )r&   r'   r(   r)   r*   r+   r   r'   )r.   r/   r0   r/   r*   r+   r   r1   r2   r3   r   r   )__doc__
__future__r   typingr   numpyr   pandas._libsr   pandas.core.dtypes.genericr   r   pandas._typingr   pandas.core.genericr   r%   r-   r:   r#   r#   r#   r$   <module>   s    

9%