def test_generate_date_range_month(self, config):
        config["time_unit"] = "months"
        params = get_resampling_params(config)
        frequency = params.resampling_step
        time_unit = params.time_unit
        time_step = params.time_step

        end_time = pd.Timestamp('2021-06-20 00:00:00')

        start_time = pd.Timestamp('2021-01-31 00:00:00')
        date_range = generate_date_range(start_time, end_time, 0, 0, 0, frequency, time_step, time_unit)
        np.testing.assert_array_equal(date_range, pd.DatetimeIndex(['2021-01-31', '2021-03-31', '2021-05-31', '2021-07-31']))

        start_time = pd.Timestamp('2021-01-23 00:00:00')
        date_range = generate_date_range(start_time, end_time, 0, 0, 0, frequency, time_step, time_unit)
        np.testing.assert_array_equal(date_range, pd.DatetimeIndex(['2021-01-31', '2021-03-31', '2021-05-31', '2021-07-31']))

        start_time = pd.Timestamp('2021-01-31 10:00:00')
        date_range = generate_date_range(start_time, end_time, 0, 0, 0, frequency, time_step, time_unit)
        np.testing.assert_array_equal(date_range, pd.DatetimeIndex(['2021-01-31', '2021-03-31', '2021-05-31', '2021-07-31']))

        start_time = pd.Timestamp('2021-01-31 10:00:00').tz_localize("CET")
        end_time = pd.Timestamp('2021-06-20 00:00:00').tz_localize("CET")
        date_range = generate_date_range(start_time, end_time, 0, 0, 0, frequency, time_step, time_unit)
        np.testing.assert_array_equal(date_range, pd.DatetimeIndex(
            ['2021-01-31 00:00:00+01:00', '2021-03-31 00:00:00+02:00', '2021-05-31 00:00:00+02:00', '2021-07-31 00:00:00+02:00']))

        start_time = pd.Timestamp('2021-01-31 10:00:00')
        end_time = pd.Timestamp('2021-06-20 00:00:00')
        date_range = generate_date_range(start_time, end_time, 1, 0, 1, frequency, time_step, time_unit)
        np.testing.assert_array_equal(date_range, pd.DatetimeIndex(['2021-03-31', '2021-05-31', '2021-07-31']))
    def test_generate_date_range_week(self, config):
        config["time_unit"] = "weeks"
        params = get_resampling_params(config)
        frequency = params.resampling_step
        time_unit = params.time_unit
        time_step = params.time_step

        start_time = pd.Timestamp('2020-12-23 00:00:00')
        end_time = pd.Timestamp('2021-01-18 00:00:00')

        date_range = generate_date_range(start_time, end_time, 0, 0, 0, frequency, time_step, time_unit)
        np.testing.assert_array_equal(date_range, pd.DatetimeIndex(['2020-12-27', '2021-01-10', '2021-01-24']))

        end_time = pd.Timestamp('2021-01-24 00:00:00')
        date_range = generate_date_range(start_time, end_time, 0, 0, 0, frequency, time_step, time_unit)
        np.testing.assert_array_equal(date_range, pd.DatetimeIndex(['2020-12-27', '2021-01-10', '2021-01-24', '2021-02-07']))

        date_range = generate_date_range(start_time, end_time, 1, 0, 1, frequency, time_step, time_unit)
        np.testing.assert_array_equal(date_range, pd.DatetimeIndex(['2021-01-10', '2021-01-24', '2021-02-07']))

        config["time_unit"] = "weeks"
        config["time_unit_end_of_week"] = "WED"
        params = get_resampling_params(config)
        frequency = params.resampling_step
        time_unit = params.time_unit
        time_step = params.time_step
        date_range = generate_date_range(start_time, end_time, 0, 0, 0, frequency, time_step, time_unit)
        np.testing.assert_array_equal(date_range, pd.DatetimeIndex(['2020-12-23', '2021-01-6', '2021-01-20', '2021-02-03']))
    def test_generate_date_range_b_days(self, config):
        config["time_unit"] = "business_days"
        config["time_step"] = 1
        start_time = pd.Timestamp('2021-01-02 00:00:00')
        end_time = pd.Timestamp('2021-01-10 00:00:00')

        params = get_resampling_params(config)
        frequency = params.resampling_step
        time_unit = params.time_unit
        time_step = params.time_step

        date_range = generate_date_range(start_time, end_time, 0, 0, 0, frequency, time_step, time_unit)
        np.testing.assert_array_equal(date_range, pd.DatetimeIndex(['2021-01-04', '2021-01-05', '2021-01-06', '2021-01-07', '2021-01-08', '2021-01-11']))

        clip_start = 1
        clip_end = 1
        shift = 0
        date_range = generate_date_range(start_time, end_time, clip_start, clip_end, shift, frequency, time_step, time_unit)
        np.testing.assert_array_equal(date_range, pd.DatetimeIndex(['2021-01-04', '2021-01-05', '2021-01-06', '2021-01-07', '2021-01-08', '2021-01-11']))

        clip_start = 2
        clip_end = 2
        shift = 0
        date_range = generate_date_range(start_time, end_time, clip_start, clip_end, shift, frequency, time_step, time_unit)
        np.testing.assert_array_equal(date_range, pd.DatetimeIndex(['2021-01-05', '2021-01-06', '2021-01-07', '2021-01-08']))
    def test_generate_date_range_microseconds(self, config):
        config["time_unit"] = "microseconds"
        config["time_step"] = 1
        start_time = pd.Timestamp('20190131 01:59:00').tz_localize('CET')
        end_time = pd.Timestamp('2019-01-31 01:59:00.000016').tz_localize('CET')

        params = get_resampling_params(config)
        frequency = params.resampling_step
        time_unit = params.time_unit
        time_step = params.time_step

        clip_start = 5
        shift = 2
        clip_end = 3

        date_range = generate_date_range(start_time, end_time, clip_start, clip_end, shift, frequency, time_step, time_unit)
        expected_range = pd.DatetimeIndex(['2019-01-31 01:59:00.000007+01:00',
                                           '2019-01-31 01:59:00.000008+01:00',
                                           '2019-01-31 01:59:00.000009+01:00',
                                           '2019-01-31 01:59:00.000010+01:00',
                                           '2019-01-31 01:59:00.000011+01:00',
                                           '2019-01-31 01:59:00.000012+01:00',
                                           '2019-01-31 01:59:00.000013+01:00',
                                           '2019-01-31 01:59:00.000014+01:00',
                                           '2019-01-31 01:59:00.000015+01:00'])
        np.testing.assert_array_equal(date_range, expected_range)
    def test_generate_date_range_half_year(self, config):
        config["time_step"] = 1
        config["time_unit"] = "semi_annual"
        start_time = pd.Timestamp('2020-01-01 00:00:00')
        end_time = pd.Timestamp('2021-06-18 00:00:00')

        params = get_resampling_params(config)
        frequency = params.resampling_step
        time_unit = params.time_unit
        time_step = params.time_step

        date_range = generate_date_range(start_time, end_time, 0, 0, 0, frequency, time_step, time_unit)
        np.testing.assert_array_equal(date_range, pd.DatetimeIndex(['2020-01-31', '2020-07-31', '2021-01-31', '2021-07-31']))
