示例#1
0
    def helper_test_drop(test_case, test_series: TimeSeries):
        seriesA = test_series.drop_after(pd.Timestamp('20130105'))
        test_case.assertEqual(seriesA.end_time(),
                              pd.Timestamp('20130105') - test_series.freq())
        test_case.assertTrue(
            np.all(seriesA.time_index() < pd.Timestamp('20130105')))

        seriesB = test_series.drop_before(pd.Timestamp('20130105'))
        test_case.assertEqual(seriesB.start_time(),
                              pd.Timestamp('20130105') + test_series.freq())
        test_case.assertTrue(
            np.all(seriesB.time_index() > pd.Timestamp('20130105')))

        test_case.assertEqual(test_series.freq_str(), seriesA.freq_str())
        test_case.assertEqual(test_series.freq_str(), seriesB.freq_str())
示例#2
0
    def helper_test_append(test_case, test_series: TimeSeries):
        # reconstruct series
        seriesA, seriesB = test_series.split_after(pd.Timestamp('20130106'))
        test_case.assertEqual(seriesA.append(seriesB), test_series)
        test_case.assertEqual(
            seriesA.append(seriesB).freq(), test_series.freq())

        # Creating a gap is not allowed
        seriesC = test_series.drop_before(pd.Timestamp('20130107'))
        with test_case.assertRaises(ValueError):
            seriesA.append(seriesC)

        # Changing frequence is not allowed
        seriesM = TimeSeries.from_times_and_values(
            pd.date_range('20130107', '20130507', freq='30D'), range(5))
        with test_case.assertRaises(ValueError):
            seriesA.append(seriesM)
示例#3
0
    def helper_test_append_values(test_case, test_series: TimeSeries):
        # reconstruct series
        seriesA, seriesB = test_series.split_after(pd.Timestamp('20130106'))
        test_case.assertEqual(
            seriesA.append_values(seriesB.values(), seriesB.time_index()),
            test_series)
        test_case.assertEqual(seriesA.append_values(seriesB.values()),
                              test_series)

        # test for equality
        test_case.assertEqual(
            test_series.drop_after(pd.Timestamp('20130105')).append_values(
                test_series.drop_before(pd.Timestamp('20130104')).values()),
            test_series)
        test_case.assertEqual(seriesA.append_values([]), seriesA)

        # randomize order
        rd_order = np.random.permutation(range(len(seriesB.values())))
        test_case.assertEqual(
            seriesA.append_values(seriesB.values()[rd_order],
                                  seriesB.time_index()[rd_order]), test_series)

        # add non consecutive index
        with test_case.assertRaises(ValueError):
            test_case.assertEqual(
                seriesA.append_values(seriesB.values(),
                                      seriesB.time_index() + seriesB.freq()),
                test_series)

        # add existing indices
        with test_case.assertRaises(ValueError):
            test_case.assertEqual(
                seriesA.append_values(
                    seriesB.values(),
                    seriesB.time_index() - 3 * seriesB.freq()), test_series)

        # other frequency
        with test_case.assertRaises(ValueError):
            test_case.assertEqual(
                seriesA.append_values(
                    seriesB.values(),
                    pd.date_range('20130107', '20130113', freq='2d')),
                test_series)
示例#4
0
def _crop_to_match_seasons(series: TimeSeries,
                           required_matches: Optional[set]) -> TimeSeries:
    """Crops TimeSeries instance to contain full periods.

    Crops a given TimeSeries `series` that will be used as a training set in such
    a way that its first entry has a timestamp that matches the first timestamp
    right after the end of `series` in all attributes given in `required_matches`.
    If no such timestamp can be found, the original TimeSeries instance is returned.
    If the value of `required_matches` is `None`, the original TimeSeries instance is returned.

    Parameters
    ----------
    series
        TimeSeries instance to be cropped.
    required_matches
        A set of pd.Timestamp attributes which will be used to choose the cropping point.

    Returns
    -------
    TimeSeries
        New TimeSeries instance that is cropped as described above.
    """
    if required_matches is None or len(required_matches) == 0:
        return series

    first_ts = series.time_index[0]
    freq = series.freq
    pred_ts = series.time_index[-1] + freq

    # start at first timestamp of given series and move forward until a matching timestamp is found
    curr_ts = first_ts
    while curr_ts < pred_ts - 4 * freq:
        curr_ts += freq
        if _compare_timestamps_on_attributes(pred_ts, curr_ts,
                                             required_matches):
            new_series = series.drop_before(curr_ts)
            return new_series

    logger.warning(
        "No matching timestamp could be found, returning original TimeSeries.")
    return series