Skip to content

Commit 16fabfc

Browse files
authored
fix unsorted series in build_time_series function, change doc string … (#172)
* fix unsorted series in build_time_series function, change doc string to add sorted timeseries * add unittest for build_time_series, changed build_time_series to avoid warning thrown
1 parent 347d4d3 commit 16fabfc

2 files changed

Lines changed: 37 additions & 4 deletions

File tree

datacommons_pandas/df_builder.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,15 @@ def build_time_series(place,
4646
scaling_factor (`int`): Optional, the preferred `scalingFactor` value.
4747
Returns:
4848
A pandas Series with Place IDs as the index and observed statistics as
49-
values, representing a time series satisfying all optional args.
49+
values, representing a sorted time series satisfying all optional args.
5050
"""
51-
return pd.Series(
52-
dc.get_stat_series(place, stat_var, measurement_method,
53-
observation_period, unit, scaling_factor))
51+
result_dict = dc.get_stat_series(place, stat_var, measurement_method,observation_period, unit, scaling_factor)
52+
53+
# Explicit dtype to avoid warning thrown by pd.Series({})
54+
if not result_dict:
55+
return pd.Series(result_dict,dtype=object)
56+
else:
57+
return pd.Series(result_dict).sort_index()
5458

5559

5660
def _group_stat_all_by_obs_options(places, stat_vars, keep_series=True):

datacommons_pandas/test/df_builder_test.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import unittest
3232
import six
3333
import six.moves.urllib as urllib
34+
import pandas as pd
3435

3536
# Reusable parts of REST API /stat/all response.
3637
CA_COUNT_PERSON = {
@@ -219,6 +220,34 @@ def read(self):
219220
return urllib.error.HTTPError(None, 404, None, None, None)
220221

221222

223+
class TestBuildTimeSeries(unittest.TestCase):
224+
"""Unit tests for build_time_series."""
225+
226+
@patch('six.moves.urllib.request.urlopen', side_effect=request_mock)
227+
def test_basic(self, urlopen):
228+
"""Calling build_time_series with basic args."""
229+
series = dcpd.build_time_series('geoId/06', 'Count_Person')
230+
exp = pd.Series({"2000": 1, "2001": 2})
231+
232+
self.assertCountEqual(series, exp)
233+
234+
@patch('six.moves.urllib.request.urlopen', side_effect=request_mock)
235+
def test_multi_option(self, urlopen):
236+
"""Calling build_time_series with basic args."""
237+
series = dcpd.build_time_series('geoId/06', 'Count_Person', 'CensusPEPSurvey', 'P1Y', 'RealPeople', '100')
238+
exp = pd.Series({"2000": 3, "2001": 42})
239+
240+
self.assertCountEqual(series, exp)
241+
242+
@patch('six.moves.urllib.request.urlopen', side_effect=request_mock)
243+
def test_no_data(self, urlopen):
244+
"""Error if there is no data."""
245+
series = dcpd.build_time_series('geoId/06', 'Count_Person', 'DNE')
246+
exp = pd.Series({}, dtype=object)
247+
248+
self.assertCountEqual(series, exp)
249+
250+
222251
class TestPdTimeSeries(unittest.TestCase):
223252
"""Unit tests for _time_series_pd_input."""
224253

0 commit comments

Comments
 (0)