carbonara: remove numpy warning

We ask scipy/numpy to compute std on serie with one timestamp. And numpy
complains and return nan. Next we drop all nan.

This change creates the list of timestamps that are not unique and then
compute the std. numpy does not complain anymore and we don't have to
remove nan.

Closes-bug: #1663419
Change-Id: Ie1ddd244e42dcac4f91f741f37a980e452f91845
This commit is contained in:
Mehdi Abaakouk 2017-03-07 12:16:04 +01:00
parent 1975a9799c
commit 8040d06d3b
2 changed files with 25 additions and 7 deletions

View File

@ -154,15 +154,16 @@ class GroupedTimeSeries(object):
def _scipy_aggregate(self, method, remove_unique=False, *args, **kwargs):
if remove_unique:
locs = numpy.argwhere(self.counts > 1).T[0]
tstamps = self.tstamps[self.counts > 1]
else:
tstamps = self.tstamps
values = method(self._ts.values, self.indexes, self.tstamps,
if len(tstamps) == 0:
return pandas.Series()
values = method(self._ts.values, self.indexes, tstamps,
*args, **kwargs)
timestamps = numpy.array(self.tstamps, 'datetime64[ns]')
if remove_unique:
timestamps = timestamps[locs]
values = values[locs]
timestamps = numpy.array(tstamps, 'datetime64[ns]')
return pandas.Series(values, pandas.to_datetime(timestamps))

View File

@ -240,6 +240,23 @@ class TestAggregatedTimeSerie(base.BaseTestCase):
self._do_test_aggregation('std', 1.5275252316519465,
0.70710678118654757)
def test_aggregation_std_with_unique(self):
ts = carbonara.TimeSerie.from_tuples(
[(datetime.datetime(2014, 1, 1, 12, 0, 0), 3)])
ts = self._resample(ts, 60, 'std')
self.assertEqual(0, len(ts), ts.ts.values)
ts = carbonara.TimeSerie.from_tuples(
[(datetime.datetime(2014, 1, 1, 12, 0, 0), 3),
(datetime.datetime(2014, 1, 1, 12, 0, 4), 6),
(datetime.datetime(2014, 1, 1, 12, 0, 9), 5),
(datetime.datetime(2014, 1, 1, 12, 1, 6), 9)])
ts = self._resample(ts, 60, "std")
self.assertEqual(1, len(ts))
self.assertEqual(1.5275252316519465,
ts[datetime.datetime(2014, 1, 1, 12, 0, 0)])
def test_different_length_in_timestamps_and_data(self):
self.assertRaises(ValueError,
carbonara.AggregatedTimeSerie.from_data,