Пример #1
0
def test_resample_data_frame_rules():
    resample_rules = ['1T', '2T', '5T', '10T', '60T', '1H']
    expected_lengths = [782, 392, 158, 80, 14, 14]
    for resample_rule, expected_length in zip(resample_rules,
                                              expected_lengths):
        resampled_data_frame = resample_data_frame(sample_data_frame,
                                                   resample_rule)
        assert len(resampled_data_frame) == expected_length
Пример #2
0
    def resample(feature, data):
        """

        :param feature: the feature object
        :type feature: Feature

        :param data: the data to be resampled
        :type data: pd.DataFrame

        :return: the resampled data
        :rtype pd.DataFrame
        """

        if not feature.local and feature.resample_minutes > 0:
            resample_rule = str(feature.resample_minutes) + 'T'
            sampling_function = RESAMPLE_FUNCTION_MAP.get(
                feature.name, RESAMPLE_FUNCTION_LAST)
            data = resample_data_frame(data,
                                       resample_rule,
                                       sampling_function=sampling_function)

        return data
Пример #3
0
def test_resample_data_frame_functions():
    index = pd.date_range('1/1/2000', periods=9, freq='T')
    data_frame = pd.DataFrame(list(range(9)), index=index, columns=['col'])
    resample_rule = '3T'

    sampling_function = 'mean'
    assert_almost_equal(
        resample_data_frame(data_frame, resample_rule,
                            sampling_function)['col'], [0., 2., 5., 7.5])

    sampling_function = 'median'
    assert_almost_equal(
        resample_data_frame(data_frame, resample_rule,
                            sampling_function)['col'], [0., 2., 5., 7.5])

    sampling_function = 'sum'
    assert_almost_equal(
        resample_data_frame(data_frame, resample_rule,
                            sampling_function)['col'], [0, 6, 15, 15])

    sampling_function = 'first'
    assert_almost_equal(
        resample_data_frame(data_frame, resample_rule,
                            sampling_function)['col'], [0, 1, 4, 7])

    sampling_function = 'last'
    assert_almost_equal(
        resample_data_frame(data_frame, resample_rule,
                            sampling_function)['col'], [0, 3, 6, 8])

    sampling_function = 'min'
    assert_almost_equal(
        resample_data_frame(data_frame, resample_rule,
                            sampling_function)['col'], [0, 1, 4, 7])

    sampling_function = 'max'
    assert_almost_equal(
        resample_data_frame(data_frame, resample_rule,
                            sampling_function)['col'], [0, 3, 6, 8])
Пример #4
0
def test_resample_data_frame_wrong_sampling_function():
    with pytest.raises(AssertionError):
        resample_data_frame(sample_data_frame, '15T', 'wrong')
Пример #5
0
def test_resample_data_frame_wrong_sampling_function_type():
    with pytest.raises(AssertionError):
        resample_data_frame(sample_data_frame, '15T', 12)
        resample_data_frame(sample_data_frame, '15T', ['mean', 'median'])
Пример #6
0
def test_resample_data_frame_wrong_resample_rule_type():
    with pytest.raises(ValueError):
        resample_data_frame(sample_data_frame, 15)
        resample_data_frame(sample_data_frame, '15')