How to use register_check_strategy method in pandera

Best Python code snippet using pandera_python

checks.py

Source:checks.py Github

copy

Full Screen

...440 )441class Check(_CheckBase):442 """Check a pandas Series or DataFrame for certain properties."""443 @classmethod444 @st.register_check_strategy(st.eq_strategy)445 @register_check_statistics(["value"])446 def equal_to(cls, value, **kwargs) -> "Check":447 """Ensure all elements of a series equal a certain value.448 *New in version 0.4.5*449 Alias: ``eq``450 :param value: All elements of a given :class:`pandas.Series` must have451 this value452 :param kwargs: key-word arguments passed into the `Check` initializer.453 :returns: :class:`Check` object454 """455 def _equal(series: pd.Series) -> pd.Series:456 """Comparison function for check"""457 return series == value458 return cls(459 _equal,460 name=cls.equal_to.__name__,461 error=f"equal_to({value})",462 **kwargs,463 )464 eq = equal_to465 @classmethod466 @st.register_check_strategy(st.ne_strategy)467 @register_check_statistics(["value"])468 def not_equal_to(cls, value, **kwargs) -> "Check":469 """Ensure no elements of a series equals a certain value.470 *New in version 0.4.5*471 Alias: ``ne``472 :param value: This value must not occur in the checked473 :class:`pandas.Series`.474 :param kwargs: key-word arguments passed into the `Check` initializer.475 :returns: :class:`Check` object476 """477 def _not_equal(series: pd.Series) -> pd.Series:478 """Comparison function for check"""479 return series != value480 return cls(481 _not_equal,482 name=cls.not_equal_to.__name__,483 error=f"not_equal_to({value})",484 **kwargs,485 )486 ne = not_equal_to487 @classmethod488 @st.register_check_strategy(st.gt_strategy)489 @register_check_statistics(["min_value"])490 def greater_than(cls, min_value, **kwargs) -> "Check":491 """Ensure values of a series are strictly greater than a minimum value.492 *New in version 0.4.5*493 Alias: ``gt``494 :param min_value: Lower bound to be exceeded. Must be a type comparable495 to the dtype of the :class:`pandas.Series` to be validated (e.g. a496 numerical type for float or int and a datetime for datetime).497 :param kwargs: key-word arguments passed into the `Check` initializer.498 :returns: :class:`Check` object499 """500 if min_value is None:501 raise ValueError("min_value must not be None")502 def _greater_than(series: pd.Series) -> pd.Series:503 """Comparison function for check"""504 return series > min_value505 return cls(506 _greater_than,507 name=cls.greater_than.__name__,508 error=f"greater_than({min_value})",509 **kwargs,510 )511 gt = greater_than512 @classmethod513 @st.register_check_strategy(st.ge_strategy)514 @register_check_statistics(["min_value"])515 def greater_than_or_equal_to(cls, min_value, **kwargs) -> "Check":516 """Ensure all values are greater or equal a certain value.517 *New in version 0.4.5*518 Alias: ``ge``519 :param min_value: Allowed minimum value for values of a series. Must be520 a type comparable to the dtype of the :class:`pandas.Series` to be521 validated.522 :param kwargs: key-word arguments passed into the `Check` initializer.523 :returns: :class:`Check` object524 """525 if min_value is None:526 raise ValueError("min_value must not be None")527 def _greater_or_equal(series: pd.Series) -> pd.Series:528 """Comparison function for check"""529 return series >= min_value530 return cls(531 _greater_or_equal,532 name=cls.greater_than_or_equal_to.__name__,533 error=f"greater_than_or_equal_to({min_value})",534 **kwargs,535 )536 ge = greater_than_or_equal_to537 @classmethod538 @st.register_check_strategy(st.lt_strategy)539 @register_check_statistics(["max_value"])540 def less_than(cls, max_value, **kwargs) -> "Check":541 """Ensure values of a series are strictly below a maximum value.542 *New in version 0.4.5*543 Alias: ``lt``544 :param max_value: All elements of a series must be strictly smaller545 than this. Must be a type comparable to the dtype of the546 :class:`pandas.Series` to be validated.547 :param kwargs: key-word arguments passed into the `Check` initializer.548 :returns: :class:`Check` object549 """550 if max_value is None:551 raise ValueError("max_value must not be None")552 def _less_than(series: pd.Series) -> pd.Series:553 """Comparison function for check"""554 return series < max_value555 return cls(556 _less_than,557 name=cls.less_than.__name__,558 error=f"less_than({max_value})",559 **kwargs,560 )561 lt = less_than562 @classmethod563 @st.register_check_strategy(st.le_strategy)564 @register_check_statistics(["max_value"])565 def less_than_or_equal_to(cls, max_value, **kwargs) -> "Check":566 """Ensure values are less than or equal to a maximum value.567 *New in version 0.4.5*568 Alias: ``le``569 :param max_value: Upper bound not to be exceeded. Must be a type570 comparable to the dtype of the :class:`pandas.Series` to be571 validated.572 :param kwargs: key-word arguments passed into the `Check` initializer.573 :returns: :class:`Check` object574 """575 if max_value is None:576 raise ValueError("max_value must not be None")577 def _less_or_equal(series: pd.Series) -> pd.Series:578 """Comparison function for check"""579 return series <= max_value580 return cls(581 _less_or_equal,582 name=cls.less_than_or_equal_to.__name__,583 error=f"less_than_or_equal_to({max_value})",584 **kwargs,585 )586 le = less_than_or_equal_to587 @classmethod588 @st.register_check_strategy(st.in_range_strategy)589 @register_check_statistics(590 ["min_value", "max_value", "include_min", "include_max"]591 )592 def in_range(593 cls, min_value, max_value, include_min=True, include_max=True, **kwargs594 ) -> "Check":595 """Ensure all values of a series are within an interval.596 :param min_value: Left / lower endpoint of the interval.597 :param max_value: Right / upper endpoint of the interval. Must not be598 smaller than min_value.599 :param include_min: Defines whether min_value is also an allowed value600 (the default) or whether all values must be strictly greater than601 min_value.602 :param include_max: Defines whether min_value is also an allowed value603 (the default) or whether all values must be strictly smaller than604 max_value.605 :param kwargs: key-word arguments passed into the `Check` initializer.606 Both endpoints must be a type comparable to the dtype of the607 :class:`pandas.Series` to be validated.608 :returns: :class:`Check` object609 """610 if min_value is None:611 raise ValueError("min_value must not be None")612 if max_value is None:613 raise ValueError("max_value must not be None")614 if max_value < min_value or (615 min_value == max_value and (not include_min or not include_max)616 ):617 raise ValueError(618 f"The combination of min_value = {min_value} and max_value = {max_value} "619 "defines an empty interval!"620 )621 # Using functions from operator module to keep conditions out of the622 # closure623 left_op = operator.le if include_min else operator.lt624 right_op = operator.ge if include_max else operator.gt625 def _in_range(series: pd.Series) -> pd.Series:626 """Comparison function for check"""627 return left_op(min_value, series) & right_op(max_value, series)628 return cls(629 _in_range,630 name=cls.in_range.__name__,631 error=f"in_range({min_value}, {max_value})",632 **kwargs,633 )634 @classmethod635 @st.register_check_strategy(st.isin_strategy)636 @register_check_statistics(["allowed_values"])637 def isin(cls, allowed_values: Iterable, **kwargs) -> "Check":638 """Ensure only allowed values occur within a series.639 :param allowed_values: The set of allowed values. May be any iterable.640 :param kwargs: key-word arguments passed into the `Check` initializer.641 :returns: :class:`Check` object642 .. note::643 It is checked whether all elements of a :class:`pandas.Series`644 are part of the set of elements of allowed values. If allowed645 values is a string, the set of elements consists of all distinct646 characters of the string. Thus only single characters which occur647 in allowed_values at least once can meet this condition. If you648 want to check for substrings use :func:`Check.str_is_substring`.649 """650 # Turn allowed_values into a set. Not only for performance but also651 # avoid issues with a mutable argument passed by reference which may be652 # changed from outside.653 try:654 allowed_values = frozenset(allowed_values)655 except TypeError as exc:656 raise ValueError(657 f"Argument allowed_values must be iterable. Got {allowed_values}"658 ) from exc659 def _isin(series: pd.Series) -> pd.Series:660 """Comparison function for check"""661 return series.isin(allowed_values)662 return cls(663 _isin,664 name=cls.isin.__name__,665 error=f"isin({set(allowed_values)})",666 **kwargs,667 )668 @classmethod669 @st.register_check_strategy(st.notin_strategy)670 @register_check_statistics(["forbidden_values"])671 def notin(cls, forbidden_values: Iterable, **kwargs) -> "Check":672 """Ensure some defined values don't occur within a series.673 :param forbidden_values: The set of values which should not occur. May674 be any iterable.675 :param raise_warning: if True, check raises UserWarning instead of676 SchemaError on validation.677 :returns: :class:`Check` object678 .. note::679 Like :func:`Check.isin` this check operates on single characters if680 it is applied on strings. A string as paraforbidden_valuesmeter681 forbidden_values is understood as set of prohibited characters. Any682 string of length > 1 can't be in it by design.683 """684 # Turn forbidden_values into a set. Not only for performance but also685 # avoid issues with a mutable argument passed by reference which may be686 # changed from outside.687 try:688 forbidden_values = frozenset(forbidden_values)689 except TypeError as exc:690 raise ValueError(691 f"Argument forbidden_values must be iterable. Got {forbidden_values}"692 ) from exc693 def _notin(series: pd.Series) -> pd.Series:694 """Comparison function for check"""695 return ~series.isin(forbidden_values)696 return cls(697 _notin,698 name=cls.notin.__name__,699 error=f"notin({set(forbidden_values)})",700 **kwargs,701 )702 @classmethod703 @st.register_check_strategy(st.str_matches_strategy)704 @register_check_statistics(["pattern"])705 def str_matches(cls, pattern: str, **kwargs) -> "Check":706 """Ensure that string values match a regular expression.707 :param pattern: Regular expression pattern to use for matching708 :param kwargs: key-word arguments passed into the `Check` initializer.709 :returns: :class:`Check` object710 The behaviour is as of :func:`pandas.Series.str.match`.711 """712 # By compiling the regex we get the benefit of an early argument check713 try:714 regex = re.compile(pattern)715 except TypeError as exc:716 raise ValueError(717 f'pattern="{pattern}" cannot be compiled as regular expression'718 ) from exc719 def _match(series: pd.Series) -> pd.Series:720 """721 Check if all strings in the series match the regular expression.722 """723 return series.str.match(regex, na=False)724 return cls(725 _match,726 name=cls.str_matches.__name__,727 error=f"str_matches({regex})",728 **kwargs,729 )730 @classmethod731 @st.register_check_strategy(st.str_contains_strategy)732 @register_check_statistics(["pattern"])733 def str_contains(cls, pattern: str, **kwargs) -> "Check":734 """Ensure that a pattern can be found within each row.735 :param pattern: Regular expression pattern to use for searching736 :param kwargs: key-word arguments passed into the `Check` initializer.737 :returns: :class:`Check` object738 The behaviour is as of :func:`pandas.Series.str.contains`.739 """740 # By compiling the regex we get the benefit of an early argument check741 try:742 regex = re.compile(pattern)743 except TypeError as exc:744 raise ValueError(745 f'pattern="{pattern}" cannot be compiled as regular expression'746 ) from exc747 def _contains(series: pd.Series) -> pd.Series:748 """Check if a regex search is successful within each value"""749 return series.str.contains(regex, na=False)750 return cls(751 _contains,752 name=cls.str_contains.__name__,753 error=f"str_contains({regex})",754 **kwargs,755 )756 @classmethod757 @st.register_check_strategy(st.str_startswith_strategy)758 @register_check_statistics(["string"])759 def str_startswith(cls, string: str, **kwargs) -> "Check":760 """Ensure that all values start with a certain string.761 :param string: String all values should start with762 :param kwargs: key-word arguments passed into the `Check` initializer.763 :returns: :class:`Check` object764 """765 def _startswith(series: pd.Series) -> pd.Series:766 """Returns true only for strings starting with string"""767 return series.str.startswith(string, na=False)768 return cls(769 _startswith,770 name=cls.str_startswith.__name__,771 error=f"str_startswith({string})",772 **kwargs,773 )774 @classmethod775 @st.register_check_strategy(st.str_endswith_strategy)776 @register_check_statistics(["string"])777 def str_endswith(cls, string: str, **kwargs) -> "Check":778 """Ensure that all values end with a certain string.779 :param string: String all values should end with780 :param kwargs: key-word arguments passed into the `Check` initializer.781 :returns: :class:`Check` object782 """783 def _endswith(series: pd.Series) -> pd.Series:784 """Returns true only for strings ending with string"""785 return series.str.endswith(string, na=False)786 return cls(787 _endswith,788 name=cls.str_endswith.__name__,789 error=f"str_endswith({string})",790 **kwargs,791 )792 @classmethod793 @st.register_check_strategy(st.str_length_strategy)794 @register_check_statistics(["min_value", "max_value"])795 def str_length(796 cls, min_value: int = None, max_value: int = None, **kwargs797 ) -> "Check":798 """Ensure that the length of strings is within a specified range.799 :param min_value: Minimum length of strings (default: no minimum)800 :param max_value: Maximum length of strings (default: no maximum)801 :param kwargs: key-word arguments passed into the `Check` initializer.802 :returns: :class:`Check` object803 """804 if min_value is None and max_value is None:805 raise ValueError(806 "At least a minimum or a maximum need to be specified. Got "807 "None."...

Full Screen

Full Screen

extensions.py

Source:extensions.py Github

copy

Full Screen

...125 name=check_fn.__name__,126 **validate_check_kwargs(check_kwargs),127 )128 if strategy is not None:129 check_method = st.register_check_strategy(strategy)(check_method)130 Check.REGISTERED_CUSTOM_CHECKS[check_fn.__name__] = partial(131 check_method, Check132 )...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run pandera automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful