Skip to content

Commit d522606

Browse files
committed
Refactor groupby-resample tests via TestCase to remove duplicate code.
1 parent 645cf1f commit d522606

File tree

1 file changed

+61
-77
lines changed

1 file changed

+61
-77
lines changed

pandas/tests/resample/test_resampler_grouper.py

+61-77
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import unittest
12
from textwrap import dedent
23

34
import numpy as np
@@ -538,80 +539,63 @@ def test_groupby_resample_size_all_index_same():
538539
tm.assert_series_equal(result, expected)
539540

540541

541-
def test_groupby_resample_on_index_with_list_of_keys():
542-
# GH 50840
543-
df = DataFrame(
544-
data={
545-
"group": [0, 0, 0, 0, 1, 1, 1, 1],
546-
"val": [3, 1, 4, 1, 5, 9, 2, 6],
547-
},
548-
index=Series(
549-
date_range(start="2016-01-01", periods=8),
550-
name="date",
551-
),
552-
)
553-
result = df.groupby("group").resample("2D")[["val"]].mean()
554-
expected = DataFrame(
555-
data={
556-
"val": [2.0, 2.5, 7.0, 4.0],
557-
},
558-
index=Index(
559-
data=[
560-
(0, Timestamp("2016-01-01")),
561-
(0, Timestamp("2016-01-03")),
562-
(1, Timestamp("2016-01-05")),
563-
(1, Timestamp("2016-01-07")),
564-
],
565-
name=("group", "date"),
566-
),
567-
)
568-
tm.assert_frame_equal(result, expected)
569-
570-
571-
def test_groupby_resample_on_index_with_list_of_keys_multi_columns():
572-
# GH 50876
573-
df = DataFrame(
574-
data={
575-
"group": [0, 0, 0, 0, 1, 1, 1, 1],
576-
"first_val": [3, 1, 4, 1, 5, 9, 2, 6],
577-
"second_val": [2, 7, 1, 8, 2, 8, 1, 8],
578-
"third_val": [1, 4, 1, 4, 2, 1, 3, 5],
579-
},
580-
index=Series(
581-
date_range(start="2016-01-01", periods=8),
582-
name="date",
583-
),
584-
)
585-
result = df.groupby("group").resample("2D")[["first_val", "second_val"]].mean()
586-
expected = DataFrame(
587-
data={
588-
"first_val": [2.0, 2.5, 7.0, 4.0],
589-
"second_val": [4.5, 4.5, 5.0, 4.5],
590-
},
591-
index=Index(
592-
data=[
593-
(0, Timestamp("2016-01-01")),
594-
(0, Timestamp("2016-01-03")),
595-
(1, Timestamp("2016-01-05")),
596-
(1, Timestamp("2016-01-07")),
597-
],
598-
name=("group", "date"),
599-
),
600-
)
601-
tm.assert_frame_equal(result, expected)
602-
603-
604-
def test_groupby_resample_on_index_with_list_of_keys_missing_column():
605-
# GH 50876
606-
df = DataFrame(
607-
data={
608-
"group": [0, 0, 0, 0, 1, 1, 1, 1],
609-
"val": [3, 1, 4, 1, 5, 9, 2, 6],
610-
},
611-
index=Series(
612-
date_range(start="2016-01-01", periods=8),
613-
name="date",
614-
),
615-
)
616-
with pytest.raises(KeyError, match="Columns not found"):
617-
df.groupby("group").resample("2D")[["val_not_in_dataframe"]].mean()
542+
class TestGroupByResampleTimeIndex(unittest.TestCase):
543+
"""Test groupby resample with a time index where a list of columns is given."""
544+
def setUp(self) -> None:
545+
self.df = DataFrame(
546+
data={
547+
"group": [0, 0, 0, 0, 1, 1, 1, 1],
548+
"first_val": [3, 1, 4, 1, 5, 9, 2, 6],
549+
"second_val": [2, 7, 1, 8, 2, 8, 1, 8],
550+
"third_val": [1, 4, 1, 4, 2, 1, 3, 5],
551+
},
552+
index=Series(
553+
date_range(start="2016-01-01", periods=8),
554+
name="date",
555+
),
556+
)
557+
558+
def test_list_of_one_key(self):
559+
# GH 50840
560+
result = self.df.groupby("group").resample("2D")[["first_val"]].mean()
561+
expected = DataFrame(
562+
data={
563+
"first_val": [2.0, 2.5, 7.0, 4.0],
564+
},
565+
index=Index(
566+
data=[
567+
(0, Timestamp("2016-01-01")),
568+
(0, Timestamp("2016-01-03")),
569+
(1, Timestamp("2016-01-05")),
570+
(1, Timestamp("2016-01-07")),
571+
],
572+
name=("group", "date"),
573+
),
574+
)
575+
tm.assert_frame_equal(result, expected)
576+
577+
def test_list_of_multiple_keys(self):
578+
# GH 50876
579+
result = self.df.groupby("group").resample("2D")[["first_val", "second_val"]].mean()
580+
expected = DataFrame(
581+
data={
582+
"first_val": [2.0, 2.5, 7.0, 4.0],
583+
"second_val": [4.5, 4.5, 5.0, 4.5],
584+
},
585+
index=Index(
586+
data=[
587+
(0, Timestamp("2016-01-01")),
588+
(0, Timestamp("2016-01-03")),
589+
(1, Timestamp("2016-01-05")),
590+
(1, Timestamp("2016-01-07")),
591+
],
592+
name=("group", "date"),
593+
),
594+
)
595+
tm.assert_frame_equal(result, expected)
596+
597+
def test_missing_key_raises_KeyError(self):
598+
"""Test a key that is not in the list of columns."""
599+
# GH 50876
600+
with pytest.raises(KeyError, match="Columns not found"):
601+
self.df.groupby("group").resample("2D")[["val_not_in_dataframe"]].mean()

0 commit comments

Comments
 (0)