24
24
25
25
26
26
@pytest .fixture
27
- def groupby_with_truncated_bingrouper ():
27
+ def frame_for_truncated_bingrouper ():
28
28
"""
29
- GroupBy object such that gb.grouper is a BinGrouper and
30
- len(gb.grouper.result_index) < len(gb.grouper.group_keys_seq)
31
-
32
- Aggregations on this groupby should have
33
-
34
- dti = date_range("2013-09-01", "2013-10-01", freq="5D", name="Date")
35
-
36
- As either the index or an index level.
29
+ DataFrame used by groupby_with_truncated_bingrouper, made into
30
+ a separate fixture for easier re-use in
31
+ test_groupby_apply_timegrouper_with_nat_apply_squeeze
37
32
"""
38
33
df = DataFrame (
39
34
{
@@ -48,6 +43,22 @@ def groupby_with_truncated_bingrouper():
48
43
],
49
44
}
50
45
)
46
+ return df
47
+
48
+
49
+ @pytest .fixture
50
+ def groupby_with_truncated_bingrouper (frame_for_truncated_bingrouper ):
51
+ """
52
+ GroupBy object such that gb.grouper is a BinGrouper and
53
+ len(gb.grouper.result_index) < len(gb.grouper.group_keys_seq)
54
+
55
+ Aggregations on this groupby should have
56
+
57
+ dti = date_range("2013-09-01", "2013-10-01", freq="5D", name="Date")
58
+
59
+ As either the index or an index level.
60
+ """
61
+ df = frame_for_truncated_bingrouper
51
62
52
63
tdg = Grouper (key = "Date" , freq = "5D" )
53
64
gb = df .groupby (tdg )
@@ -847,3 +858,31 @@ def test_groupby_apply_timegrouper_with_nat_scalar_returns(
847
858
)
848
859
849
860
tm .assert_series_equal (res , expected )
861
+
862
+ def test_groupby_apply_timegrouper_with_nat_apply_squeeze (
863
+ self , frame_for_truncated_bingrouper
864
+ ):
865
+ df = frame_for_truncated_bingrouper
866
+
867
+ # We need to create a GroupBy object with only one non-NaT group,
868
+ # so use a huge freq so that all non-NaT dates will be grouped together
869
+ tdg = Grouper (key = "Date" , freq = "100Y" )
870
+
871
+ with tm .assert_produces_warning (FutureWarning , match = "`squeeze` parameter" ):
872
+ gb = df .groupby (tdg , squeeze = True )
873
+
874
+ # check that we will go through the singular_series path
875
+ # in _wrap_applied_output_series
876
+ assert gb .ngroups == 1
877
+ assert gb ._selected_obj ._get_axis (gb .axis ).nlevels == 1
878
+
879
+ # function that returns a Series
880
+ res = gb .apply (lambda x : x ["Quantity" ] * 2 )
881
+
882
+ key = Timestamp ("2013-12-31" )
883
+ ordering = df ["Date" ].sort_values ().dropna ().index
884
+ mi = MultiIndex .from_product ([[key ], ordering ], names = ["Date" , None ])
885
+
886
+ ex_values = df ["Quantity" ].take (ordering ).values * 2
887
+ expected = Series (ex_values , index = mi , name = "Quantity" )
888
+ tm .assert_series_equal (res , expected )
0 commit comments