Skip to content

Commit 1b6b7e0

Browse files
committed
refactor: convenience functions for CovidcastEpidata
1 parent ab2a299 commit 1b6b7e0

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

epidatpy/_covidcast.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def to_df(signals: Iterable["DataSignal"]) -> DataFrame:
145145
],
146146
)
147147
df["geo_types"] = [",".join(s.geo_types.keys()) for s in signals]
148-
return df.set_index(["source", "signal"])
148+
return df
149149

150150
@property
151151
def key(self) -> Tuple[str, str]:
@@ -236,7 +236,7 @@ def to_df(sources: Iterable["DataSource"]) -> DataFrame:
236236
],
237237
)
238238
df["signals"] = [",".join(ss.signal for ss in s.signals) for s in sources]
239-
return df.set_index("source")
239+
return df
240240

241241
def get_signal(self, signal: str) -> Optional[DataSignal]:
242242
return next((s for s in self.signals if s.signal == signal), None)
@@ -266,10 +266,14 @@ def __post_init__(self) -> None:
266266
for signal in source.signals:
267267
self._signals_by_key[signal.key] = signal
268268

269-
@property
270269
def source_names(self) -> Sequence[str]:
271270
return [s.source for s in self.sources]
272271

272+
def signal_names(self, source: Optional[str] = None) -> Sequence[str]:
273+
if not source:
274+
return [x.signal for src in self._source_by_name.values() for x in src.signals]
275+
return [s.signal for s in self._source_by_name[source].signals]
276+
273277
@cached_property
274278
def source_df(self) -> DataFrame:
275279
"""Fetch metadata about available covidcast sources.

smoke_test.py

+20-15
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
from datetime import date
2+
from typing import cast
3+
4+
import pandas as pd
25

36
from epidatpy import CovidcastEpidata, Epidata, EpiRange
47

58
print("Epidata Test")
69
apicall = Epidata.pub_covidcast("fb-survey", "smoothed_cli", "nation", "day", "us", EpiRange(20210405, 20210410))
710

11+
# Call info
812
print(apicall)
13+
# URL
914
print(apicall.request_url())
10-
11-
classic = apicall.classic()
12-
print(classic)
13-
15+
# DataFrame
1416
df = apicall.df()
1517
print(df.columns)
1618
print(df.dtypes)
1719
print(df.iloc[0])
20+
print(df)
21+
# Classic
22+
classic = apicall.classic()
23+
# DataFrame
1824
df = apicall.df(disable_date_parsing=True)
1925
print(df.columns)
2026
print(df.dtypes)
2127
print(df.iloc[0])
2228

2329

2430
StagingEpidata = Epidata.with_base_url("https://staging.delphi.cmu.edu/epidata/")
25-
2631
epicall = StagingEpidata.pub_covidcast(
2732
"fb-survey", "smoothed_cli", "nation", "day", "*", EpiRange(date(2021, 4, 5), date(2021, 4, 10))
2833
)
@@ -32,23 +37,23 @@
3237
# Covidcast test
3338
print("Covidcast Test")
3439
epidata = CovidcastEpidata()
35-
print(epidata.source_names)
40+
epidata.source_df
41+
epidata.signal_df
42+
print(epidata.source_names())
43+
print(epidata.signal_names("hhs"))
44+
45+
# Print signals as df
3646
epidata["fb-survey"].signal_df
3747
apicall = epidata[("fb-survey", "smoothed_cli")].call(
3848
"nation",
3949
"us",
4050
EpiRange(20210405, 20210410),
4151
)
4252
print(apicall)
43-
44-
classic = apicall.classic()
45-
print(classic)
46-
47-
df = apicall.df()
48-
print(df.columns)
49-
print(df.dtypes)
50-
print(df.iloc[0])
51-
df = apicall.df(disable_date_parsing=True)
53+
df = cast(pd.DataFrame, apicall())
5254
print(df.columns)
5355
print(df.dtypes)
5456
print(df.iloc[0])
57+
58+
classic = apicall.classic()
59+
print(classic)

0 commit comments

Comments
 (0)