From 0533d354b51f0eb544d436730b26c26baa22a502 Mon Sep 17 00:00:00 2001 From: chinandrew Date: Wed, 7 Oct 2020 23:48:07 -0700 Subject: [PATCH 1/4] Reorder and update metadata docstring --- .../covidcast-py/covidcast/covidcast.py | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/Python-packages/covidcast-py/covidcast/covidcast.py b/Python-packages/covidcast-py/covidcast/covidcast.py index 5c359b06..c951c56b 100644 --- a/Python-packages/covidcast-py/covidcast/covidcast.py +++ b/Python-packages/covidcast-py/covidcast/covidcast.py @@ -205,11 +205,9 @@ def metadata() -> pd.DataFrame: ``signal`` Signal name. - ``min_time`` - First day for which this signal is available. - - ``max_time`` - Most recent day for which this signal is available. + ``time_type`` + Temporal resolution at which this signal is reported. "day", for + example, means the signal is reported daily. ``geo_type`` Geographic level for which this signal is available, such as county, @@ -217,9 +215,11 @@ def metadata() -> pd.DataFrame: levels and will hence be listed in multiple rows with their own metadata. - ``time_type`` - Temporal resolution at which this signal is reported. "day", for - example, means the signal is reported daily. + ``min_time`` + First day for which this signal is available. + + ``max_time`` + Most recent day for which this signal is available. ``num_locations`` Number of distinct geographic locations available for this signal. For @@ -238,6 +238,17 @@ def metadata() -> pd.DataFrame: ``stdev_value`` The sample standard deviation of all reported values. + ``last_update`` + The Unix time for when the signal value was last updated. + + ``max_issue`` + Most recent date data was issued. + + ``min_lag`` + Smallest lag from observation to issue, in days. + + ``max_lag`` + Largest lag from observation to issue, in days. """ meta = Epidata.covidcast_meta() From da81a2b87deb5f751fd225af4150ed8a6536b588 Mon Sep 17 00:00:00 2001 From: chinandrew Date: Thu, 8 Oct 2020 18:46:55 -0700 Subject: [PATCH 2/4] convert to datetime --- .../covidcast-py/covidcast/covidcast.py | 6 ++--- .../tests/covidcast/test_covidcast.py | 24 ++++++++++--------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/Python-packages/covidcast-py/covidcast/covidcast.py b/Python-packages/covidcast-py/covidcast/covidcast.py index c951c56b..baa5a130 100644 --- a/Python-packages/covidcast-py/covidcast/covidcast.py +++ b/Python-packages/covidcast-py/covidcast/covidcast.py @@ -1,6 +1,6 @@ """This is the client side library for accessing the COVIDcast API.""" import warnings -from datetime import timedelta, date +from datetime import timedelta, date, datetime from typing import Union, Iterable, Tuple, List from functools import reduce @@ -239,7 +239,7 @@ def metadata() -> pd.DataFrame: The sample standard deviation of all reported values. ``last_update`` - The Unix time for when the signal value was last updated. + The UTC datetime for when the signal value was last updated. ``max_issue`` Most recent date data was issued. @@ -261,7 +261,7 @@ def metadata() -> pd.DataFrame: meta_df = pd.DataFrame.from_dict(meta["epidata"]) meta_df["min_time"] = pd.to_datetime(meta_df["min_time"], format="%Y%m%d") meta_df["max_time"] = pd.to_datetime(meta_df["max_time"], format="%Y%m%d") - + meta_df["last_update"] = meta_df.last_update.map(datetime.utcfromtimestamp) return meta_df diff --git a/Python-packages/covidcast-py/tests/covidcast/test_covidcast.py b/Python-packages/covidcast-py/tests/covidcast/test_covidcast.py index f6fa38fe..ad8a4f2c 100644 --- a/Python-packages/covidcast-py/tests/covidcast/test_covidcast.py +++ b/Python-packages/covidcast-py/tests/covidcast/test_covidcast.py @@ -72,19 +72,21 @@ def test_signal(mock_covidcast, mock_metadata): @patch("delphi_epidata.Epidata.covidcast_meta") def test_metadata(mock_covidcast_meta): # not generating full DF since most attributes used - mock_covidcast_meta.side_effect = [{"result": 1, # successful API response - "epidata": [{"max_time": 20200622, "min_time": 20200421}, - {"max_time": 20200724, "min_time": 20200512}], - "message": "success"}, - {"result": 0, # unsuccessful API response - "epidata": [{"max_time": 20200622, "min_time": 20200421}, - {"max_time": 20200724, "min_time": 20200512}], - "message": "error: failed"}] - + mock_covidcast_meta.side_effect = [ + {"result": 1, # successful API response + "epidata": [{"max_time": 20200622, "min_time": 20200421, "last_update": 12345}, + {"max_time": 20200724, "min_time": 20200512, "last_update": 99999}], + "message": "success"}, + {"result": 0, # unsuccessful API response + "epidata": [{"max_time": 20200622, "min_time": 20200421}, + {"max_time": 20200724, "min_time": 20200512}], + "message": "error: failed"}] # test happy path response = covidcast.metadata() - expected = pd.DataFrame({"max_time": [datetime(2020, 6, 22), datetime(2020, 7, 24)], - "min_time": [datetime(2020, 4, 21), datetime(2020, 5, 12)]}) + expected = pd.DataFrame({ + "max_time": [datetime(2020, 6, 22), datetime(2020, 7, 24)], + "min_time": [datetime(2020, 4, 21), datetime(2020, 5, 12)], + "last_update": [datetime(1970, 1, 1, 3, 25, 45), datetime(1970, 1, 2, 3, 46, 39)]}) assert sort_df(response).equals(sort_df(expected)) # test failed response raises RuntimeError From cd93f81deb5c8d81dd5e80d329436e666517777b Mon Sep 17 00:00:00 2001 From: chinandrew Date: Thu, 8 Oct 2020 18:50:18 -0700 Subject: [PATCH 3/4] Use pandas to_datetime --- Python-packages/covidcast-py/covidcast/covidcast.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Python-packages/covidcast-py/covidcast/covidcast.py b/Python-packages/covidcast-py/covidcast/covidcast.py index baa5a130..6d7f8ac9 100644 --- a/Python-packages/covidcast-py/covidcast/covidcast.py +++ b/Python-packages/covidcast-py/covidcast/covidcast.py @@ -261,7 +261,7 @@ def metadata() -> pd.DataFrame: meta_df = pd.DataFrame.from_dict(meta["epidata"]) meta_df["min_time"] = pd.to_datetime(meta_df["min_time"], format="%Y%m%d") meta_df["max_time"] = pd.to_datetime(meta_df["max_time"], format="%Y%m%d") - meta_df["last_update"] = meta_df.last_update.map(datetime.utcfromtimestamp) + meta_df["last_update"] = pd.to_datetime(meta_df["last_update"], unit="s") return meta_df From 7d0105fe2dd72d56d53a2296c4813d5ad6203216 Mon Sep 17 00:00:00 2001 From: chinandrew Date: Thu, 8 Oct 2020 18:52:59 -0700 Subject: [PATCH 4/4] Remove unused import --- Python-packages/covidcast-py/covidcast/covidcast.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Python-packages/covidcast-py/covidcast/covidcast.py b/Python-packages/covidcast-py/covidcast/covidcast.py index 6d7f8ac9..aa63649a 100644 --- a/Python-packages/covidcast-py/covidcast/covidcast.py +++ b/Python-packages/covidcast-py/covidcast/covidcast.py @@ -1,8 +1,8 @@ """This is the client side library for accessing the COVIDcast API.""" import warnings -from datetime import timedelta, date, datetime -from typing import Union, Iterable, Tuple, List +from datetime import timedelta, date from functools import reduce +from typing import Union, Iterable, Tuple, List import pandas as pd from delphi_epidata import Epidata