gL i7<dZddlmZddlmZmZddlmZmZddl Z ddl m Z ddl mZmZedeeZed eedZdd Zdd Zdd Zdd ZddZ ddZ d ddZdddZddZed dZed!dZ d"dZd#dZd$dZd%dZ ddZ!y)&zV Module that contains many useful utilities for validating data or function arguments ) annotations)IterableSequence)TypeVaroverloadN)lib)is_bool is_integerBoolishT BoolishNoneTc |dkr tdt|t|kDr> t9'::!.!!3:g&}oQxjA ! *  $c|D]H} ||}||}||||d}n||k(}t|s td |r8td|d|dy#t$r ||||u}Y,wxYw)z Check that the keys in `arg_val_dict` are mapped to their default values as specified in `compat_args`. Note that this function is to be called only when it has been checked that arg_val_dict.keys() is a subset of compat_args NFz'match' is not a booleanzthe 'z=' parameter is not supported in the pandas implementation of z())r r)r arg_val_dictrkeyv1v2matchs r_check_for_default_valuesr$1s :c"BS!B2:2:".b5> !;<<"u005wb: 1( : %S)99E :s0AA%$A%cdt||||tt||}t|||y)a Checks whether the length of the `*args` argument passed into a function has at most `len(compat_args)` arguments and whether or not all of these elements in `args` are set to their default values. Parameters ---------- fname : str The name of the function being passed the `*args` parameter args : tuple The `*args` parameter passed into a function max_fname_arg_count : int The maximum number of arguments that the function `fname` can accept, excluding those in `args`. Used for displaying appropriate error messages. Must be non-negative. compat_args : dict A dictionary of keys and their associated default values. In order to accommodate buggy behaviour in some versions of `numpy`, where a signature displayed keyword arguments but then passed those arguments **positionally** internally when calling downstream implementations, a dict ensures that the original order of the keyword arguments is enforced. Raises ------ TypeError If `args` contains more values than there are `compat_args` ValueError If `args` contains values that do not correspond to those of the default values specified in `compat_args` N)rdictzipr$)rrrrkwargss r validate_argsr)Ws3@eT#6 D #k4( )FeV[9rct|t|z }|r%tt|}t|d|dy)z} Checks whether 'kwargs' contains any keys that are not in 'compat_args' and raises a TypeError if there is one. z'() got an unexpected keyword argument ''N)setnextiterr)rr(rdiffbad_args r_check_for_invalid_keysr1sG v;[) )D tDz"5'!H QRSTT rcX|j}t|||t|||y)a Checks whether parameters passed to the **kwargs argument in a function `fname` are valid parameters as specified in `*compat_args` and whether or not they are set to their default values. Parameters ---------- fname : str The name of the function being passed the `**kwargs` parameter kwargs : dict The `**kwargs` parameter passed into `fname` compat_args: dict A dictionary of keys that `kwargs` is allowed to have and their associated default values Raises ------ TypeError if `kwargs` contains keys not in `compat_args` ValueError if `kwargs` contains keys in `compat_args` that do not map to the default values specified in `compat_args` N)copyr1r$)rr(rkwdss rvalidate_kwargsr5s', ;;=DE6;7eT;7rct||t|jz||tt ||}|D]}||vst |d|d|j |t|||y)a Checks whether parameters passed to the *args and **kwargs argument in a function `fname` are valid parameters as specified in `*compat_args` and whether or not they are set to their default values. Parameters ---------- fname: str The name of the function being passed the `**kwargs` parameter args: tuple The `*args` parameter passed into a function kwargs: dict The `**kwargs` parameter passed into `fname` max_fname_arg_count: int The minimum number of arguments that the function `fname` requires, excluding those in `args`. Used for displaying appropriate error messages. Must be non-negative. compat_args: dict A dictionary of keys that `kwargs` is allowed to have and their associated default values. Raises ------ TypeError if `args` contains more values than there are `compat_args` OR `kwargs` contains keys not in `compat_args` ValueError if `args` contains values not at the default value (`None`) `kwargs` contains keys in `compat_args` that do not map to the default value as specified in `compat_args` See Also -------- validate_args : Purely args validation. validate_kwargs : Purely kwargs validation. z-() got multiple values for keyword argument 'r+N)rtuplevaluesr&r'rupdater5)rrr(rr args_dictr s rvalidate_args_and_kwargsr;sP teFMMO,,.A; Sd+,I &='Fse1M   MM)E6;/rct|}|r|xs|du}|r|xst|t}|s%td|dt |j d|S)aR Ensure that argument passed in arg_name can be interpreted as boolean. Parameters ---------- value : bool Value to be validated. arg_name : str Name of the argument. To be reflected in the error message. none_allowed : bool, default True Whether to consider None to be a valid boolean. int_allowed : bool, default False Whether to consider integer value to be a valid boolean. Returns ------- value The same value as input. Raises ------ ValueError If the value is not a valid boolean. NzFor argument "z$" expected type bool, received type .)r isinstanceintrtype__name__)valuearg_name none_allowed int_allowed good_values rvalidate_bool_kwargrGsl<J2ETM 9:eS#9 XJ'K(() ,   Lrcddlm}| | td|| ||}||fS|@|>|r8t|tt fr"t dt|jd||fS| | td||fS)a$ Validate the keyword arguments to 'fillna'. This checks that exactly one of 'value' and 'method' is specified. If 'method' is specified, this validates that it's a valid method. Parameters ---------- value, method : object The 'value' and 'method' keyword arguments for 'fillna'. validate_scalar_dict_value : bool, default True Whether to validate that 'value' is a scalar or dict. Specifically, validate that it is not a list or tuple. Returns ------- value, method : object r)clean_fill_methodz(Must specify a fill 'value' or 'method'.z>"value" parameter must be a scalar or dict, but you passed a ""z)Cannot specify both 'value' and 'method'.) pandas.core.missingrIrr>listr7rr@rA)rBmethodvalidate_scalar_dict_valuerIs rvalidate_fillna_kwargsrOs&6 }CDD }+"6* &=  v~ %*UT5M*J!!%e!5!5 6a9  &=  v1DEE &=rctj|}d}|jdk(r$d|cxkrdkst|t||St d|Ds t||S)a Validate percentiles (used by describe and quantile). This function checks if the given float or iterable of floats is a valid percentile otherwise raises a ValueError. Parameters ---------- q: float or iterable of floats A single percentile or an iterable of percentiles. Returns ------- ndarray An ndarray of the percentiles if valid. Raises ------ ValueError if percentiles are not in given interval([0, 1]). z0percentiles should all be in the interval [0, 1]rrc3<K|]}d|cxkxrdkncyw)rrN).0qss r z&validate_percentile..Ss0B1rrG)rar(items rrbrbbsK$D 9F i *"9kDVDDIR S k c`d}d}|d}d}||fS|dk(rd}||fS|dk(rd}||fStd)a% Check that the `closed` argument is among [None, "left", "right"] Parameters ---------- closed : {None, "left", "right"} Returns ------- left_closed : bool right_closed : bool Raises ------ ValueError : if argument is not among valid values FTleftrightz/Closed has to be either 'left', 'right' or None)r)closed left_closed right_closeds rvalidate_endpointsrmmsp"KL ~    $$ 6   $$ 7    $$JKKrcpd}t|trdddddj|}| td|S)aD Check that the `inclusive` argument is among {"both", "neither", "left", "right"}. Parameters ---------- inclusive : {"both", "neither", "left", "right"} Returns ------- left_right_inclusive : tuple[bool, bool] Raises ------ ValueError : if argument is not among valid values N)TTTF)FT)FF)bothrhrineitherz?Inclusive has to be either 'both', 'neither', 'left' or 'right')r>strgetr) inclusiveleft_right_inclusives rvalidate_inclusivervsU 6:)S! !"%  #i. # M   rct|std|d||dkr||z }d|cxkr|ksntd|d||S)z Check that we have an integer between -length and length, inclusive. Standardize negative loc to within [0, length]. The exceptions we raise on failure match np.insert. z loc must be an integer between -z and r)r r IndexError)loclengths rvalidate_insert_locr{s_ c?:6(%xPQQ Qw v   v ;F85QRR JrcP|tjur|dvrtd|dyy)N)numpy_nullablepyarrowzdtype_backend z= is invalid, only 'numpy_nullable' and 'pyarrow' are allowed.)r no_defaultr) dtype_backends rcheck_dtype_backendrs?CNN*  = = 0)*  >+r)returnNonero) rBr rCrrrDboolrErrr )T)rNr)rZzfloat | Iterable[float]rz np.ndarray)rar rr )razSequence[BoolishT]rzlist[BoolishT])razbool | int | Sequence[BoolishT]rzbool | int | list[BoolishT])rj str | Nonertuple[bool, bool])rtrrr)ryr?rzr?rr?)"__doc__ __future__rcollections.abcrrtypingrrnumpyrV pandas._libsrpandas.core.dtypes.commonr r rr?r r rr$r)r1r5r;rGrOr]rbrmrvr{rrRrrrs#  :tS )~tS$7  (#L&:R U8670 70z * *** *  *Z$ND    T.T T%B D$r