Skip to content

Commit e8070f1

Browse files
committed
Fix issue, add test
1 parent 10b6044 commit e8070f1

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

doc/source/whatsnew/v2.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,6 +1367,7 @@ Groupby/resample/rolling
13671367
- Bug in :meth:`.DataFrameGroupBy.describe` produced incorrect results when data had duplicate columns (:issue:`50806`)
13681368
- Bug in :meth:`.DataFrameGroupBy.agg` with ``engine="numba"`` failing to respect ``as_index=False`` (:issue:`51228`)
13691369
- Bug in :meth:`DataFrameGroupBy.agg`, :meth:`SeriesGroupBy.agg`, and :meth:`Resampler.agg` would ignore arguments when passed a list of functions (:issue:`50863`)
1370+
- Bug in :meth:`BaseGrouper.groups` when grouping by datetime, zip() was using :meth:`BaseGrouper.__iter__` that produced incorrect result in case of :class:`BinGrouper`. Fixed by adding :meth:`BinGrouper.__iter__` that returns correct iterable (:issue:`51158`)
13701371
-
13711372

13721373
Reshaping

pandas/core/groupby/ops.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,10 @@ def groups(self):
10851085
}
10861086
return result
10871087

1088+
def __iter__(self) -> Iterator[Hashable]:
1089+
return iter(self.groupings[0].grouping_vector)
1090+
1091+
10881092
@property
10891093
def nkeys(self) -> int:
10901094
# still matches len(self.groupings), but we can hard-code

pandas/tests/groupby/test_grouping.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,20 @@ def test_groupby_grouper_f_sanity_checked(self):
442442
with pytest.raises(AssertionError, match=msg):
443443
ts.groupby(lambda key: key[0:6])
444444

445+
def test_groupby_with_datetime_key(self):
446+
# GH 51158
447+
df = DataFrame({"id": ["a", "b"] * 3, "b": pd.date_range("2000-01-01", "2000-01-03", freq="9H")})
448+
grouper = pd.Grouper(key='b', freq="D")
449+
g = df.groupby([grouper, 'id'])
450+
451+
# test number of groups
452+
expected = {(pd.Timestamp("2000-01-01"), 'a'): [0, 2], (pd.Timestamp("2000-01-01"), 'b'): [1],
453+
(pd.Timestamp("2000-01-02"), 'a'): [4], (pd.Timestamp("2000-01-02"), 'b'): [3, 5]}
454+
tm.assert_dict_equal(g.groups, expected)
455+
456+
# test number of group keys
457+
tm.assert_equal(len(g.groups.keys()), 4)
458+
445459
def test_grouping_error_on_multidim_input(self, df):
446460
msg = "Grouper for '<class 'pandas.core.frame.DataFrame'>' not 1-dimensional"
447461
with pytest.raises(ValueError, match=msg):

0 commit comments

Comments
 (0)