How to use _dataframe_strategy method in pandera

Best Python code snippet using pandera_python

strategies.py

Source:strategies.py Github

copy

Full Screen

...864 if strategy is None:865 strategy = pandas_dtype_strategy(col.dtype)866 return strategy867 @composite868 def _dataframe_strategy(draw):869 row_strategy_checks = []870 undefined_strat_df_checks = []871 for check in checks:872 if hasattr(check, "strategy") or check.element_wise:873 # we can apply element-wise checks defined at the dataframe874 # level to the row strategy875 row_strategy_checks.append(check)876 else:877 undefined_strat_df_checks.append(check)878 # expand column set to generate column names for columns where879 # regex=True.880 expanded_columns = {}881 for col_name, column in columns.items():882 if unique and col_name in unique:883 # if the column is in the set of columns specified in `unique`,884 # make the column strategy independently unique. This is885 # technically stricter than it should be, since the list of886 # columns in `unique` are required to be jointly unique, but887 # this is a simple solution that produces synthetic data that888 # fulfills the uniqueness constraints of the dataframe.889 column = deepcopy(column)890 column.unique = True891 if not column.regex:892 expanded_columns[col_name] = column893 else:894 regex_columns = draw(895 st.lists(896 st.from_regex(column.name, fullmatch=True),897 min_size=n_regex_columns,898 max_size=n_regex_columns,899 unique=True,900 )901 )902 for regex_col in regex_columns:903 expanded_columns[regex_col] = deepcopy(column).set_name(904 regex_col905 )906 # collect all non-element-wise column checks with undefined strategies907 undefined_strat_column_checks: Dict[str, list] = defaultdict(list)908 for col_name, column in expanded_columns.items():909 undefined_strat_column_checks[col_name].extend(910 check911 for check in column.checks912 if not hasattr(check, "strategy") and not check.element_wise913 )914 # override the column datatype with dataframe-level datatype if915 # specified916 col_dtypes = {917 col_name: str(col.dtype)918 if pandera_dtype is None919 else str(pandera_dtype)920 for col_name, col in expanded_columns.items()921 }922 nullable_columns = {923 col_name: col.nullable924 for col_name, col in expanded_columns.items()925 }926 row_strategy = None927 if row_strategy_checks:928 row_strategy = st.fixed_dictionaries(929 {930 col_name: make_row_strategy(col, row_strategy_checks)931 for col_name, col in expanded_columns.items()932 }933 )934 strategy = pdst.data_frames(935 columns=[936 column.strategy_component()937 for column in expanded_columns.values()938 ],939 rows=row_strategy,940 index=pdst.range_indexes(941 min_size=0 if size is None else size, max_size=size942 ),943 )944 # this is a hack to convert np.str_ data values into native python str.945 for col_name, col_dtype in col_dtypes.items():946 if col_dtype in {"object", "str"} or col_dtype.startswith(947 "string"948 ):949 # pylint: disable=cell-var-from-loop,undefined-loop-variable950 strategy = strategy.map(951 lambda df: df.assign(**{col_name: df[col_name].map(str)})952 )953 strategy = strategy.map(954 lambda df: df if df.empty else df.astype(col_dtypes)955 )956 if size is not None and size > 0 and any(nullable_columns.values()):957 strategy = null_dataframe_masks(strategy, nullable_columns)958 if index is not None:959 strategy = set_pandas_index(strategy, index)960 for check in undefined_strat_df_checks:961 strategy = undefined_check_strategy(strategy, check)962 for col_name, column_checks in undefined_strat_column_checks.items():963 for check in column_checks: # type: ignore964 strategy = undefined_check_strategy(965 strategy, check, column=col_name966 )967 return draw(strategy)968 return _dataframe_strategy()969# pylint: disable=unused-argument970def multiindex_strategy(971 pandera_dtype: Optional[DataType] = None,972 strategy: Optional[SearchStrategy] = None,973 *,974 indexes: Optional[List] = None,975 size: Optional[int] = None,976):977 """Strategy to generate a pandas MultiIndex object.978 :param pandera_dtype: :class:`pandera.dtypes.DataType` instance.979 :param strategy: an optional hypothesis strategy. If specified, the980 pandas dtype strategy will be chained onto this strategy.981 :param indexes: a list of :class:`~pandera.schema_components.Index`982 objects....

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