Skip to content

Commit fba6723

Browse files
authored
Bug fix using GroupBy.resample produces inconsistent behavior when calling it over empty df #47705 (#47672)
* DOC #45443 edited the documentation of where/mask functions * DOC #45443 edited the documentation of where/mask functions * Update generic.py * Bug 43767 fix * fixing lines doc * visual indent * visual indent * indentation * grouby.resample bug * groubby.sample * syntax * syntax * syntax * syntax * what's new * flake8 error * pytest * blank line * editting resample * editting resample * syntax * syntax * syntax * space * space * space * inplace * spelling * test * test resampler * tests * tests * Update resample.py * Update resample.py * Update resample.py * Update v1.6.0.rst
1 parent e6024c1 commit fba6723

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

doc/source/whatsnew/v1.6.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ Groupby/resample/rolling
244244
- Bug in :class:`.ExponentialMovingWindow` with ``online`` not raising a ``NotImplementedError`` for unsupported operations (:issue:`48834`)
245245
- Bug in :meth:`DataFrameGroupBy.sample` raises ``ValueError`` when the object is empty (:issue:`48459`)
246246
- Bug in :meth:`Series.groupby` raises ``ValueError`` when an entry of the index is equal to the name of the index (:issue:`48567`)
247+
- Bug in :meth:`DataFrameGroupBy.resample` produces inconsistent results when passing empty DataFrame (:issue:`47705`)
247248
-
248249

249250
Reshaping

pandas/core/resample.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -522,11 +522,21 @@ def _wrap_result(self, result):
522522
"""
523523
Potentially wrap any results.
524524
"""
525+
# GH 47705
526+
obj = self.obj
527+
if (
528+
isinstance(result, ABCDataFrame)
529+
and result.empty
530+
and not isinstance(result.index, PeriodIndex)
531+
):
532+
result = result.set_index(
533+
_asfreq_compat(obj.index[:0], freq=self.freq), append=True
534+
)
535+
525536
if isinstance(result, ABCSeries) and self._selection is not None:
526537
result.name = self._selection
527538

528539
if isinstance(result, ABCSeries) and result.empty:
529-
obj = self.obj
530540
# When index is all NaT, result is empty but index is not
531541
result.index = _asfreq_compat(obj.index[:0], freq=self.freq)
532542
result.name = getattr(obj, "name", None)

pandas/tests/resample/test_resampler_grouper.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,11 @@ def test_empty(keys):
435435
# GH 26411
436436
df = DataFrame([], columns=["a", "b"], index=TimedeltaIndex([]))
437437
result = df.groupby(keys).resample(rule=pd.to_timedelta("00:00:01")).mean()
438-
expected = DataFrame(columns=["a", "b"]).set_index(keys, drop=False)
438+
expected = (
439+
DataFrame(columns=["a", "b"])
440+
.set_index(keys, drop=False)
441+
.set_index(TimedeltaIndex([]), append=True)
442+
)
439443
if len(keys) == 1:
440444
expected.index.name = keys[0]
441445

@@ -497,3 +501,19 @@ def test_groupby_resample_with_list_of_keys():
497501
),
498502
)
499503
tm.assert_frame_equal(result, expected)
504+
505+
506+
@pytest.mark.parametrize("keys", [["a"], ["a", "b"]])
507+
def test_resample_empty_Dataframe(keys):
508+
# GH 47705
509+
df = DataFrame([], columns=["a", "b", "date"])
510+
df["date"] = pd.to_datetime(df["date"])
511+
df = df.set_index("date")
512+
result = df.groupby(keys).resample(rule=pd.to_timedelta("00:00:01")).mean()
513+
expected = DataFrame(columns=["a", "b", "date"]).set_index(keys, drop=False)
514+
expected["date"] = pd.to_datetime(expected["date"])
515+
expected = expected.set_index("date", append=True, drop=True)
516+
if len(keys) == 1:
517+
expected.index.name = keys[0]
518+
519+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)