Skip to content

Commit 9030685

Browse files
authored
BUG: GroupBy.apply with timegrouper and NaT (pandas-dev#43509)
1 parent 625ec6b commit 9030685

File tree

3 files changed

+49
-14
lines changed

3 files changed

+49
-14
lines changed

pandas/core/groupby/generic.py

-4
Original file line numberDiff line numberDiff line change
@@ -1168,11 +1168,7 @@ def _wrap_applied_output_series(
11681168
applied_index = self._selected_obj._get_axis(self.axis)
11691169
singular_series = len(values) == 1 and applied_index.nlevels == 1
11701170

1171-
# assign the name to this series
11721171
if singular_series:
1173-
keys = self.grouper.group_keys_seq
1174-
values[0].name = keys[0]
1175-
11761172
# GH2893
11771173
# we have series in the values array, we want to
11781174
# produce a series:

pandas/core/groupby/groupby.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1037,7 +1037,7 @@ def reset_identity(values):
10371037
if self.as_index:
10381038

10391039
# possible MI return case
1040-
group_keys = self.grouper.group_keys_seq
1040+
group_keys = self.grouper.result_index
10411041
group_levels = self.grouper.levels
10421042
group_names = self.grouper.names
10431043

pandas/tests/groupby/test_timegrouper.py

+48-9
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,11 @@
2424

2525

2626
@pytest.fixture
27-
def groupby_with_truncated_bingrouper():
27+
def frame_for_truncated_bingrouper():
2828
"""
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
3732
"""
3833
df = DataFrame(
3934
{
@@ -48,6 +43,22 @@ def groupby_with_truncated_bingrouper():
4843
],
4944
}
5045
)
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
5162

5263
tdg = Grouper(key="Date", freq="5D")
5364
gb = df.groupby(tdg)
@@ -847,3 +858,31 @@ def test_groupby_apply_timegrouper_with_nat_scalar_returns(
847858
)
848859

849860
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

Comments
 (0)