Best Python code snippet using pandera_python
test_schema_inference.py
Source:test_schema_inference.py
...97 pd.Series(list("abcdefg"), dtype="category"),98 pd.Series(pd.to_datetime(["20180101", "20180102", "20180103"])),99 ],100)101def test_infer_series_schema(series: pd.Series) -> None:102 """Test series schema is correctly inferred."""103 schema = schema_inference.infer_series_schema(series)104 assert isinstance(schema, pa.SeriesSchema)105 with pytest.warns(106 UserWarning,107 match="^This .+ is an inferred schema that hasn't been modified",108 ):109 schema.validate(series)110 # modifying an inferred schema should set _is_inferred to False111 schema_with_new_checks = schema.set_checks(112 [pa.Check(lambda x: x is not None)]113 )114 assert schema._is_inferred115 assert not schema_with_new_checks._is_inferred...
schema_inference.py
Source:schema_inference.py
...18 """19 if isinstance(pandas_obj, pd.DataFrame):20 return infer_dataframe_schema(pandas_obj)21 elif isinstance(pandas_obj, pd.Series):22 return infer_series_schema(pandas_obj)23 else:24 raise TypeError(25 "pandas_obj type not recognized. Expected a pandas DataFrame or "26 f"Series, found {type(pandas_obj)}"27 )28def _create_index(index_statistics):29 index = [30 Index(31 properties["dtype"],32 checks=parse_check_statistics(properties["checks"]),33 nullable=properties["nullable"],34 name=properties["name"],35 )36 for properties in index_statistics37 ]38 if len(index) == 1:39 index = index[0] # type: ignore40 else:41 index = MultiIndex(index) # type: ignore42 return index43def infer_dataframe_schema(df: pd.DataFrame) -> DataFrameSchema:44 """Infer a DataFrameSchema from a pandas DataFrame.45 :param df: DataFrame object to infer.46 :returns: DataFrameSchema47 """48 df_statistics = infer_dataframe_statistics(df)49 schema = DataFrameSchema(50 columns={51 colname: Column(52 properties["dtype"],53 checks=parse_check_statistics(properties["checks"]),54 nullable=properties["nullable"],55 )56 for colname, properties in df_statistics["columns"].items()57 },58 index=_create_index(df_statistics["index"]),59 coerce=True,60 )61 schema._is_inferred = True62 return schema63def infer_series_schema(series) -> SeriesSchema:64 """Infer a SeriesSchema from a pandas DataFrame.65 :param series: Series object to infer.66 :returns: SeriesSchema67 """68 series_statistics = infer_series_statistics(series)69 schema = SeriesSchema(70 dtype=series_statistics["dtype"],71 checks=parse_check_statistics(series_statistics["checks"]),72 nullable=series_statistics["nullable"],73 name=series_statistics["name"],74 coerce=True,75 )76 schema._is_inferred = True77 return schema
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!!