Best Python code snippet using pandera_python
test_typing.py
Source:test_typing.py  
...122class SchemaFieldCategoricalDtype(pa.SchemaModel):123    col: Series[pd.CategoricalDtype] = pa.Field(124        dtype_kwargs={"categories": ["b", "a"], "ordered": True}125    )126def _test_annotated_dtype(127    model: Type[pa.SchemaModel],128    dtype: Type,129    dtype_kwargs: Optional[Dict[str, Any]] = None,130):131    dtype_kwargs = dtype_kwargs or {}132    schema = model.to_schema()133    actual = schema.columns["col"].dtype134    expected = pa.Column(dtype(**dtype_kwargs), name="col").dtype135    assert actual == expected136def _test_default_annotated_dtype(137    model: Type[pa.SchemaModel], dtype: Type, has_mandatory_args: bool138):139    if has_mandatory_args:140        err_msg = "cannot be instantiated"141        with pytest.raises(TypeError, match=err_msg):142            model.to_schema()143    else:144        _test_annotated_dtype(model, dtype)145class SchemaFieldDatetimeTZDtype(pa.SchemaModel):146    col: Series[pd.DatetimeTZDtype] = pa.Field(147        dtype_kwargs={"unit": "ns", "tz": "EST"}148    )149class SchemaFieldIntervalDtype(pa.SchemaModel):150    col: Series[pd.IntervalDtype] = pa.Field(dtype_kwargs={"subtype": "int32"})151class SchemaFieldPeriodDtype(pa.SchemaModel):152    col: Series[pd.PeriodDtype] = pa.Field(dtype_kwargs={"freq": "D"})153class SchemaFieldSparseDtype(pa.SchemaModel):154    col: Series[pd.SparseDtype] = pa.Field(155        dtype_kwargs={"dtype": np.int32, "fill_value": 0}156    )157@pytest.mark.parametrize(158    "model, dtype, dtype_kwargs",159    [160        (161            SchemaFieldCategoricalDtype,162            pd.CategoricalDtype,163            {"categories": ["b", "a"], "ordered": True},164        ),165        (166            SchemaFieldDatetimeTZDtype,167            pd.DatetimeTZDtype,168            {"unit": "ns", "tz": "EST"},169        ),170        (SchemaFieldIntervalDtype, pd.IntervalDtype, {"subtype": "int32"}),171        (SchemaFieldPeriodDtype, pd.PeriodDtype, {"freq": "D"}),172        (173            SchemaFieldSparseDtype,174            pd.SparseDtype,175            {"dtype": np.int32, "fill_value": 0},176        ),177    ],178)179def test_parametrized_pandas_extension_dtype_field(180    model: Type[pa.SchemaModel], dtype: Type, dtype_kwargs: Dict[str, Any]181):182    """Test type annotations for parametrized pandas extension dtypes."""183    _test_annotated_dtype(model, dtype, dtype_kwargs)184class SchemaDefaultCategoricalDtype(pa.SchemaModel):185    col: Series[pd.CategoricalDtype]186class SchemaDefaultDatetimeTZDtype(pa.SchemaModel):187    col: Series[pd.DatetimeTZDtype]188class SchemaDefaultIntervalDtype(pa.SchemaModel):189    col: Series[pd.IntervalDtype]190class SchemaDefaultPeriodDtype(pa.SchemaModel):191    col: Series[pd.PeriodDtype]192class SchemaDefaultSparseDtype(pa.SchemaModel):193    col: Series[pd.SparseDtype]194@pytest.mark.parametrize(195    "model, dtype, has_mandatory_args",196    [197        (SchemaDefaultCategoricalDtype, pd.CategoricalDtype, False),198        # DatetimeTZDtype: tz is implictly required199        (SchemaDefaultDatetimeTZDtype, pd.DatetimeTZDtype, True),200        (SchemaDefaultIntervalDtype, pd.IntervalDtype, False),201        # PeriodDtype: freq is implicitely required -> str(pd.PeriodDtype())202        # raises AttributeError203        (SchemaDefaultPeriodDtype, pd.PeriodDtype, True),204        (SchemaDefaultSparseDtype, pd.SparseDtype, False),205    ],206)207def test_legacy_default_pandas_extension_dtype(208    model, dtype: pd.core.dtypes.base.ExtensionDtype, has_mandatory_args: bool209):210    """Test type annotations for default pandas extension dtypes."""211    _test_default_annotated_dtype(model, dtype, has_mandatory_args)212class SchemaAnnotatedCategoricalDtype(pa.SchemaModel):213    col: Series[Annotated[pd.CategoricalDtype, ["b", "a"], True]]214class SchemaAnnotatedDatetimeTZDtype(pa.SchemaModel):215    col: Series[Annotated[pd.DatetimeTZDtype, "ns", "est"]]216if pa.PANDAS_1_3_0_PLUS:217    class SchemaAnnotatedIntervalDtype(pa.SchemaModel):218        col: Series[Annotated[pd.IntervalDtype, "int32", "both"]]219else:220    class SchemaAnnotatedIntervalDtype(pa.SchemaModel):  # type: ignore221        col: Series[Annotated[pd.IntervalDtype, "int32"]]222    class SchemaAnnotatedPeriodDtype(pa.SchemaModel):223        col: Series[Annotated[pd.PeriodDtype, "D"]]224    class SchemaAnnotatedSparseDtype(pa.SchemaModel):225        col: Series[Annotated[pd.SparseDtype, np.int32, 0]]226    @pytest.mark.parametrize(227        "model, dtype, dtype_kwargs",228        [229            (230                SchemaAnnotatedCategoricalDtype,231                pd.CategoricalDtype,232                {"categories": ["b", "a"], "ordered": True},233            ),234            (235                SchemaAnnotatedDatetimeTZDtype,236                pd.DatetimeTZDtype,237                {"unit": "ns", "tz": "EST"},238            ),239            (240                SchemaAnnotatedIntervalDtype,241                pd.IntervalDtype,242                (243                    {"subtype": "int32", "closed": "both"}244                    if pa.PANDAS_1_3_0_PLUS245                    else {"subtype": "int32"}246                ),247            ),248            (SchemaAnnotatedPeriodDtype, pd.PeriodDtype, {"freq": "D"}),249            (250                SchemaAnnotatedSparseDtype,251                pd.SparseDtype,252                {"dtype": np.int32, "fill_value": 0},253            ),254        ],255    )256    def test_annotated_dtype(257        model: Type[pa.SchemaModel], dtype: Type, dtype_kwargs: Dict[str, Any]258    ):259        """Test type annotations for parametrized pandas extension dtypes."""260        _test_annotated_dtype(model, dtype, dtype_kwargs)261    class SchemaInvalidAnnotatedDtype(pa.SchemaModel):262        col: Series[Annotated[pd.DatetimeTZDtype, "utc"]]263    def test_invalid_annotated_dtype():264        """265        Test incorrect number of parameters for parametrized pandas extension266        dtypes.267        """268        err_msg = re.escape(269            "Annotation 'DatetimeTZDtype' requires all "270            r"positional arguments ['unit', 'tz']."271        )272        with pytest.raises(TypeError, match=err_msg):273            SchemaInvalidAnnotatedDtype.to_schema()274    class SchemaRedundantField(pa.SchemaModel):...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!!
