Skip to content

Commit a3eff48

Browse files
committed
add end date functionality to export csvs
1 parent e622e12 commit a3eff48

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

_delphi_utils_python/delphi_utils/export.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@
44
from os.path import join
55
from typing import Optional
66

7+
import numpy as np
78
import pandas as pd
89

910
def create_export_csv(
1011
df: pd.DataFrame,
1112
export_dir: str,
1213
geo_res: str,
1314
sensor: str,
15+
metric: Optional[str] = None,
1416
start_date: Optional[datetime] = None,
15-
metric: Optional[str] = None
17+
end_date: Optional[datetime] = None
1618
):
1719
"""Export data in the format expected by the Delphi API.
1820
@@ -26,18 +28,24 @@ def create_export_csv(
2628
Geographic resolution to which the data has been aggregated
2729
sensor: str
2830
Sensor that has been calculated (cumulative_counts vs new_counts)
29-
start_date: Optional[datetime]
30-
Earliest date to export. If None, all dates are exported.
3131
metric: Optional[str]
32-
Metric we are considering, if any
32+
Metric we are considering, if any.
33+
start_date: Optional[datetime]
34+
Earliest date to export or None if no minimum date restrictions should be applied.
35+
end_date: Optional[datetime]
36+
Latest date to export or None if no maximum date restrictions should be applied.
3337
"""
3438
df = df.copy()
3539

3640
df["timestamp"] = pd.to_datetime(df["timestamp"])
3741
if start_date is None:
3842
start_date = min(df["timestamp"])
43+
if end_date is None:
44+
end_date = max(df["timestamp"])
45+
3946
dates = pd.Series(
40-
df[df["timestamp"] >= start_date]["timestamp"].unique()
47+
df[np.logical_and(df["timestamp"] >= start_date,
48+
df["timestamp"] <= end_date)]["timestamp"].unique()
4149
).sort_values()
4250

4351
for date in dates:

_delphi_utils_python/tests/test_export.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,29 @@ def test_export_with_limiting_start_date(self):
112112
]
113113
)
114114

115-
def test_export_with_no_start_date(self):
116-
"""Test that omitting the `start_date` exports all dates."""
115+
def test_export_with_limiting_end_date(self):
116+
"""Test that the `end_date` prevents later dates from being exported."""
117+
118+
# Clean receiving directory
119+
_clean_directory(self.TEST_DIR)
120+
121+
create_export_csv(
122+
df=self.DF,
123+
end_date=datetime.strptime("2020-03-07", "%Y-%m-%d"),
124+
export_dir=self.TEST_DIR,
125+
geo_res="county",
126+
sensor="test",
127+
)
128+
129+
assert _non_ignored_files_set(self.TEST_DIR) == set(
130+
[
131+
"20200215_county_test.csv",
132+
"20200301_county_test.csv",
133+
]
134+
)
135+
136+
def test_export_with_no_dates(self):
137+
"""Test that omitting the `start_date` and `end_date` exports all dates."""
117138

118139
# Clean receiving directory
119140
_clean_directory(self.TEST_DIR)

0 commit comments

Comments
 (0)