Skip to content

TST: add tests for period resample sum with min_count #29762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion pandas/tests/resample/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pandas import DataFrame, Series
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.datetimes import DatetimeIndex, date_range
from pandas.core.indexes.period import PeriodIndex, period_range
from pandas.core.indexes.timedeltas import TimedeltaIndex, timedelta_range
import pandas.util.testing as tm
Expand Down Expand Up @@ -267,3 +267,60 @@ def test_resample_quantile(series):
result = s.resample(freq).quantile(q)
expected = s.resample(freq).agg(lambda x: x.quantile(q)).rename(s.name)
tm.assert_series_equal(result, expected)


@pytest.mark.parametrize(
"_index_factory,_series_name,_index_start,_index_end", [DATE_RANGE, PERIOD_RANGE]
)
@pytest.mark.parametrize(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ganevgv rather than nesting a dict, just make data, index paramters e.g.

@pytest.mark.parameterize('freq, data, index')

otherwise this is very hard to follow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jreback - removed the nested dict and changed the format to:

@pytest.mark.parameterize("freq, result_name, result_data, result_index, result_freq")

"freq, result_name, result_data, result_index, result_freq",
[
(
"D",
"dti",
[1.0] * 5 + [np.nan] * 5,
["2005-01-{}".format(i) for i in range(1, 11)],
"D",
),
(
"D",
"pi",
[1.0] * 5 + [np.nan] * 5,
["2005-01-{}".format(i) for i in range(1, 11)],
"D",
),
(
"W",
"dti",
[2.0, 3.0, np.nan],
["2005-01-02", "2005-01-09", "2005-01-16"],
"W-SUN",
),
(
"W",
"pi",
[2.0, 3.0, np.nan],
["2004-12-27/2005-01-02", "2005-01-03/2005-01-09", "2005-01-10/2005-01-16"],
"W-SUN",
),
("M", "dti", [5.0], ["2005-01-31"], "M"),
("M", "pi", [5.0], ["2005-01"], "M"),
],
)
def test_resample_sum(
series, freq, result_name, result_data, result_index, result_freq
):
# GH 19974
series[:5] = 1
series[5:] = np.nan
result = series.resample(freq).sum(min_count=1)

if isinstance(series.index, PeriodIndex) and result_name == "pi":
index = PeriodIndex(result_index, freq=result_freq)
elif isinstance(series.index, DatetimeIndex) and result_name == "dti":
index = DatetimeIndex(result_index, freq=result_freq)
else:
pytest.skip("unsupported configuration")

expected = Series(result_data, index, name=result_name)
tm.assert_series_equal(result, expected)
14 changes: 13 additions & 1 deletion pandas/tests/resample/test_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pandas as pd
from pandas import DataFrame, Series
from pandas.core.indexes.timedeltas import timedelta_range
from pandas.core.indexes.timedeltas import TimedeltaIndex, timedelta_range
import pandas.util.testing as tm


Expand Down Expand Up @@ -125,3 +125,15 @@ def test_resample_timedelta_values():
tm.assert_series_equal(res, exp)
res = df["time"].resample("2D").first()
tm.assert_series_equal(res, exp)


def test_resample_sum():
# GH 19974
data = [1.0] * 5 + [np.nan] * 5
index = timedelta_range("1 day", "10 day", freq="D")
series = Series(data, index=index)
result = series.resample("D").sum(min_count=1)

index = TimedeltaIndex(["{} days".format(i) for i in range(1, 11)], freq="D")
expected = Series(data, index=index)
tm.assert_series_equal(result, expected)