def test_tssdict_combine(self): """ A batch of tests combining columns to one timeseries. Tests check to see whether the parameters are passed down properly to each timeseries. """ # combine(self, discard=True, pad=None) ts_new, _ = self.tssdict.combine(discard=True, pad=None) # shape corresponds to the shortest length self.assertEqual( ts_new.tseries.shape[0], self.ts_short.tseries.shape[0] ) self.assertEqual(ts_new.tseries.shape[1], 3) # combine(self, discard=False, pad=0) ts_new, _ = self.tssdict.combine(discard=False, pad=0) # shape corresponds to the longest length self.assertEqual( ts_new.tseries.shape[0], self.ts_long.tseries.shape[0] ) self.assertEqual(ts_new.tseries.shape[1], 3) # test with TssList tmp_ts0 = Timeseries() tmp_ts0.key = "First" tmp_ts0.dseries = datetime(2014, 12, 31).toordinal() - np.arange(10) tmp_ts0.tseries = np.arange(10) tmp_ts0.make_arrays() tmp_ts1 = Timeseries() tmp_ts1.key = "Second" tmp_ts1.dseries = datetime(2014, 12, 31).toordinal() - np.arange(10) tmp_ts1.tseries = np.arange(10) tmp_ts1.make_arrays() tssdict = TssDict(TssList([tmp_ts0, tmp_ts1])) ts, _ = tssdict.combine() self.assertTupleEqual(ts.tseries.shape, (10, 2)) # test with TssDict tssdict = TssDict(TssDict([tmp_ts0, tmp_ts1])) ts, _ = tssdict.combine() self.assertTupleEqual(ts.tseries.shape, (10, 2))
def test_tssdict_min_date(self): """Tests min date """ # First add a timeseries that is earlier than the others tmp_ts0 = Timeseries() tmp_ts0.key = "First" tmp_ts0.dseries = datetime(2014, 12, 31).toordinal() - np.arange(10) tmp_ts0.tseries = np.arange(10) tmp_ts0.make_arrays() self.tssdict[tmp_ts0.key] = tmp_ts0 self.assertTupleEqual( self.tssdict.min_date(), (date(2014, 12, 22), "First") ) tmp_nodata = Timeseries() tmp_nodata.key = "nothing" tssdict = TssDict() tssdict[tmp_nodata.key] = tmp_nodata self.assertTupleEqual(tssdict.min_date(), (None, "nothing")) tssdict = TssDict() # none timeseries list tssdict["test"] = [ date(2014, 12, 31) + timedelta(days=i) for i in range(10) ] tssdict["test1"] = [ date(2013, 12, 31) + timedelta(days=i) for i in range(10) ] self.assertRaises(ValueError, tssdict.min_date)
def test_timeseries_extend(self): """Tests adding rows to a timeseries.""" # create overlapping timeseries ts = Timeseries() start_date = datetime(2016, 1, 5).toordinal() ts.dseries = start_date + np.arange(10) ts.tseries = np.arange(10, 20) ts.make_arrays() ts_copy = self.ts.clone() self.assertRaises(ValueError, self.ts.extend, ts, overlay=False) # [ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9.] # # [ 10. 11. 12. 13. 14. 15. 16. 17. 18. 19.] ts_copy.extend(ts, overlay=True) self.assertEqual(ts_copy.tseries[4], 4) self.assertEqual(ts_copy.tseries[5], 10) self.assertEqual(ts_copy.tseries[6], 11) self.assertEqual(ts_copy.tseries[7], 12) self.assertEqual(ts_copy.tseries[8], 13) self.assertEqual(ts_copy.tseries[9], 14) self.assertEqual(ts_copy.tseries[10], 15) self.assertEqual(ts_copy.tseries[11], 16)
def test_timeseries_start_date(self): """Tests start date regardless of date sorts and types.""" # get as ordinal self.assertEqual(self.ts.dseries[0], self.ts.start_date()) # reverse - now new to old self.ts.reverse() self.assertEqual(self.ts.dseries[-1], self.ts.start_date()) # get as datetime from ordinal self.assertEqual(date(2015, 12, 31), self.ts.start_date("datetime")) # get as timestamp ts = Timeseries(frequency="sec") self.assertEqual("timestamp", ts.get_date_series_type()) ts.dseries = datetime(2015, 12, 31).timestamp() + np.arange(10) ts.tseries = np.arange(10) self.assertEqual(ts.dseries[0], ts.start_date()) # reverse - now new to old ts.reverse() self.assertEqual(ts.dseries[-1], ts.start_date()) # get as datetime from timestamp self.assertEqual(date(2015, 12, 31), self.ts.start_date("datetime")) # string date self.assertEqual(self.ts.start_date("str"), "2015-12-31") # bad format self.assertRaises(ValueError, self.ts.start_date, "bad")
def test_date_string_series(self): """Tests returning a list of dates in string format.""" # ordinal default fmt str_series = self.ts.date_string_series() self.assertEqual(str_series[0], "2015-12-31") self.assertEqual(str_series[1], "2016-01-01") self.assertEqual(str_series[2], "2016-01-02") self.assertEqual(str_series[3], "2016-01-03") self.assertEqual(str_series[4], "2016-01-04") # timestamp default fmt ts = Timeseries(frequency="sec") ts.dseries = datetime(2016, 1, 1, 0, 0, 0).timestamp() + np.arange(10) ts.tseries = np.arange(10) ts.make_arrays() str_series = ts.date_string_series() self.assertEqual(str_series[0], "2016-01-01 00:00:00") self.assertEqual(str_series[1], "2016-01-01 00:00:01") self.assertEqual(str_series[2], "2016-01-01 00:00:02") self.assertEqual(str_series[3], "2016-01-01 00:00:03") self.assertEqual(str_series[4], "2016-01-01 00:00:04") # ordinal custom fmt str_series = self.ts.date_string_series("%Y-%b-%d") self.assertEqual(str_series[0], "2015-Dec-31") self.assertEqual(str_series[1], "2016-Jan-01") self.assertEqual(str_series[2], "2016-Jan-02") self.assertEqual(str_series[3], "2016-Jan-03") self.assertEqual(str_series[4], "2016-Jan-04")
def test_timeseries_daterange(self): """Tests returning the starting and ending dates in various formats.""" # ordinal daterange as ordinals, self.assertTupleEqual( self.ts.daterange(), ( datetime(2015, 12, 31).toordinal(), datetime(2016, 1, 9).toordinal(), ), ) # ordinal daterange as string self.assertTupleEqual(self.ts.daterange("str"), ("2015-12-31", "2016-01-09")) # ordinal daterange as datetimes self.assertTupleEqual( self.ts.daterange("datetime"), (date(2015, 12, 31), date(2016, 1, 9)), ) # timestamp tstamp_list = datetime(2016, 1, 1).timestamp() + np.arange(20) ts = Timeseries() ts.frequency = FREQ_SEC ts.dseries = tstamp_list ts.tseries = np.arange(20) # timestamp daterange as timestamps self.assertTupleEqual( ts.daterange(), ( datetime(2016, 1, 1).timestamp(), datetime(2016, 1, 1, 0, 0, 19).timestamp(), ), ) # timestamp daterange as string self.assertTupleEqual( ts.daterange("str"), ("2016-01-01 00:00:00", "2016-01-01 00:00:19"), ) # timestamp daterange as datetimes self.assertTupleEqual( ts.daterange("datetime"), ( datetime(2016, 1, 1, 0, 0, 0), datetime(2016, 1, 1, 0, 0, 19), ), ) # test blank Timeseries ts = Timeseries() self.assertTupleEqual(ts.daterange(), (None, None)) # test invalid format flag self.assertRaises(ValueError, self.ts.daterange, fmt="wrong")
def test_timeseries_get_duped_dates(self): """Test the dupes works properly.""" ts = self.ts.clone() ts.dseries[3] = ts.dseries[4] ts = Timeseries(frequency="sec") ts.dseries = datetime(2015, 12, 31).timestamp() + np.arange(10) ts.tseries = np.arange(10) ts.make_arrays() ts.dseries[3] = ts.dseries[4]
def test_timeseries_get_datetime(self): """Tests conversion to datetime from ordinal/timestamps""" tmp_date = self.ts.start_date() self.assertEqual(date(2015, 12, 31), self.ts.get_datetime(tmp_date)) ts = Timeseries(frequency="sec") ts.dseries = datetime(2015, 12, 31).timestamp() + np.arange(10) ts.tseries = np.arange(10) tmp_date = self.ts.start_date() self.assertEqual(date(2015, 12, 31), self.ts.get_datetime(tmp_date))
def test_timeseries_datetime_series(self): """ Tests returning a date series converted to date/datetime objects. """ ord_list = datetime(2016, 1, 1).toordinal() + np.arange(20) tstamp_list = datetime(2016, 1, 1).timestamp() + np.arange(20) ts = Timeseries() ts.dseries = ord_list ts.tseries = np.arange(20) # gilding the lily dt_list = [date(2016, 1, 1) + timedelta(days=i) for i in range(20)] self.assertListEqual(ts.datetime_series(), dt_list) ts.frequency = FREQ_SEC ts.dseries = tstamp_list ts.tseries = np.arange(20) dt_list = [ datetime(2016, 1, 1) + timedelta(seconds=i) for i in range(20) ] self.assertListEqual(ts.datetime_series(), dt_list)
def test_timeseries_closest_date(self): """Tests returning the closest date in the series to the input date.""" ts = Timeseries() ts.dseries = [] ts.tseries = [] start_date = datetime(2015, 12, 31) for i in range(40): date = start_date + timedelta(days=i) if date.weekday() not in [5, 6]: ts.dseries.append(date.toordinal()) ts.tseries.append(i) ts.make_arrays() date1 = datetime(2016, 1, 7) # existing date within date series date2 = datetime(2016, 1, 16) # date falling on a weekend date3 = datetime(2015, 6, 16) # date prior to start of date series date4 = datetime(2016, 3, 8) # date after to end of date series # as datetime and in the series test_date = ts.closest_date(rowdate=date1, closest=1) self.assertEqual(test_date, date1.toordinal()) # as ordinal and in the series test_date = ts.closest_date(rowdate=date1, closest=1) self.assertEqual(test_date, date1.toordinal()) # as datetime but date not in series test_date = ts.closest_date(rowdate=date2, closest=1) self.assertEqual(test_date, datetime(2016, 1, 18).toordinal()) test_date = ts.closest_date(rowdate=date2, closest=-1) self.assertEqual(test_date, datetime(2016, 1, 15).toordinal()) # as datetime but date not in series, look for earlier date self.assertRaises(ValueError, ts.closest_date, rowdate=date3, closest=-1) # as datetime but date not in series, look for later date self.assertRaises(ValueError, ts.closest_date, rowdate=date4, closest=1)
def test_timeseries_years(self): """Tests returning the ending values by years in a dict.""" ts = Timeseries() ts.dseries = datetime(2015, 12, 31).toordinal() + np.arange(1000) ts.tseries = np.arange(1000) self.assertDictEqual( ts.years(), { 2015: 0, 2016: 366, 2017: 731, 2018: 999, }, )
def test_convert(self): """ This function is a pass-through to the convert function. This version basically checks to see if the plumbing works. However, until the design decision is made on how to transition from intraday to monthly, etc., this cannot be considered complete. """ ts = Timeseries() ts.dseries = datetime(2015, 12, 31).toordinal() + np.arange(1000) ts.tseries = np.arange(1000) ts_monthly = ts.convert(new_freq=FREQ_M, include_partial=True) self.assertEqual(ts_monthly.dseries[0], 735963) self.assertEqual(ts_monthly.dseries[1], 735994) self.assertEqual(ts_monthly.dseries[2], 736023) self.assertEqual(ts_monthly.dseries[3], 736054) self.assertEqual(ts_monthly.dseries[4], 736084) self.assertEqual(ts_monthly.dseries[5], 736115) self.assertEqual(ts_monthly.dseries[6], 736145) self.assertEqual(ts_monthly.dseries[7], 736176) self.assertEqual(ts_monthly.dseries[8], 736207) self.assertEqual(ts_monthly.dseries[9], 736237) self.assertEqual(ts_monthly.tseries[0], 0) self.assertEqual(ts_monthly.tseries[1], 31) self.assertEqual(ts_monthly.tseries[2], 60) self.assertEqual(ts_monthly.tseries[3], 91) self.assertEqual(ts_monthly.tseries[4], 121) self.assertEqual(ts_monthly.tseries[5], 152) self.assertEqual(ts_monthly.tseries[6], 182) self.assertEqual(ts_monthly.tseries[7], 213) self.assertEqual(ts_monthly.tseries[8], 244) self.assertEqual(ts_monthly.tseries[9], 274) self.assertEqual(ts_monthly.dseries[-1], 736962) self.assertEqual(ts_monthly.tseries[-1], 999) # bad frequency self.assertRaises(ValueError, ts.convert, new_freq="bad", include_partial=True)
def test_timeseries_months(self): """Tests returning the ending values by months in a dict.""" ts = Timeseries() ts.dseries = datetime(2015, 12, 31).toordinal() + np.arange(1000) ts.tseries = np.arange(1000) self.assertDictEqual( ts.months(), { "2015-12": 0, "2016-01": 31, "2016-02": 60, "2016-03": 91, "2016-04": 121, "2016-05": 152, "2016-06": 182, "2016-07": 213, "2016-08": 244, "2016-09": 274, "2016-10": 305, "2016-11": 335, "2016-12": 366, "2017-01": 397, "2017-02": 425, "2017-03": 456, "2017-04": 486, "2017-05": 517, "2017-06": 547, "2017-07": 578, "2017-08": 609, "2017-09": 639, "2017-10": 670, "2017-11": 700, "2017-12": 731, "2018-01": 762, "2018-02": 790, "2018-03": 821, "2018-04": 851, "2018-05": 882, "2018-06": 912, "2018-07": 943, "2018-08": 974, "2018-09": 999, }, )
def test_timeseries_row_no(self): """ Tests the ability to locate the correct row. """ ts = Timeseries() ts.dseries = [] ts.tseries = [] start_date = datetime(2015, 12, 31) for i in range(40): date = start_date + timedelta(days=i) if date.weekday() not in [5, 6]: ts.dseries.append(date.toordinal()) ts.tseries.append(i) ts.make_arrays() date1 = datetime(2016, 1, 7) # existing date within date series date2 = datetime(2016, 1, 16) # date falling on a weekend date3 = datetime(2015, 6, 16) # date prior to start of date series date4 = datetime(2016, 3, 8) # date after to end of date series # as datetime row_no = ts.row_no(rowdate=date1, closest=0, no_error=False) self.assertEqual(row_no, 5) # as ordinal row_no = ts.row_no(rowdate=date1.toordinal(), closest=0, no_error=False) self.assertEqual(row_no, 5) # as datetime but date not in series self.assertRaises( ValueError, ts.row_no, rowdate=date2, closest=0, no_error=False, ) row_no = ts.row_no(rowdate=date2, closest=0, no_error=True) self.assertEqual(row_no, -1) # as datetime but date not in series, look for earlier date row_no = ts.row_no(rowdate=date2, closest=-1, no_error=False) self.assertEqual(row_no, 11) # as datetime but date not in series, look for later date row_no = ts.row_no(rowdate=date2, closest=1, no_error=False) self.assertEqual(row_no, 12) # as datetime but date not in series, look for earlier date self.assertRaises( ValueError, ts.row_no, rowdate=date3, closest=-1, no_error=False, ) # as datetime but date not in series, look for later date self.assertRaises( ValueError, ts.row_no, rowdate=date4, closest=1, no_error=False, ) # now change series direction ts.reverse() # as datetime row_no = ts.row_no(rowdate=date1, closest=0, no_error=False) self.assertEqual(row_no, 22) # as ordinal row_no = ts.row_no(rowdate=date1.toordinal(), closest=0, no_error=False) self.assertEqual(row_no, 22) # as datetime but date not in series self.assertRaises( ValueError, ts.row_no, rowdate=date2, closest=0, no_error=False, ) row_no = ts.row_no(rowdate=date2, closest=0, no_error=True) self.assertEqual(row_no, -1) # as datetime but date not in series, look for earlier date row_no = ts.row_no(rowdate=date2, closest=-1, no_error=False) self.assertEqual(row_no, 16) # as datetime but date not in series, look for later date row_no = ts.row_no(rowdate=date2, closest=1, no_error=False) self.assertEqual(row_no, 15) # as datetime but date not in series, look for earlier date self.assertRaises( ValueError, ts.row_no, rowdate=date3, closest=-1, no_error=False, ) # as datetime but date not in series, look for later date self.assertRaises( ValueError, ts.row_no, rowdate=date4, closest=1, no_error=False, )
def test_timeseries_truncdate(self): """ Tests returning a timeseries that is a subset, but selected by date. """ # set up separate timeseries with weekends skipped ts = Timeseries() ts.dseries = [] ts.tseries = [] start_date = datetime(2015, 12, 31) for i in range(40): date = start_date + timedelta(days=i) if date.weekday() not in [5, 6]: ts.dseries.append(date.toordinal()) ts.tseries.append(i) ts.make_arrays() date1 = datetime(2016, 1, 7) # existing date within date series date2 = datetime(2016, 1, 25) # existing date within date series date3 = datetime(2016, 1, 16) # date falling on a weekend # date as datetime ts1 = ts.clone() ts1.truncdate(start=date1, finish=None, new=False) self.assertTrue(np.array_equal(ts1.tseries, ts.tseries[5:])) self.assertTrue(np.array_equal(ts1.dseries, ts.dseries[5:])) # date as ordinal ts1 = ts.clone() ts1.truncdate(start=date1.toordinal(), finish=None, new=False) self.assertTrue(np.array_equal(ts1.tseries, ts.tseries[5:])) self.assertTrue(np.array_equal(ts1.dseries, ts.dseries[5:])) # finish date ts1 = ts.clone() ts1.truncdate(start=None, finish=date2, new=False) self.assertTrue(np.array_equal(ts1.tseries, ts.tseries[:18])) self.assertTrue(np.array_equal(ts1.dseries, ts.dseries[:18])) # finish date that is not in dseries ts1 = ts.clone() ts1.truncdate(start=None, finish=date3, new=False) self.assertTrue(np.array_equal(ts1.tseries, ts.tseries[:12])) self.assertTrue(np.array_equal(ts1.dseries, ts.dseries[:12])) # start and finish date ts1 = ts.clone() ts1.truncdate(start=date1, finish=date2, new=False) self.assertTrue(np.array_equal(ts1.tseries, ts.tseries[5:18])) self.assertTrue(np.array_equal(ts1.dseries, ts.dseries[5:18])) # start and finish date that is not in dseries ts1 = ts.clone() ts1.truncdate(start=date1, finish=date3, new=False) self.assertTrue(np.array_equal(ts1.tseries, ts.tseries[5:12])) self.assertTrue(np.array_equal(ts1.dseries, ts.dseries[5:12])) # start and finish date as a list ts1 = ts.clone() ts1.truncdate([date1, date3], new=False) self.assertTrue(np.array_equal(ts1.tseries, ts.tseries[5:12])) self.assertTrue(np.array_equal(ts1.dseries, ts.dseries[5:12])) # start and finish date as a tuple ts1 = ts.clone() ts1.truncdate((date1, date3), new=False) self.assertTrue(np.array_equal(ts1.tseries, ts.tseries[5:12])) self.assertTrue(np.array_equal(ts1.dseries, ts.dseries[5:12])) # start and finish date as a tuple new=True ts1 = ts.clone() ts2 = ts1.truncdate((date1, date3), new=True) self.assertTrue(np.array_equal(ts1.tseries, ts.tseries)) self.assertTrue(np.array_equal(ts1.dseries, ts.dseries)) self.assertTrue(np.array_equal(ts2.tseries, ts.tseries[5:12])) self.assertTrue(np.array_equal(ts2.dseries, ts.dseries[5:12]))