
    Owg                        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	Z	 	 	 	 	 	 	 	 dd
Z	 	 	 	 	 	 	 	 	 	 	 	 ddZy)zH
Module containing utilities for NDFrame.sample() and .GroupBy.sample()
    )annotations)TYPE_CHECKINGN)lib)ABCDataFrame	ABCSeries)AxisInt)NDFramec                   t        |t              r|j                  | j                  |         }t        |t              r2t        | t
              r|dk(  r	 | |   }nt        d      t        d      t        | t              r| j                  }n| j                  } ||d      j                  }t        |      | j                  |   k7  rt        d      t        j                  |      rt        d	      |dk  j                         rt        d
      t!        j"                  |      }|j                         r|j%                         }d||<   |S # t        $ r}t        d      |d}~ww xY w)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)objweightsaxiserrfuncmissings         I/var/www/horilla/myenv/lib/python3.12/site-packages/pandas/core/sample.pypreprocess_weightsr$      sT    '9%//#((4.1 '3c<(qy!'lG !"  R  #y!&&7),44G
7|syy&PQQ
||GEFF!IJJhhwG{{},,.NI   "Es   E 	E(E##E(c                    | |d} | S | |t        d      | %| dk  rt        d      | dz  dk7  rt        d      | S |J |dkD  r|st        d      |dk  rt        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.
       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   )nfracreplaces      r#   process_sampling_sizer*   P   s     	yT\, H+ 
4+KLL	
q5O  q5A:CDD H !8G8  !8R  H    c                    |&|j                         }|dk7  r||z  }nt        d      |j                  | |||      j                  t        j
                  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]
    r   z$Invalid weights: weights sum to zero)sizer)   pF)r   )sumr   choiceastyper   intp)obj_lenr-   r)   r   random_state
weight_sums         r#   sampler6   u   sh    8 [[]
?
*GCDDwT7gNUU
e V  r+   )r   r	   r   r   return
np.ndarray)r'   
int | Noner(   zfloat | Noner)   boolr7   r9   )r3   intr-   r;   r)   r:   r   znp.ndarray | Noner4   z+np.random.RandomState | np.random.Generatorr7   r8   )__doc__
__future__r   typingr   numpyr   pandas._libsr   pandas.core.dtypes.genericr   r   pandas._typingr   pandas.core.genericr	   r$   r*   r6    r+   r#   <module>rE      s    #    
 &+6r""%"04""J%%
% % 	%
 >% %r+   