From 485d7724761e653b7e4e383498a5d0bc6b2cc05e Mon Sep 17 00:00:00 2001 From: steliospetrakis02 Date: Thu, 11 May 2023 02:17:51 +0300 Subject: [PATCH] Add test for groupby with TimeGrouper --- pandas/tests/groupby/test_groupby.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/pandas/tests/groupby/test_groupby.py b/pandas/tests/groupby/test_groupby.py index 2e432a768af9e..7bda7c575d994 100644 --- a/pandas/tests/groupby/test_groupby.py +++ b/pandas/tests/groupby/test_groupby.py @@ -3058,3 +3058,27 @@ def test_groupby_selection_other_methods(df): tm.assert_frame_equal( g.filter(lambda x: len(x) == 3), g_exp.filter(lambda x: len(x) == 3) ) + + +def test_groupby_with_Time_Grouper(): + idx2 = [ + to_datetime("2016-08-31 22:08:12.000"), + to_datetime("2016-08-31 22:09:12.200"), + to_datetime("2016-08-31 22:20:12.400"), + ] + + test_data = DataFrame( + {"quant": [1.0, 1.0, 3.0], "quant2": [1.0, 1.0, 3.0], "time2": idx2} + ) + + expected_output = DataFrame( + { + "time2": date_range("2016-08-31 22:08:00", periods=13, freq="1T"), + "quant": [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], + "quant2": [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], + } + ) + + df = test_data.groupby(Grouper(key="time2", freq="1T")).count().reset_index() + + tm.assert_frame_equal(df, expected_output)