def testCheckValidPeriodsWeekly(self): dates = list(pd.date_range(start='2020-01-01', periods=4 * 2, freq='W')) geos = [1, 2] df = pd.DataFrame({ 'date': dates * len(geos), 'geo': sorted(geos * len(dates)) }) out = util.check_time_periods( geox_data=df, start_date_eval=pd.Timestamp('2020-01-01'), start_date_aa_test=pd.Timestamp('2020-01-29'), experiment_duration_weeks=4, frequency='W') self.assertTrue(out) out_infer = util.check_time_periods( geox_data=df, start_date_eval=pd.Timestamp('2020-01-01'), start_date_aa_test=pd.Timestamp('2020-01-29'), experiment_duration_weeks=4, frequency='infer') self.assertTrue(out_infer)
def testInvalidFrequency(self): dates = list(pd.date_range(start='2020-01-01', periods=8, freq='7D')) geos = [1, 2] df = pd.DataFrame({ 'date': dates * len(geos), 'geo': sorted(geos * len(dates)) }) with self.assertRaises(ValueError) as cm: _ = util.check_time_periods( geox_data=df, start_date_eval=pd.Timestamp('2020-01-01'), start_date_aa_test=pd.Timestamp('2020-01-29'), experiment_duration_weeks=4, frequency='invalid') self.assertEqual( str(cm.exception), 'frequency should be one of ["infer", "D", "W"], got invalid')
def testInvalidPeriodsWeekly(self): dates = list(pd.date_range(start='2020-01-01', periods=7, freq='7D')) geos = [1, 2] df = pd.DataFrame({ 'date': dates * len(geos), 'geo': sorted(geos * len(dates)) }) missing = ['2020-02-19'] freq_str = 'weeks' with self.assertRaises(ValueError) as cm: _ = util.check_time_periods( geox_data=df, start_date_eval=pd.Timestamp('2020-01-01'), start_date_aa_test=pd.Timestamp('2020-01-29'), experiment_duration_weeks=4, frequency='W') self.assertEqual( str(cm.exception), (f'The AA test period contains the following {freq_str} ' + f'{missing} for which we do not have data.'))