|
21 | 21 | from pandas.core.indexes.timedeltas import timedelta_range
|
22 | 22 | from pandas.core.resample import _asfreq_compat
|
23 | 23 |
|
| 24 | +# a fixture value can be overridden by the test parameter value. Note that the |
| 25 | +# value of the fixture can be overridden this way even if the test doesn't use |
| 26 | +# it directly (doesn't mention it in the function prototype). |
| 27 | +# see https://docs.pytest.org/en/latest/fixture.html#override-a-fixture-with-direct-test-parametrization # noqa: E501 |
| 28 | +# in this module we override the fixture values defined in conftest.py |
| 29 | +# tuples of '_index_factory,_series_name,_index_start,_index_end' |
| 30 | +DATE_RANGE = (date_range, "dti", datetime(2005, 1, 1), datetime(2005, 1, 10)) |
| 31 | +PERIOD_RANGE = (period_range, "pi", datetime(2005, 1, 1), datetime(2005, 1, 10)) |
| 32 | +TIMEDELTA_RANGE = (timedelta_range, "tdi", "1 day", "10 day") |
| 33 | + |
| 34 | +all_ts = pytest.mark.parametrize( |
| 35 | + "_index_factory,_series_name,_index_start,_index_end", |
| 36 | + [DATE_RANGE, PERIOD_RANGE, TIMEDELTA_RANGE], |
| 37 | +) |
| 38 | + |
| 39 | +all_1d_no_arg_interpolation_methods = pytest.mark.parametrize( |
| 40 | + "method", |
| 41 | + [ |
| 42 | + "linear", |
| 43 | + "time", |
| 44 | + "index", |
| 45 | + "values", |
| 46 | + "nearest", |
| 47 | + "zero", |
| 48 | + "slinear", |
| 49 | + "quadratic", |
| 50 | + "cubic", |
| 51 | + "barycentric", |
| 52 | + "krogh", |
| 53 | + "from_derivatives", |
| 54 | + "piecewise_polynomial", |
| 55 | + "pchip", |
| 56 | + "akima", |
| 57 | + ], |
| 58 | +) |
| 59 | + |
| 60 | + |
| 61 | +@pytest.fixture |
| 62 | +def create_index(_index_factory): |
| 63 | + def _create_index(*args, **kwargs): |
| 64 | + """return the _index_factory created using the args, kwargs""" |
| 65 | + return _index_factory(*args, **kwargs) |
| 66 | + |
| 67 | + return _create_index |
| 68 | + |
24 | 69 |
|
25 | 70 | @pytest.mark.parametrize("freq", ["2D", "1h"])
|
26 | 71 | @pytest.mark.parametrize(
|
@@ -89,6 +134,53 @@ def test_resample_interpolate(index):
|
89 | 134 | tm.assert_frame_equal(result, expected)
|
90 | 135 |
|
91 | 136 |
|
| 137 | +@all_1d_no_arg_interpolation_methods |
| 138 | +def test_resample_interpolate_regular_sampling_off_grid(method): |
| 139 | + # GH#21351 |
| 140 | + index = date_range("2000-01-01 00:01:00", periods=5, freq="2h") |
| 141 | + ser = Series(np.arange(5.0), index) |
| 142 | + |
| 143 | + # Resample to 1 hour sampling and interpolate with the given method |
| 144 | + ser_resampled = ser.resample("1h").interpolate(method) |
| 145 | + |
| 146 | + # Check that none of the resampled values are NaN, except the first one |
| 147 | + # which lies 1 minute before the first actual data point |
| 148 | + assert np.isnan(ser_resampled.iloc[0]) |
| 149 | + assert not ser_resampled.iloc[1:].isna().any() |
| 150 | + |
| 151 | + if method not in ["nearest", "zero"]: |
| 152 | + # Check that the resampled values are close to the expected values |
| 153 | + # except for methods with known inaccuracies |
| 154 | + assert np.all( |
| 155 | + np.isclose(ser_resampled.values[1:], np.arange(0.5, 4.5, 0.5), rtol=1.0e-1) |
| 156 | + ) |
| 157 | + |
| 158 | + |
| 159 | +@all_1d_no_arg_interpolation_methods |
| 160 | +def test_resample_interpolate_irregular_sampling(method): |
| 161 | + # GH#21351 |
| 162 | + ser = Series( |
| 163 | + np.linspace(0.0, 1.0, 5), |
| 164 | + index=DatetimeIndex( |
| 165 | + [ |
| 166 | + "2000-01-01 00:00:03", |
| 167 | + "2000-01-01 00:00:22", |
| 168 | + "2000-01-01 00:00:24", |
| 169 | + "2000-01-01 00:00:31", |
| 170 | + "2000-01-01 00:00:39", |
| 171 | + ] |
| 172 | + ), |
| 173 | + ) |
| 174 | + |
| 175 | + # Resample to 5 second sampling and interpolate with the given method |
| 176 | + ser_resampled = ser.resample("5s").interpolate(method) |
| 177 | + |
| 178 | + # Check that none of the resampled values are NaN, except the first one |
| 179 | + # which lies 3 seconds before the first actual data point |
| 180 | + assert np.isnan(ser_resampled.iloc[0]) |
| 181 | + assert not ser_resampled.iloc[1:].isna().any() |
| 182 | + |
| 183 | + |
92 | 184 | def test_raises_on_non_datetimelike_index():
|
93 | 185 | # this is a non datetimelike index
|
94 | 186 | xp = DataFrame()
|
|
0 commit comments