Skip to content

Commit

Permalink
Bug fix using GroupBy.resample produces inconsistent behavior when ca…
Browse files Browse the repository at this point in the history
…lling 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
  • Loading branch information
ahmedibrhm committed Oct 3, 2022
1 parent e6024c1 commit fba6723
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.6.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ Groupby/resample/rolling
- Bug in :class:`.ExponentialMovingWindow` with ``online`` not raising a ``NotImplementedError`` for unsupported operations (:issue:`48834`)
- Bug in :meth:`DataFrameGroupBy.sample` raises ``ValueError`` when the object is empty (:issue:`48459`)
- Bug in :meth:`Series.groupby` raises ``ValueError`` when an entry of the index is equal to the name of the index (:issue:`48567`)
- Bug in :meth:`DataFrameGroupBy.resample` produces inconsistent results when passing empty DataFrame (:issue:`47705`)
-

Reshaping
Expand Down
12 changes: 11 additions & 1 deletion pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,11 +522,21 @@ def _wrap_result(self, result):
"""
Potentially wrap any results.
"""
# GH 47705
obj = self.obj
if (
isinstance(result, ABCDataFrame)
and result.empty
and not isinstance(result.index, PeriodIndex)
):
result = result.set_index(
_asfreq_compat(obj.index[:0], freq=self.freq), append=True
)

if isinstance(result, ABCSeries) and self._selection is not None:
result.name = self._selection

if isinstance(result, ABCSeries) and result.empty:
obj = self.obj
# When index is all NaT, result is empty but index is not
result.index = _asfreq_compat(obj.index[:0], freq=self.freq)
result.name = getattr(obj, "name", None)
Expand Down
22 changes: 21 additions & 1 deletion pandas/tests/resample/test_resampler_grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,11 @@ def test_empty(keys):
# GH 26411
df = DataFrame([], columns=["a", "b"], index=TimedeltaIndex([]))
result = df.groupby(keys).resample(rule=pd.to_timedelta("00:00:01")).mean()
expected = DataFrame(columns=["a", "b"]).set_index(keys, drop=False)
expected = (
DataFrame(columns=["a", "b"])
.set_index(keys, drop=False)
.set_index(TimedeltaIndex([]), append=True)
)
if len(keys) == 1:
expected.index.name = keys[0]

Expand Down Expand Up @@ -497,3 +501,19 @@ def test_groupby_resample_with_list_of_keys():
),
)
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize("keys", [["a"], ["a", "b"]])
def test_resample_empty_Dataframe(keys):
# GH 47705
df = DataFrame([], columns=["a", "b", "date"])
df["date"] = pd.to_datetime(df["date"])
df = df.set_index("date")
result = df.groupby(keys).resample(rule=pd.to_timedelta("00:00:01")).mean()
expected = DataFrame(columns=["a", "b", "date"]).set_index(keys, drop=False)
expected["date"] = pd.to_datetime(expected["date"])
expected = expected.set_index("date", append=True, drop=True)
if len(keys) == 1:
expected.index.name = keys[0]

tm.assert_frame_equal(result, expected)

0 comments on commit fba6723

Please sign in to comment.