Best Python code snippet using pandera_python
strategies.py
Source:strategies.py
...617 nanoseconds if given a pandas.Timestamp. We need to pass the unix epoch via618 the pandas.Timestamp.value attribute.619 """620 return st.builds(lambda x: np.datetime64(x.value, "ns"), strategy)621def field_element_strategy(622 pandera_dtype: Union[numpy_engine.DataType, pandas_engine.DataType],623 strategy: Optional[SearchStrategy] = None,624 *,625 checks: Optional[Sequence] = None,626) -> SearchStrategy:627 """Strategy to generate elements of a column or index.628 :param pandera_dtype: :class:`pandera.dtypes.DataType` instance.629 :param strategy: an optional hypothesis strategy. If specified, the630 pandas dtype strategy will be chained onto this strategy.631 :param checks: sequence of :class:`~pandera.checks.Check` s to constrain632 the values of the data in the column/index.633 :returns: ``hypothesis`` strategy634 """635 if strategy:636 raise BaseStrategyOnlyError(637 "The series strategy is a base strategy. You cannot specify the "638 "strategy argument to chain it to a parent strategy."639 )640 checks = [] if checks is None else checks641 elements = None642 def undefined_check_strategy(elements, check):643 """Strategy for checks with undefined strategies."""644 warnings.warn(645 "Element-wise check doesn't have a defined strategy."646 "Falling back to filtering drawn values based on the check "647 "definition. This can considerably slow down data-generation."648 )649 return (650 pandas_dtype_strategy(pandera_dtype)651 if elements is None652 else elements653 ).filter(check._check_fn)654 for check in checks:655 if hasattr(check, "strategy"):656 elements = check.strategy(pandera_dtype, elements)657 elif check.element_wise:658 elements = undefined_check_strategy(elements, check)659 # NOTE: vectorized checks with undefined strategies should be handled660 # by the series/dataframe strategy.661 if elements is None:662 elements = pandas_dtype_strategy(pandera_dtype)663 # Hypothesis only supports pure numpy datetime64 (i.e. timezone naive).664 # We cast to datetime64 after applying the check strategy so that checks665 # can see timezone-aware values.666 if _is_datetime_tz(pandera_dtype):667 elements = _timestamp_to_datetime64_strategy(elements)668 return elements669def series_strategy(670 pandera_dtype: Union[numpy_engine.DataType, pandas_engine.DataType],671 strategy: Optional[SearchStrategy] = None,672 *,673 checks: Optional[Sequence] = None,674 nullable: bool = False,675 unique: bool = False,676 name: Optional[str] = None,677 size: Optional[int] = None,678):679 """Strategy to generate a pandas Series.680 :param pandera_dtype: :class:`pandera.dtypes.DataType` instance.681 :param strategy: an optional hypothesis strategy. If specified, the682 pandas dtype strategy will be chained onto this strategy.683 :param checks: sequence of :class:`~pandera.checks.Check` s to constrain684 the values of the data in the column/index.685 :param nullable: whether or not generated Series contains null values.686 :param unique: whether or not generated Series contains unique values.687 :param name: name of the Series.688 :param size: number of elements in the Series.689 :returns: ``hypothesis`` strategy.690 """691 elements = field_element_strategy(pandera_dtype, strategy, checks=checks)692 strategy = (693 pdst.series(694 elements=elements,695 dtype=to_numpy_dtype(pandera_dtype),696 index=pdst.range_indexes(697 min_size=0 if size is None else size, max_size=size698 ),699 unique=unique,700 )701 .filter(lambda x: x.shape[0] > 0)702 .map(lambda x: x.rename(name))703 .map(lambda x: x.astype(pandera_dtype.type))704 )705 if nullable:706 strategy = null_field_masks(strategy)707 def undefined_check_strategy(strategy, check):708 """Strategy for checks with undefined strategies."""709 warnings.warn(710 "Vectorized check doesn't have a defined strategy."711 "Falling back to filtering drawn values based on the check "712 "definition. This can considerably slow down data-generation."713 )714 def _check_fn(series):715 return check(series).check_passed716 return strategy.filter(_check_fn)717 for check in checks if checks is not None else []:718 if not hasattr(check, "strategy") and not check.element_wise:719 strategy = undefined_check_strategy(strategy, check)720 return strategy721def column_strategy(722 pandera_dtype: Union[numpy_engine.DataType, pandas_engine.DataType],723 strategy: Optional[SearchStrategy] = None,724 *,725 checks: Optional[Sequence] = None,726 unique: bool = False,727 name: Optional[str] = None,728):729 # pylint: disable=line-too-long730 """Create a data object describing a column in a DataFrame.731 :param pandera_dtype: :class:`pandera.dtypes.DataType` instance.732 :param strategy: an optional hypothesis strategy. If specified, the733 pandas dtype strategy will be chained onto this strategy.734 :param checks: sequence of :class:`~pandera.checks.Check` s to constrain735 the values of the data in the column/index.736 :param unique: whether or not generated Series contains unique values.737 :param name: name of the Series.738 :returns: a `column <https://hypothesis.readthedocs.io/en/latest/numpy.html#hypothesis.extra.pandas.column>`_ object.739 """740 verify_dtype(pandera_dtype, schema_type="column", name=name)741 elements = field_element_strategy(pandera_dtype, strategy, checks=checks)742 return pdst.column(743 name=name,744 elements=elements,745 dtype=to_numpy_dtype(pandera_dtype),746 unique=unique,747 )748def index_strategy(749 pandera_dtype: Union[numpy_engine.DataType, pandas_engine.DataType],750 strategy: Optional[SearchStrategy] = None,751 *,752 checks: Optional[Sequence] = None,753 nullable: bool = False,754 unique: bool = False,755 name: Optional[str] = None,756 size: Optional[int] = None,757):758 """Strategy to generate a pandas Index.759 :param pandera_dtype: :class:`pandera.dtypes.DataType` instance.760 :param strategy: an optional hypothesis strategy. If specified, the761 pandas dtype strategy will be chained onto this strategy.762 :param checks: sequence of :class:`~pandera.checks.Check` s to constrain763 the values of the data in the column/index.764 :param nullable: whether or not generated Series contains null values.765 :param unique: whether or not generated Series contains unique values.766 :param name: name of the Series.767 :param size: number of elements in the Series.768 :returns: ``hypothesis`` strategy.769 """770 verify_dtype(pandera_dtype, schema_type="index", name=name)771 elements = field_element_strategy(pandera_dtype, strategy, checks=checks)772 strategy = pdst.indexes(773 elements=elements,774 dtype=to_numpy_dtype(pandera_dtype),775 min_size=0 if size is None else size,776 max_size=size,777 unique=unique,778 ).map(lambda x: x.astype(pandera_dtype.type))779 # this is a hack to convert np.str_ data values into native python str.780 col_dtype = str(pandera_dtype)781 if col_dtype in {"object", "str"} or col_dtype.startswith("string"):782 # pylint: disable=cell-var-from-loop,undefined-loop-variable783 strategy = strategy.map(lambda index: index.map(str))784 if name is not None:785 strategy = strategy.map(lambda index: index.rename(name))...
test_strategies.py
Source:test_strategies.py
...584 example = multiindex.example()585 multiindex(pd.DataFrame(index=example))586@pytest.mark.parametrize("data_type", NULLABLE_DTYPES)587@hypothesis.given(st.data())588def test_field_element_strategy(data_type, data):589 """Test strategy for generating elements in columns/indexes."""590 strategy = strategies.field_element_strategy(data_type)591 element = data.draw(strategy)592 expected_type = strategies.to_numpy_dtype(data_type).type593 assert element.dtype.type == expected_type594 with pytest.raises(pa.errors.BaseStrategyOnlyError):595 strategies.field_element_strategy(596 data_type, strategies.pandas_dtype_strategy(data_type)597 )598@pytest.mark.parametrize("data_type", NULLABLE_DTYPES)599@pytest.mark.parametrize(600 "field_strategy",601 [strategies.index_strategy, strategies.series_strategy],602)603@pytest.mark.parametrize("nullable", [True, False])604@hypothesis.given(st.data())605@hypothesis.settings(606 suppress_health_check=[hypothesis.HealthCheck.too_slow],607)608def test_check_nullable_field_strategy(609 data_type, field_strategy, nullable, data...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!