Example #6
0
 def _compute_full_time_index(self, df, datetime_column):
     """
     From the resampling config, create the full index of the output dataframe.
     """
     start_time = df[datetime_column].min()
     end_time = df[datetime_column].max()
     clip_start = self.params.clip_start
     clip_end = self.params.clip_end
     shift = self.params.shift
     frequency = self.params.resampling_step
     time_step = self.params.time_step
     time_unit = self.params.time_unit
     return generate_date_range(start_time, end_time, clip_start, clip_end, shift, frequency, time_step, time_unit)
    def test_generate_date_range_nanoseconds(self, config):
        config["time_unit"] = "nanoseconds"
        config["time_step"] = 1
        start_time = pd.Timestamp('2019-01-31T00:59:00.000000000')
        end_time = pd.Timestamp('2019-01-31T00:59:00.000000009')

        params = get_resampling_params(config)
        frequency = params.resampling_step
        time_unit = params.time_unit
        time_step = params.time_step

        clip_start = 5
        shift = 2
        clip_end = 3

        date_range = generate_date_range(start_time, end_time, clip_start, clip_end, shift, frequency, time_step, time_unit)
        np.testing.assert_array_equal(date_range, pd.DatetimeIndex(['2019-01-31 00:59:00.000000007',
                                                                    '2019-01-31 00:59:00.000000008']))
    def test_generate_date_range_days(self, config):
        config["time_unit"] = "days"
        config["time_step"] = 1
        start_time = pd.Timestamp('20190131 01:59:00').tz_localize('CET')
        end_time = pd.Timestamp('20190214 01:59:00').tz_localize('CET')

        params = get_resampling_params(config)
        frequency = params.resampling_step
        time_unit = params.time_unit
        time_step = params.time_step

        clip_start = 5
        shift = 2
        clip_end = 3

        date_range = generate_date_range(start_time, end_time, clip_start, clip_end, shift, frequency, time_step, time_unit)
        expected_range = pd.DatetimeIndex(['2019-02-07 00:00:00+01:00', '2019-02-08 00:00:00+01:00',
                                           '2019-02-09 00:00:00+01:00', '2019-02-10 00:00:00+01:00',
                                           '2019-02-11 00:00:00+01:00', '2019-02-12 00:00:00+01:00',
                                           '2019-02-13 00:00:00+01:00'])
        np.testing.assert_array_equal(date_range, expected_range)
    def test_generate_date_range_minutes(self, config):
        config["time_unit"] = "minutes"
        config["time_step"] = 1
        start_time = pd.Timestamp('20190131 01:59:00').tz_localize('CET')
        end_time = pd.Timestamp('20190131 02:15:00').tz_localize('CET')

        params = get_resampling_params(config)
        frequency = params.resampling_step
        time_unit = params.time_unit
        time_step = params.time_step

        clip_start = 5
        shift = 2
        clip_end = 3

        date_range = generate_date_range(start_time, end_time, clip_start, clip_end, shift, frequency, time_step, time_unit)
        expected_range = pd.DatetimeIndex(['2019-01-31 02:06:00+01:00', '2019-01-31 02:07:00+01:00',
                                           '2019-01-31 02:08:00+01:00', '2019-01-31 02:09:00+01:00',
                                           '2019-01-31 02:10:00+01:00', '2019-01-31 02:11:00+01:00',
                                           '2019-01-31 02:12:00+01:00', '2019-01-31 02:13:00+01:00',
                                           '2019-01-31 02:14:00+01:00'])
        np.testing.assert_array_equal(date_range, expected_range)