Skip to content

Commit 1b52ae5

Browse files
committed
Update: add new field type to support mixed time types.
1 parent 7f77a77 commit 1b52ae5

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

delphi_epidata/_covidcast.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def define_covidcast_fields() -> List[EpidataFieldInfo]:
7070
),
7171
EpidataFieldInfo("geo_value", EpidataFieldType.text),
7272
EpidataFieldInfo("time_type", EpidataFieldType.categorical, categories=list(get_args(TimeType))),
73-
EpidataFieldInfo("time_value", EpidataFieldType.date),
73+
EpidataFieldInfo("time_value", EpidataFieldType.date_or_epiweek),
7474
EpidataFieldInfo("issue", EpidataFieldType.date),
7575
EpidataFieldInfo("lag", EpidataFieldType.int),
7676
EpidataFieldInfo("value", EpidataFieldType.float),

delphi_epidata/_endpoints.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,8 @@ def covidcast_meta(self) -> CALL_TYPE:
355355
EpidataFieldInfo("data_source", EpidataFieldType.text),
356356
EpidataFieldInfo("signal", EpidataFieldType.text),
357357
EpidataFieldInfo("time_type", EpidataFieldType.categorical, categories=["week", "day"]),
358-
EpidataFieldInfo("min_time", EpidataFieldType.date),
359-
EpidataFieldInfo("max_time", EpidataFieldType.date),
358+
EpidataFieldInfo("min_time", EpidataFieldType.date_or_epiweek),
359+
EpidataFieldInfo("max_time", EpidataFieldType.date_or_epiweek),
360360
EpidataFieldInfo("num_locations", EpidataFieldType.int),
361361
EpidataFieldInfo("min_value", EpidataFieldType.float),
362362
EpidataFieldInfo("max_value", EpidataFieldType.float),

delphi_epidata/_model.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from epiweeks import Week
2222
from pandas import DataFrame, CategoricalDtype
2323

24-
from ._parse import parse_api_date, parse_api_week, fields_to_predicate
24+
from ._parse import parse_api_date, parse_api_week, parse_api_date_or_week, fields_to_predicate
2525

2626
EpiDateLike = Union[int, str, date, Week]
2727
EpiRangeDict = TypedDict("EpiRangeDict", {"from": EpiDateLike, "to": EpiDateLike})
@@ -124,6 +124,7 @@ class EpidataFieldType(Enum):
124124
epiweek = 4
125125
categorical = 5
126126
bool = 6
127+
date_or_epiweek = 7
127128

128129

129130
@dataclass
@@ -235,6 +236,8 @@ def _parse_value(
235236
meta = self.meta_by_name.get(key)
236237
if not meta or value is None:
237238
return value
239+
if meta.type == EpidataFieldType.date_or_epiweek and not disable_date_parsing:
240+
return parse_api_date_or_week(value)
238241
if meta.type == EpidataFieldType.date and not disable_date_parsing:
239242
return parse_api_date(value)
240243
if meta.type == EpidataFieldType.epiweek and not disable_date_parsing:
@@ -270,7 +273,7 @@ def _as_df(
270273
data_types[info.name] = CategoricalDtype(categories=info.categories or None, ordered=True)
271274
elif info.type == EpidataFieldType.int:
272275
data_types[info.name] = int
273-
elif info.type in (EpidataFieldType.date, EpidataFieldType.epiweek):
276+
elif info.type in (EpidataFieldType.date, EpidataFieldType.epiweek, EpidataFieldType.date_or_epiweek):
274277
data_types[info.name] = int if disable_date_parsing else "datetime64"
275278
elif info.type == EpidataFieldType.float:
276279
data_types[info.name] = float

delphi_epidata/_parse.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ def parse_api_week(value: Union[str, int, float, None]) -> Optional[date]:
1818
return cast(date, Week.fromstring(str(value)).startdate())
1919

2020

21+
def parse_api_date_or_week(value: Union[str, int, float, None]) -> Optional[date]:
22+
if value is None:
23+
return None
24+
v = str(value)
25+
if len(v) == 6:
26+
return cast(date, Week.fromstring(v).startdate())
27+
if len(v) == 8:
28+
return datetime.strptime(v, "%Y%m%d").date()
29+
30+
2131
def fields_to_predicate(fields: Optional[Iterable[str]] = None) -> Callable[[str], bool]:
2232
if not fields:
2333
return lambda _: True

0 commit comments

Comments
 (0)