File size: 15,475 Bytes
7885a28 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 |
from datetime import datetime
import numpy as np
import pytest
from pandas.core.dtypes.common import is_extension_array_dtype
import pandas as pd
from pandas import (
DataFrame,
DatetimeIndex,
Index,
MultiIndex,
NaT,
PeriodIndex,
Series,
TimedeltaIndex,
)
import pandas._testing as tm
from pandas.core.groupby.groupby import DataError
from pandas.core.groupby.grouper import Grouper
from pandas.core.indexes.datetimes import date_range
from pandas.core.indexes.period import period_range
from pandas.core.indexes.timedeltas import timedelta_range
from pandas.core.resample import _asfreq_compat
# a fixture value can be overridden by the test parameter value. Note that the
# value of the fixture can be overridden this way even if the test doesn't use
# it directly (doesn't mention it in the function prototype).
# see https://docs.pytest.org/en/latest/fixture.html#override-a-fixture-with-direct-test-parametrization # noqa: E501
# in this module we override the fixture values defined in conftest.py
# tuples of '_index_factory,_series_name,_index_start,_index_end'
DATE_RANGE = (date_range, "dti", datetime(2005, 1, 1), datetime(2005, 1, 10))
PERIOD_RANGE = (period_range, "pi", datetime(2005, 1, 1), datetime(2005, 1, 10))
TIMEDELTA_RANGE = (timedelta_range, "tdi", "1 day", "10 day")
all_ts = pytest.mark.parametrize(
"_index_factory,_series_name,_index_start,_index_end",
[DATE_RANGE, PERIOD_RANGE, TIMEDELTA_RANGE],
)
@pytest.fixture
def create_index(_index_factory):
def _create_index(*args, **kwargs):
"""return the _index_factory created using the args, kwargs"""
return _index_factory(*args, **kwargs)
return _create_index
@pytest.mark.parametrize("freq", ["2D", "1h"])
@pytest.mark.parametrize(
"_index_factory,_series_name,_index_start,_index_end", [DATE_RANGE, TIMEDELTA_RANGE]
)
def test_asfreq(series_and_frame, freq, create_index):
obj = series_and_frame
result = obj.resample(freq).asfreq()
new_index = create_index(obj.index[0], obj.index[-1], freq=freq)
expected = obj.reindex(new_index)
tm.assert_almost_equal(result, expected)
@pytest.mark.parametrize(
"_index_factory,_series_name,_index_start,_index_end", [DATE_RANGE, TIMEDELTA_RANGE]
)
def test_asfreq_fill_value(series, create_index):
# test for fill value during resampling, issue 3715
ser = series
result = ser.resample("1h").asfreq()
new_index = create_index(ser.index[0], ser.index[-1], freq="1h")
expected = ser.reindex(new_index)
tm.assert_series_equal(result, expected)
# Explicit cast to float to avoid implicit cast when setting None
frame = ser.astype("float").to_frame("value")
frame.iloc[1] = None
result = frame.resample("1h").asfreq(fill_value=4.0)
new_index = create_index(frame.index[0], frame.index[-1], freq="1h")
expected = frame.reindex(new_index, fill_value=4.0)
tm.assert_frame_equal(result, expected)
@all_ts
def test_resample_interpolate(frame):
# GH#12925
df = frame
warn = None
if isinstance(df.index, PeriodIndex):
warn = FutureWarning
msg = "Resampling with a PeriodIndex is deprecated"
with tm.assert_produces_warning(warn, match=msg):
result = df.resample("1min").asfreq().interpolate()
expected = df.resample("1min").interpolate()
tm.assert_frame_equal(result, expected)
def test_raises_on_non_datetimelike_index():
# this is a non datetimelike index
xp = DataFrame()
msg = (
"Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, "
"but got an instance of 'RangeIndex'"
)
with pytest.raises(TypeError, match=msg):
xp.resample("YE")
@all_ts
@pytest.mark.parametrize("freq", ["ME", "D", "h"])
def test_resample_empty_series(freq, empty_series_dti, resample_method):
# GH12771 & GH12868
ser = empty_series_dti
if freq == "ME" and isinstance(ser.index, TimedeltaIndex):
msg = (
"Resampling on a TimedeltaIndex requires fixed-duration `freq`, "
"e.g. '24h' or '3D', not <MonthEnd>"
)
with pytest.raises(ValueError, match=msg):
ser.resample(freq)
return
elif freq == "ME" and isinstance(ser.index, PeriodIndex):
# index is PeriodIndex, so convert to corresponding Period freq
freq = "M"
warn = None
if isinstance(ser.index, PeriodIndex):
warn = FutureWarning
msg = "Resampling with a PeriodIndex is deprecated"
with tm.assert_produces_warning(warn, match=msg):
rs = ser.resample(freq)
result = getattr(rs, resample_method)()
if resample_method == "ohlc":
expected = DataFrame(
[], index=ser.index[:0].copy(), columns=["open", "high", "low", "close"]
)
expected.index = _asfreq_compat(ser.index, freq)
tm.assert_frame_equal(result, expected, check_dtype=False)
else:
expected = ser.copy()
expected.index = _asfreq_compat(ser.index, freq)
tm.assert_series_equal(result, expected, check_dtype=False)
tm.assert_index_equal(result.index, expected.index)
assert result.index.freq == expected.index.freq
@all_ts
@pytest.mark.parametrize(
"freq",
[
pytest.param("ME", marks=pytest.mark.xfail(reason="Don't know why this fails")),
"D",
"h",
],
)
def test_resample_nat_index_series(freq, series, resample_method):
# GH39227
ser = series.copy()
ser.index = PeriodIndex([NaT] * len(ser), freq=freq)
msg = "Resampling with a PeriodIndex is deprecated"
with tm.assert_produces_warning(FutureWarning, match=msg):
rs = ser.resample(freq)
result = getattr(rs, resample_method)()
if resample_method == "ohlc":
expected = DataFrame(
[], index=ser.index[:0].copy(), columns=["open", "high", "low", "close"]
)
tm.assert_frame_equal(result, expected, check_dtype=False)
else:
expected = ser[:0].copy()
tm.assert_series_equal(result, expected, check_dtype=False)
tm.assert_index_equal(result.index, expected.index)
assert result.index.freq == expected.index.freq
@all_ts
@pytest.mark.parametrize("freq", ["ME", "D", "h"])
@pytest.mark.parametrize("resample_method", ["count", "size"])
def test_resample_count_empty_series(freq, empty_series_dti, resample_method):
# GH28427
ser = empty_series_dti
if freq == "ME" and isinstance(ser.index, TimedeltaIndex):
msg = (
"Resampling on a TimedeltaIndex requires fixed-duration `freq`, "
"e.g. '24h' or '3D', not <MonthEnd>"
)
with pytest.raises(ValueError, match=msg):
ser.resample(freq)
return
elif freq == "ME" and isinstance(ser.index, PeriodIndex):
# index is PeriodIndex, so convert to corresponding Period freq
freq = "M"
warn = None
if isinstance(ser.index, PeriodIndex):
warn = FutureWarning
msg = "Resampling with a PeriodIndex is deprecated"
with tm.assert_produces_warning(warn, match=msg):
rs = ser.resample(freq)
result = getattr(rs, resample_method)()
index = _asfreq_compat(ser.index, freq)
expected = Series([], dtype="int64", index=index, name=ser.name)
tm.assert_series_equal(result, expected)
@all_ts
@pytest.mark.parametrize("freq", ["ME", "D", "h"])
def test_resample_empty_dataframe(empty_frame_dti, freq, resample_method):
# GH13212
df = empty_frame_dti
# count retains dimensions too
if freq == "ME" and isinstance(df.index, TimedeltaIndex):
msg = (
"Resampling on a TimedeltaIndex requires fixed-duration `freq`, "
"e.g. '24h' or '3D', not <MonthEnd>"
)
with pytest.raises(ValueError, match=msg):
df.resample(freq, group_keys=False)
return
elif freq == "ME" and isinstance(df.index, PeriodIndex):
# index is PeriodIndex, so convert to corresponding Period freq
freq = "M"
warn = None
if isinstance(df.index, PeriodIndex):
warn = FutureWarning
msg = "Resampling with a PeriodIndex is deprecated"
with tm.assert_produces_warning(warn, match=msg):
rs = df.resample(freq, group_keys=False)
result = getattr(rs, resample_method)()
if resample_method == "ohlc":
# TODO: no tests with len(df.columns) > 0
mi = MultiIndex.from_product([df.columns, ["open", "high", "low", "close"]])
expected = DataFrame(
[], index=df.index[:0].copy(), columns=mi, dtype=np.float64
)
expected.index = _asfreq_compat(df.index, freq)
elif resample_method != "size":
expected = df.copy()
else:
# GH14962
expected = Series([], dtype=np.int64)
expected.index = _asfreq_compat(df.index, freq)
tm.assert_index_equal(result.index, expected.index)
assert result.index.freq == expected.index.freq
tm.assert_almost_equal(result, expected)
# test size for GH13212 (currently stays as df)
@all_ts
@pytest.mark.parametrize("freq", ["ME", "D", "h"])
def test_resample_count_empty_dataframe(freq, empty_frame_dti):
# GH28427
empty_frame_dti["a"] = []
if freq == "ME" and isinstance(empty_frame_dti.index, TimedeltaIndex):
msg = (
"Resampling on a TimedeltaIndex requires fixed-duration `freq`, "
"e.g. '24h' or '3D', not <MonthEnd>"
)
with pytest.raises(ValueError, match=msg):
empty_frame_dti.resample(freq)
return
elif freq == "ME" and isinstance(empty_frame_dti.index, PeriodIndex):
# index is PeriodIndex, so convert to corresponding Period freq
freq = "M"
warn = None
if isinstance(empty_frame_dti.index, PeriodIndex):
warn = FutureWarning
msg = "Resampling with a PeriodIndex is deprecated"
with tm.assert_produces_warning(warn, match=msg):
rs = empty_frame_dti.resample(freq)
result = rs.count()
index = _asfreq_compat(empty_frame_dti.index, freq)
expected = DataFrame(dtype="int64", index=index, columns=Index(["a"], dtype=object))
tm.assert_frame_equal(result, expected)
@all_ts
@pytest.mark.parametrize("freq", ["ME", "D", "h"])
def test_resample_size_empty_dataframe(freq, empty_frame_dti):
# GH28427
empty_frame_dti["a"] = []
if freq == "ME" and isinstance(empty_frame_dti.index, TimedeltaIndex):
msg = (
"Resampling on a TimedeltaIndex requires fixed-duration `freq`, "
"e.g. '24h' or '3D', not <MonthEnd>"
)
with pytest.raises(ValueError, match=msg):
empty_frame_dti.resample(freq)
return
elif freq == "ME" and isinstance(empty_frame_dti.index, PeriodIndex):
# index is PeriodIndex, so convert to corresponding Period freq
freq = "M"
msg = "Resampling with a PeriodIndex"
warn = None
if isinstance(empty_frame_dti.index, PeriodIndex):
warn = FutureWarning
with tm.assert_produces_warning(warn, match=msg):
rs = empty_frame_dti.resample(freq)
result = rs.size()
index = _asfreq_compat(empty_frame_dti.index, freq)
expected = Series([], dtype="int64", index=index)
tm.assert_series_equal(result, expected)
@pytest.mark.parametrize(
"index",
[
PeriodIndex([], freq="M", name="a"),
DatetimeIndex([], name="a"),
TimedeltaIndex([], name="a"),
],
)
@pytest.mark.parametrize("dtype", [float, int, object, "datetime64[ns]"])
@pytest.mark.filterwarnings(r"ignore:PeriodDtype\[B\] is deprecated:FutureWarning")
def test_resample_empty_dtypes(index, dtype, resample_method):
# Empty series were sometimes causing a segfault (for the functions
# with Cython bounds-checking disabled) or an IndexError. We just run
# them to ensure they no longer do. (GH #10228)
warn = None
if isinstance(index, PeriodIndex):
# GH#53511
index = PeriodIndex([], freq="B", name=index.name)
warn = FutureWarning
msg = "Resampling with a PeriodIndex is deprecated"
empty_series_dti = Series([], index, dtype)
with tm.assert_produces_warning(warn, match=msg):
rs = empty_series_dti.resample("d", group_keys=False)
try:
getattr(rs, resample_method)()
except DataError:
# Ignore these since some combinations are invalid
# (ex: doing mean with dtype of np.object_)
pass
@all_ts
@pytest.mark.parametrize("freq", ["ME", "D", "h"])
def test_apply_to_empty_series(empty_series_dti, freq):
# GH 14313
ser = empty_series_dti
if freq == "ME" and isinstance(empty_series_dti.index, TimedeltaIndex):
msg = (
"Resampling on a TimedeltaIndex requires fixed-duration `freq`, "
"e.g. '24h' or '3D', not <MonthEnd>"
)
with pytest.raises(ValueError, match=msg):
empty_series_dti.resample(freq)
return
elif freq == "ME" and isinstance(empty_series_dti.index, PeriodIndex):
# index is PeriodIndex, so convert to corresponding Period freq
freq = "M"
msg = "Resampling with a PeriodIndex"
warn = None
if isinstance(empty_series_dti.index, PeriodIndex):
warn = FutureWarning
with tm.assert_produces_warning(warn, match=msg):
rs = ser.resample(freq, group_keys=False)
result = rs.apply(lambda x: 1)
with tm.assert_produces_warning(warn, match=msg):
expected = ser.resample(freq).apply("sum")
tm.assert_series_equal(result, expected, check_dtype=False)
@all_ts
def test_resampler_is_iterable(series):
# GH 15314
freq = "h"
tg = Grouper(freq=freq, convention="start")
msg = "Resampling with a PeriodIndex"
warn = None
if isinstance(series.index, PeriodIndex):
warn = FutureWarning
with tm.assert_produces_warning(warn, match=msg):
grouped = series.groupby(tg)
with tm.assert_produces_warning(warn, match=msg):
resampled = series.resample(freq)
for (rk, rv), (gk, gv) in zip(resampled, grouped):
assert rk == gk
tm.assert_series_equal(rv, gv)
@all_ts
def test_resample_quantile(series):
# GH 15023
ser = series
q = 0.75
freq = "h"
msg = "Resampling with a PeriodIndex"
warn = None
if isinstance(series.index, PeriodIndex):
warn = FutureWarning
with tm.assert_produces_warning(warn, match=msg):
result = ser.resample(freq).quantile(q)
expected = ser.resample(freq).agg(lambda x: x.quantile(q)).rename(ser.name)
tm.assert_series_equal(result, expected)
@pytest.mark.parametrize("how", ["first", "last"])
def test_first_last_skipna(any_real_nullable_dtype, skipna, how):
# GH#57019
if is_extension_array_dtype(any_real_nullable_dtype):
na_value = Series(dtype=any_real_nullable_dtype).dtype.na_value
else:
na_value = np.nan
df = DataFrame(
{
"a": [2, 1, 1, 2],
"b": [na_value, 3.0, na_value, 4.0],
"c": [na_value, 3.0, na_value, 4.0],
},
index=date_range("2020-01-01", periods=4, freq="D"),
dtype=any_real_nullable_dtype,
)
rs = df.resample("ME")
method = getattr(rs, how)
result = method(skipna=skipna)
gb = df.groupby(df.shape[0] * [pd.to_datetime("2020-01-31")])
expected = getattr(gb, how)(skipna=skipna)
expected.index.freq = "ME"
tm.assert_frame_equal(result, expected)
|