How to use _test_default_annotated_dtype method in pandera

Best Python code snippet using pandera_python

test_typing.py

Source:test_typing.py Github

copy

Full Screen

...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):275 col: Series[Annotated[pd.DatetimeTZDtype, "utc"]] = pa.Field(276 dtype_kwargs={"tz": "utc"}277 )278 def test_pandas_extension_dtype_redundant_field():279 """280 Test incorrect number of parameters for parametrized pandas extension281 dtypes.282 """283 err_msg = r"Cannot specify redundant 'dtype_kwargs' for"284 with pytest.raises(TypeError, match=err_msg):285 SchemaRedundantField.to_schema()286class SchemaInt8Dtype(pa.SchemaModel):287 col: Series[pd.Int8Dtype]288class SchemaInt16Dtype(pa.SchemaModel):289 col: Series[pd.Int16Dtype]290class SchemaInt32Dtype(pa.SchemaModel):291 col: Series[pd.Int32Dtype]292class SchemaInt64Dtype(pa.SchemaModel):293 col: Series[pd.Int64Dtype]294class SchemaUInt8Dtype(pa.SchemaModel):295 col: Series[pd.UInt8Dtype]296class SchemaUInt16Dtype(pa.SchemaModel):297 col: Series[pd.UInt16Dtype]298class SchemaUInt32Dtype(pa.SchemaModel):299 col: Series[pd.UInt32Dtype]300class SchemaUInt64Dtype(pa.SchemaModel):301 col: Series[pd.UInt64Dtype]302class SchemaStringDtype(pa.SchemaModel):303 col: Series[pd.StringDtype]304class SchemaBooleanDtype(pa.SchemaModel):305 col: Series[pd.BooleanDtype]306@pytest.mark.parametrize(307 "model, dtype, has_mandatory_args",308 [309 (SchemaInt8Dtype, pd.Int8Dtype, False),310 (SchemaInt16Dtype, pd.Int16Dtype, False),311 (SchemaInt32Dtype, pd.Int32Dtype, False),312 (SchemaInt64Dtype, pd.Int64Dtype, False),313 (SchemaUInt8Dtype, pd.UInt8Dtype, False),314 (SchemaUInt16Dtype, pd.UInt16Dtype, False),315 (SchemaUInt32Dtype, pd.UInt32Dtype, False),316 (SchemaUInt64Dtype, pd.UInt64Dtype, False),317 (SchemaStringDtype, pd.StringDtype, False),318 (SchemaBooleanDtype, pd.BooleanDtype, False),319 ],320)321def test_new_pandas_extension_dtype_class(322 model,323 dtype: pd.core.dtypes.base.ExtensionDtype,324 has_mandatory_args: bool,325):326 """Test type annotations with the new nullable pandas dtypes."""327 _test_default_annotated_dtype(model, dtype, has_mandatory_args)328class InitSchema(pa.SchemaModel):329 col1: Series[int]330 col2: Series[float]331 col3: Series[str]332 index: Index[int]333def test_init_pandas_dataframe():334 """Test initialization of pandas.typing.DataFrame with Schema."""335 assert isinstance(336 DataFrame[InitSchema]({"col1": [1], "col2": [1.0], "col3": ["1"]}),337 DataFrame,338 )339@pytest.mark.parametrize(340 "invalid_data",341 [...

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