Skip to content

Commit

Permalink
Make parameterizations more explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
mroeschke committed Dec 23, 2023
1 parent dc37a6d commit d147fce
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 135 deletions.
56 changes: 3 additions & 53 deletions pandas/tests/resample/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import numpy as np
import pytest

from pandas import (
DataFrame,
Series,
)
from pandas import Series

# The various methods we support
downsample_methods = [
Expand Down Expand Up @@ -60,18 +57,12 @@ def _index_freq():


@pytest.fixture
def _index_name():
"""Fixture for parametrization of index, series and frame."""
return None


@pytest.fixture
def index(_index_factory, _index_start, _index_end, _index_freq, _index_name):
def index(_index_factory, _index_start, _index_end):
"""
Fixture for parametrization of date_range, period_range and
timedelta_range indexes
"""
return _index_factory(_index_start, _index_end, freq=_index_freq, name=_index_name)
return _index_factory(_index_start, _index_end, freq="D", name=None)


@pytest.fixture
Expand Down Expand Up @@ -100,44 +91,3 @@ def series(index, _series_name, _static_values):
timedelta_range indexes
"""
return Series(_static_values, index=index, name=_series_name)


@pytest.fixture
def empty_series_dti(series):
"""
Fixture for parametrization of empty Series with date_range,
period_range and timedelta_range indexes
"""
return series[:0]


@pytest.fixture
def frame(index, _series_name, _static_values):
"""
Fixture for parametrization of DataFrame with date_range, period_range
and timedelta_range indexes
"""
# _series_name is intentionally unused
return DataFrame({"value": _static_values}, index=index)


@pytest.fixture
def empty_frame_dti(series):
"""
Fixture for parametrization of empty DataFrame with date_range,
period_range and timedelta_range indexes
"""
index = series.index[:0]
return DataFrame(index=index)


@pytest.fixture
def series_and_frame(frame_or_series, series, frame):
"""
Fixture for parametrization of Series and DataFrame with date_range,
period_range and timedelta_range indexes
"""
if frame_or_series == Series:
return series
if frame_or_series == DataFrame:
return frame
141 changes: 100 additions & 41 deletions pandas/tests/resample/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,43 +48,61 @@ def _create_index(*args, **kwargs):

@pytest.mark.parametrize("freq", ["2D", "1h"])
@pytest.mark.parametrize(
"_index_factory,_series_name,_index_start,_index_end", [DATE_RANGE, TIMEDELTA_RANGE]
"index",
[
timedelta_range("1 day", "10 day", freq="D"),
date_range(datetime(2005, 1, 1), datetime(2005, 1, 10), freq="D"),
],
)
def test_asfreq(series_and_frame, freq, create_index):
obj = series_and_frame
@pytest.mark.parametrize("klass", [DataFrame, Series])
def test_asfreq(klass, index, freq):
obj = klass(range(len(index)), index=index)
idx_range = date_range if isinstance(index, DatetimeIndex) else timedelta_range

result = obj.resample(freq).asfreq()
new_index = create_index(obj.index[0], obj.index[-1], freq=freq)
new_index = idx_range(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]
"index",
[
timedelta_range("1 day", "10 day", freq="D"),
date_range(datetime(2005, 1, 1), datetime(2005, 1, 10), freq="D"),
],
)
def test_asfreq_fill_value(series, create_index):
def test_asfreq_fill_value(index):
# test for fill value during resampling, issue 3715

ser = series
ser = Series(range(len(index)), index=index, name="a")
idx_range = date_range if isinstance(index, DatetimeIndex) else timedelta_range

result = ser.resample("1h").asfreq()
new_index = create_index(ser.index[0], ser.index[-1], freq="1h")
new_index = idx_range(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")
new_index = idx_range(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):
@pytest.mark.parametrize(
"index",
[
timedelta_range("1 day", "10 day", freq="D"),
date_range(datetime(2005, 1, 1), datetime(2005, 1, 10), freq="D"),
period_range(datetime(2005, 1, 1), datetime(2005, 1, 10), freq="D"),
],
)
def test_resample_interpolate(index):
# GH#12925
df = frame
df = DataFrame(range(len(index)), index=index)
warn = None
if isinstance(df.index, PeriodIndex):
warn = FutureWarning
Expand All @@ -106,12 +124,19 @@ def test_raises_on_non_datetimelike_index():
xp.resample("YE")


@all_ts
@pytest.mark.parametrize(
"index",
[
PeriodIndex([], freq="D", name="a"),
DatetimeIndex([], name="a"),
TimedeltaIndex([], name="a"),
],
)
@pytest.mark.parametrize("freq", ["ME", "D", "h"])
def test_resample_empty_series(freq, empty_series_dti, resample_method):
def test_resample_empty_series(freq, index, resample_method):
# GH12771 & GH12868

ser = empty_series_dti
ser = Series(index=index, dtype=float)
if freq == "ME" and isinstance(ser.index, TimedeltaIndex):
msg = (
"Resampling on a TimedeltaIndex requires fixed-duration `freq`, "
Expand Down Expand Up @@ -179,12 +204,19 @@ def test_resample_nat_index_series(freq, series, resample_method):
assert result.index.freq == expected.index.freq


@all_ts
@pytest.mark.parametrize(
"index",
[
PeriodIndex([], freq="D", name="a"),
DatetimeIndex([], name="a"),
TimedeltaIndex([], name="a"),
],
)
@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):
def test_resample_count_empty_series(freq, index, resample_method):
# GH28427
ser = empty_series_dti
ser = Series(index=index)
if freq == "ME" and isinstance(ser.index, TimedeltaIndex):
msg = (
"Resampling on a TimedeltaIndex requires fixed-duration `freq`, "
Expand Down Expand Up @@ -213,11 +245,13 @@ def test_resample_count_empty_series(freq, empty_series_dti, resample_method):
tm.assert_series_equal(result, expected)


@all_ts
@pytest.mark.parametrize(
"index", [DatetimeIndex([]), TimedeltaIndex([]), PeriodIndex([], freq="D")]
)
@pytest.mark.parametrize("freq", ["ME", "D", "h"])
def test_resample_empty_dataframe(empty_frame_dti, freq, resample_method):
def test_resample_empty_dataframe(index, freq, resample_method):
# GH13212
df = empty_frame_dti
df = DataFrame(index=index)
# count retains dimensions too
if freq == "ME" and isinstance(df.index, TimedeltaIndex):
msg = (
Expand Down Expand Up @@ -261,12 +295,13 @@ def test_resample_empty_dataframe(empty_frame_dti, freq, resample_method):
# test size for GH13212 (currently stays as df)


@all_ts
@pytest.mark.parametrize(
"index", [DatetimeIndex([]), TimedeltaIndex([]), PeriodIndex([], freq="D")]
)
@pytest.mark.parametrize("freq", ["ME", "D", "h"])
def test_resample_count_empty_dataframe(freq, empty_frame_dti):
def test_resample_count_empty_dataframe(freq, index):
# GH28427

empty_frame_dti["a"] = []
empty_frame_dti = DataFrame(index=index, columns=Index(["a"], dtype=object))

if freq == "ME" and isinstance(empty_frame_dti.index, TimedeltaIndex):
msg = (
Expand Down Expand Up @@ -295,12 +330,14 @@ def test_resample_count_empty_dataframe(freq, empty_frame_dti):
tm.assert_frame_equal(result, expected)


@all_ts
@pytest.mark.parametrize(
"index", [DatetimeIndex([]), TimedeltaIndex([]), PeriodIndex([], freq="D")]
)
@pytest.mark.parametrize("freq", ["ME", "D", "h"])
def test_resample_size_empty_dataframe(freq, empty_frame_dti):
def test_resample_size_empty_dataframe(freq, index):
# GH28427

empty_frame_dti["a"] = []
empty_frame_dti = DataFrame(index=index, columns=Index(["a"], dtype=object))

if freq == "ME" and isinstance(empty_frame_dti.index, TimedeltaIndex):
msg = (
Expand Down Expand Up @@ -361,27 +398,34 @@ def test_resample_empty_dtypes(index, dtype, resample_method):
pass


@all_ts
@pytest.mark.parametrize(
"index",
[
PeriodIndex([], freq="D", name="a"),
DatetimeIndex([], name="a"),
TimedeltaIndex([], name="a"),
],
)
@pytest.mark.parametrize("freq", ["ME", "D", "h"])
def test_apply_to_empty_series(empty_series_dti, freq):
def test_apply_to_empty_series(index, freq):
# GH 14313
ser = empty_series_dti
ser = Series(index=index)

if freq == "ME" and isinstance(empty_series_dti.index, TimedeltaIndex):
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):
empty_series_dti.resample(freq)
ser.resample(freq)
return
elif freq == "ME" and isinstance(empty_series_dti.index, PeriodIndex):
elif freq == "ME" and isinstance(ser.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):
if isinstance(ser.index, PeriodIndex):
warn = FutureWarning

with tm.assert_produces_warning(warn, match=msg):
Expand All @@ -394,9 +438,17 @@ def test_apply_to_empty_series(empty_series_dti, freq):
tm.assert_series_equal(result, expected, check_dtype=False)


@all_ts
def test_resampler_is_iterable(series):
@pytest.mark.parametrize(
"index",
[
timedelta_range("1 day", "10 day", freq="D"),
date_range(datetime(2005, 1, 1), datetime(2005, 1, 10), freq="D"),
period_range(datetime(2005, 1, 1), datetime(2005, 1, 10), freq="D"),
],
)
def test_resampler_is_iterable(index):
# GH 15314
series = Series(range(len(index)), index=index)
freq = "h"
tg = Grouper(freq=freq, convention="start")
msg = "Resampling with a PeriodIndex"
Expand All @@ -414,16 +466,23 @@ def test_resampler_is_iterable(series):
tm.assert_series_equal(rv, gv)


@all_ts
def test_resample_quantile(series):
@pytest.mark.parametrize(
"index",
[
timedelta_range("1 day", "10 day", freq="D"),
date_range(datetime(2005, 1, 1), datetime(2005, 1, 10), freq="D"),
period_range(datetime(2005, 1, 1), datetime(2005, 1, 10), freq="D"),
],
)
def test_resample_quantile(index):
# GH 15023
ser = series
ser = Series(range(len(index)), index=index)
q = 0.75
freq = "h"

msg = "Resampling with a PeriodIndex"
warn = None
if isinstance(series.index, PeriodIndex):
if isinstance(ser.index, PeriodIndex):
warn = FutureWarning
with tm.assert_produces_warning(warn, match=msg):
result = ser.resample(freq).quantile(q)
Expand Down

0 comments on commit d147fce

Please sign in to comment.