|
| 1 | +from typing import Iterable, Optional, Union |
| 2 | + |
| 3 | +import pandas as pd |
1 | 4 | import pandera.pandas as pa |
| 5 | +from pandera import dtypes |
| 6 | +from pandera.engines import pandas_engine |
| 7 | + |
| 8 | + |
| 9 | +@pandas_engine.Engine.register_dtype |
| 10 | +@dtypes.immutable |
| 11 | +class FixedLenArray(pandas_engine.NpString): |
| 12 | + def check( |
| 13 | + self, |
| 14 | + pandera_dtype: dtypes.DataType, |
| 15 | + data_container: Optional[pd.Series] = None, |
| 16 | + ) -> Union[bool, Iterable[bool]]: |
| 17 | + # ensure that the data container's data type is a string, |
| 18 | + # using the parent class's check implementation |
| 19 | + correct_type = super().check(pandera_dtype) |
| 20 | + if not correct_type: |
| 21 | + return correct_type |
| 22 | + |
| 23 | + # ensure the filepaths actually exist locally |
| 24 | + if data_container is None: |
| 25 | + return True |
| 26 | + else: |
| 27 | + length = len(data_container[0]) |
| 28 | + return data_container.map(lambda x: len(x) == length) |
| 29 | + |
| 30 | + def __str__(self) -> str: |
| 31 | + return str(self.__class__.__name__) |
| 32 | + |
| 33 | + def __repr__(self) -> str: |
| 34 | + return f"DataType({self})" |
| 35 | + |
2 | 36 |
|
3 | 37 | extract_schema = pa.DataFrameSchema( |
4 | 38 | { |
5 | | - "date": pa.Column(str), |
6 | | - "location": pa.Column(str), |
7 | | - "demographic_category": pa.Column(str), |
8 | | - "census": pa.Column(float, nullable=True), |
9 | | - "administered_dose1": pa.Column(float, nullable=True), |
10 | | - "series_complete_yes": pa.Column(float, nullable=True), |
11 | | - "booster_doses": pa.Column(float, nullable=True), |
12 | | - "second_booster": pa.Column(float, nullable=True), |
13 | | - "administered_dose1_pct_agegroup": pa.Column(float, nullable=True), |
14 | | - "series_complete_pop_pct_agegroup": pa.Column(float, nullable=True), |
15 | | - "booster_doses_vax_pct_agegroup": pa.Column(float, nullable=True), |
16 | | - "second_booster_vax_pct_agegroup": pa.Column(float, nullable=True), |
| 39 | + "date": pa.Column( |
| 40 | + str, |
| 41 | + checks=[ |
| 42 | + pa.Check.str_matches( |
| 43 | + r"^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{3}$" |
| 44 | + ) |
| 45 | + ], |
| 46 | + ), |
| 47 | + "location": pa.Column( |
| 48 | + str, checks=[pa.Check.str_matches(r"^[A-Z]{2}$")] |
| 49 | + ), |
| 50 | + "demographic_category": pa.Column( |
| 51 | + str, checks=[pa.Check.str_matches(r"^[A-Za-z0-9\_\-]+$")] |
| 52 | + ), |
| 53 | + "census": pa.Column( |
| 54 | + int, checks=[pa.Check.in_range(0, 10_000_000_000)], nullable=True |
| 55 | + ), |
| 56 | + "administered_dose1": pa.Column( |
| 57 | + int, checks=[pa.Check.in_range(0, 10_000_000_000)], nullable=True |
| 58 | + ), |
| 59 | + "series_complete_yes": pa.Column( |
| 60 | + int, checks=[pa.Check.in_range(0, 10_000_000_000)], nullable=True |
| 61 | + ), |
| 62 | + "booster_doses": pa.Column( |
| 63 | + int, checks=[pa.Check.in_range(0, 10_000_000_000)], nullable=True |
| 64 | + ), |
| 65 | + "second_booster": pa.Column( |
| 66 | + int, checks=[pa.Check.in_range(0, 10_000_000_000)], nullable=True |
| 67 | + ), |
| 68 | + "administered_dose1_pct_agegroup": pa.Column( |
| 69 | + float, checks=[pa.Check.in_range(0, 100)], nullable=True |
| 70 | + ), |
| 71 | + "series_complete_pop_pct_agegroup": pa.Column( |
| 72 | + float, checks=[pa.Check.in_range(0, 100)], nullable=True |
| 73 | + ), |
| 74 | + "booster_doses_vax_pct_agegroup": pa.Column( |
| 75 | + float, checks=[pa.Check.in_range(0, 100)], nullable=True |
| 76 | + ), |
| 77 | + "second_booster_vax_pct_agegroup": pa.Column( |
| 78 | + float, checks=[pa.Check.in_range(0, 100)], nullable=True |
| 79 | + ), |
17 | 80 | } |
18 | 81 | ) |
19 | 82 |
|
20 | 83 | load_schema = pa.DataFrameSchema( |
21 | 84 | { |
22 | | - "date": pa.Column(str), |
23 | | - "state": pa.Column(str), |
24 | | - "age": pa.Column(object), |
25 | | - "census": pa.Column(int, coerce=True), |
26 | | - "total": pa.Column(object), |
27 | | - "percentage": pa.Column(object), |
28 | | - "dose": pa.Column(object), |
| 85 | + "date": pa.Column( |
| 86 | + str, |
| 87 | + checks=[ |
| 88 | + pa.Check.str_matches( |
| 89 | + r"^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{3}$" |
| 90 | + ) |
| 91 | + ], |
| 92 | + ), |
| 93 | + "state": pa.Column(str, checks=[pa.Check.str_matches(r"^[A-Z]{2}$")]), |
| 94 | + "age": pa.Column( |
| 95 | + str, checks=[pa.Check.isin(["65+", "18-49", "0-17", "50-64"])] |
| 96 | + ), |
| 97 | + "census": pa.Column( |
| 98 | + int, checks=[pa.Check.in_range(0, 10_000_000_000)], coerce=True |
| 99 | + ), |
| 100 | + "total": pa.Column(FixedLenArray), |
| 101 | + "percentage": pa.Column(FixedLenArray), |
| 102 | + "dose": pa.Column(FixedLenArray), |
29 | 103 | } |
30 | 104 | ) |
0 commit comments