Skip to content

Commit ebe484a

Browse files
authored
BUG: Fix Grouper with a datetime key in conjunction with another key (pandas-dev#51414)
1 parent efaa11e commit ebe484a

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ Groupby/resample/rolling
300300
grouped :class:`Series` or :class:`DataFrame` was a :class:`DatetimeIndex`, :class:`TimedeltaIndex`
301301
or :class:`PeriodIndex`, and the ``groupby`` method was given a function as its first argument,
302302
the function operated on the whole index rather than each element of the index. (:issue:`51979`)
303+
- Bug in :meth:`GroupBy.groups` with a datetime key in conjunction with another key produced incorrect number of group keys (:issue:`51158`)
303304
- Bug in :meth:`GroupBy.var` failing to raise ``TypeError`` when called with datetime64 or :class:`PeriodDtype` values (:issue:`52128`)
304305
- Bug in :meth:`DataFrameGroupBy.apply` causing an error to be raised when the input :class:`DataFrame` was subset as a :class:`DataFrame` after groupby (``[['a']]`` and not ``['a']``) and the given callable returned :class:`Series` that were not all indexed the same. (:issue:`52444`)
305306
-

pandas/core/groupby/ops.py

+3
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,9 @@ def groups(self):
963963
}
964964
return result
965965

966+
def __iter__(self) -> Iterator[Hashable]:
967+
return iter(self.groupings[0].grouping_vector)
968+
966969
@property
967970
def nkeys(self) -> int:
968971
# still matches len(self.groupings), but we can hard-code

pandas/tests/groupby/test_grouping.py

+23
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,29 @@ def test_groupby_grouper_f_sanity_checked(self):
447447
expected.index.freq = None
448448
tm.assert_series_equal(result, expected)
449449

450+
def test_groupby_with_datetime_key(self):
451+
# GH 51158
452+
df = DataFrame(
453+
{
454+
"id": ["a", "b"] * 3,
455+
"b": date_range("2000-01-01", "2000-01-03", freq="9H"),
456+
}
457+
)
458+
grouper = Grouper(key="b", freq="D")
459+
gb = df.groupby([grouper, "id"])
460+
461+
# test number of groups
462+
expected = {
463+
(Timestamp("2000-01-01"), "a"): [0, 2],
464+
(Timestamp("2000-01-01"), "b"): [1],
465+
(Timestamp("2000-01-02"), "a"): [4],
466+
(Timestamp("2000-01-02"), "b"): [3, 5],
467+
}
468+
tm.assert_dict_equal(gb.groups, expected)
469+
470+
# test number of group keys
471+
assert len(gb.groups.keys()) == 4
472+
450473
def test_grouping_error_on_multidim_input(self, df):
451474
msg = "Grouper for '<class 'pandas.core.frame.DataFrame'>' not 1-dimensional"
452475
with pytest.raises(ValueError, match=msg):

0 commit comments

Comments
 (0)