Skip to content

Commit c11dead

Browse files
committed
move and generalise test to include DTI, PI and TDI
1 parent b90a86c commit c11dead

File tree

2 files changed

+77
-14
lines changed

2 files changed

+77
-14
lines changed

pandas/tests/resample/test_base.py

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from pandas import DataFrame, Series
88
from pandas.core.groupby.groupby import DataError
99
from pandas.core.groupby.grouper import Grouper
10-
from pandas.core.indexes.datetimes import date_range
10+
from pandas.core.indexes.datetimes import DatetimeIndex, date_range
1111
from pandas.core.indexes.period import PeriodIndex, period_range
1212
from pandas.core.indexes.timedeltas import TimedeltaIndex, timedelta_range
1313
import pandas.util.testing as tm
@@ -274,3 +274,79 @@ def test_resample_quantile(series):
274274
result = s.resample(freq).quantile(q)
275275
expected = s.resample(freq).agg(lambda x: x.quantile(q)).rename(s.name)
276276
tm.assert_series_equal(result, expected)
277+
278+
279+
@all_ts
280+
@pytest.mark.parametrize(
281+
"freq, result_name, result_data, result_index, result_freq",
282+
[
283+
(
284+
"D",
285+
"dti",
286+
[1.0] * 5 + [np.nan] * 5,
287+
["2005-01-{}".format(i) for i in range(1, 11)],
288+
"D",
289+
),
290+
(
291+
"D",
292+
"pi",
293+
[1.0] * 5 + [np.nan] * 5,
294+
["2005-01-{}".format(i) for i in range(1, 11)],
295+
"D",
296+
),
297+
(
298+
"D",
299+
"tdi",
300+
[1.0] * 5 + [np.nan] * 5,
301+
["{} days".format(i) for i in range(1, 11)],
302+
"D",
303+
),
304+
(
305+
"W",
306+
"dti",
307+
[2.0, 3.0, np.nan],
308+
["2005-01-02", "2005-01-09", "2005-01-16"],
309+
"W-SUN",
310+
),
311+
(
312+
"W",
313+
"pi",
314+
[2.0, 3.0, np.nan],
315+
["2004-12-27/2005-01-02", "2005-01-03/2005-01-09", "2005-01-10/2005-01-16"],
316+
"W-SUN",
317+
),
318+
("W", "", "", "", ""),
319+
("M", "dti", [5.0], ["2005-01-31"], "M"),
320+
("M", "pi", [5.0], ["2005-01"], "M"),
321+
("M", "", "", "", ""),
322+
],
323+
)
324+
def test_resample_sum(
325+
series, freq, result_name, result_data, result_index, result_freq
326+
):
327+
# GH 19974
328+
series[:5] = 1
329+
series[5:] = np.nan
330+
331+
if isinstance(series.index, TimedeltaIndex) and freq != "D":
332+
msg = ".* is a non-fixed frequency"
333+
with pytest.raises(ValueError, match=msg):
334+
result = series.resample(freq).sum(min_count=1)
335+
336+
else:
337+
result = series.resample(freq).sum(min_count=1)
338+
339+
if isinstance(series.index, DatetimeIndex) and result_name == "dti":
340+
index = DatetimeIndex(result_index, freq=result_freq)
341+
expected = Series(result_data, index, name=result_name)
342+
tm.assert_series_equal(result, expected)
343+
344+
if isinstance(series.index, PeriodIndex) and result_name == "pi":
345+
index = PeriodIndex(result_index, freq=result_freq)
346+
expected = Series(result_data, index, name=result_name)
347+
tm.assert_series_equal(result, expected)
348+
349+
if isinstance(series.index, TimedeltaIndex) and result_name == "tdi":
350+
index = TimedeltaIndex(result_index, freq=result_freq)
351+
expected = Series(result_data, index, name=result_name)
352+
tm.assert_series_equal(result, expected)

pandas/tests/resample/test_period_index.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -548,19 +548,6 @@ def test_quarterly_resampling(self):
548548
exp = ts.to_timestamp().resample("A").mean().to_period()
549549
tm.assert_series_equal(result, exp)
550550

551-
def test_period_resample_sum_min_count(self):
552-
# GH 19974
553-
index = date_range(start="2018", freq="M", periods=6)
554-
data = np.ones(6)
555-
data[3:6] = np.nan
556-
period = Series(data, index).to_period()
557-
result = period.resample("Q").sum(min_count=1)
558-
559-
index = PeriodIndex(["2018Q1", "2018Q2"], freq="Q-DEC")
560-
expected = Series([3, np.nan], index)
561-
562-
tm.assert_series_equal(result, expected)
563-
564551
def test_resample_weekly_bug_1726(self):
565552
# 8/6/12 is a Monday
566553
ind = date_range(start="8/6/2012", end="8/26/2012", freq="D")

0 commit comments

Comments
 (0)