Skip to content

Bug fix using GroupBy.resample produces inconsistent behavior when calling it over empty df #47705 #47672

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 53 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from 46 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
54c5068
DOC #45443 edited the documentation of where/mask functions
ahmedibrhm Jul 8, 2022
2951fb1
DOC #45443 edited the documentation of where/mask functions
ahmedibrhm Jul 8, 2022
6335204
Merge branch 'main' into main
ahmedibrhm Jul 8, 2022
8afd6a1
Update generic.py
ahmedibrhm Jul 8, 2022
4d43e69
Bug 43767 fix
ahmedibrhm Jul 11, 2022
75fdb73
fixing lines doc
ahmedibrhm Jul 11, 2022
26dc5e8
visual indent
ahmedibrhm Jul 11, 2022
47bcbb3
visual indent
ahmedibrhm Jul 11, 2022
390ff5a
indentation
ahmedibrhm Jul 11, 2022
e8e51c4
grouby.resample bug
ahmedibrhm Jul 13, 2022
f25b760
Merge branch 'main' into issue4
ahmedibrhm Jul 13, 2022
d05f473
groubby.sample
ahmedibrhm Jul 13, 2022
9c1fc4f
syntax
ahmedibrhm Jul 13, 2022
c671764
syntax
ahmedibrhm Jul 13, 2022
cdb642b
syntax
ahmedibrhm Jul 13, 2022
891bc7c
syntax
ahmedibrhm Jul 13, 2022
34dcf22
what's new
ahmedibrhm Jul 13, 2022
2a51ddf
flake8 error
ahmedibrhm Jul 13, 2022
1fe7026
pytest
ahmedibrhm Jul 13, 2022
470dbb9
Merge branch 'main' into issue4
ahmedibrhm Jul 13, 2022
9d48bfc
blank line
ahmedibrhm Jul 13, 2022
2695f58
Merge branch 'issue4' of https://github.com/ahmedibrhm/pandas into is…
ahmedibrhm Jul 13, 2022
a076f44
editting resample
ahmedibrhm Jul 15, 2022
163e287
Merge branch 'main' into issue4
ahmedibrhm Jul 15, 2022
1450abd
editting resample
ahmedibrhm Jul 16, 2022
2cde833
Merge branch 'issue4' of https://github.com/ahmedibrhm/pandas into is…
ahmedibrhm Jul 16, 2022
0a45b1f
syntax
ahmedibrhm Jul 16, 2022
c04db0e
syntax
ahmedibrhm Jul 16, 2022
a47ba76
Merge branch 'main' into issue4
ahmedibrhm Jul 16, 2022
3a9070d
syntax
ahmedibrhm Jul 16, 2022
73cfe7c
Merge branch 'issue4' of https://github.com/ahmedibrhm/pandas into is…
ahmedibrhm Jul 16, 2022
e683177
space
ahmedibrhm Jul 16, 2022
b10a311
space
ahmedibrhm Jul 16, 2022
d3af97b
space
ahmedibrhm Jul 16, 2022
41c44ce
Merge branch 'main' into issue4
ahmedibrhm Jul 16, 2022
23d37ce
inplace
ahmedibrhm Jul 18, 2022
53d6649
Merge branch 'issue4' of https://github.com/ahmedibrhm/pandas into is…
ahmedibrhm Jul 18, 2022
42f141b
spelling
ahmedibrhm Jul 18, 2022
b54483b
test
ahmedibrhm Jul 18, 2022
819c836
Merge branch 'main' into issue4
ahmedibrhm Aug 11, 2022
5e12d91
test resampler
ahmedibrhm Aug 11, 2022
2f462ab
tests
ahmedibrhm Aug 11, 2022
727a1c7
tests
ahmedibrhm Aug 13, 2022
b24b531
Merge branch 'main' into issue4
ahmedibrhm Aug 13, 2022
988f37c
Merge branch 'main' into issue4
ahmedibrhm Aug 14, 2022
abee447
Merge branch 'main' into issue4
ahmedibrhm Aug 15, 2022
0b01594
Update resample.py
ahmedibrhm Aug 15, 2022
25e7cfd
Update resample.py
ahmedibrhm Aug 15, 2022
900933b
Update resample.py
ahmedibrhm Aug 15, 2022
77834b1
Merge branch 'main' into issue4
ahmedibrhm Sep 26, 2022
c178d4d
Update v1.6.0.rst
ahmedibrhm Sep 30, 2022
0f05527
Merge branch 'main' into issue4
ahmedibrhm Sep 30, 2022
308ed31
Merge branch 'main' into issue4
ahmedibrhm Oct 3, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,7 @@ Groupby/resample/rolling
- Bug in :meth:`DataFrame.resample` reduction methods when used with ``on`` would attempt to aggregate the provided column (:issue:`47079`)
- Bug in :meth:`DataFrame.groupby` and :meth:`Series.groupby` would not respect ``dropna=False`` when the input DataFrame/Series had a NaN values in a :class:`MultiIndex` (:issue:`46783`)
- Bug in :meth:`DataFrameGroupBy.resample` raises ``KeyError`` when getting the result from a key list which misses the resample key (:issue:`47362`)
- Bug in :meth:`DataFrameGroupBy.resample` produces inconsistent results when passing empty DataFrame (:issue:`47705`)
-

Reshaping
Expand Down
11 changes: 10 additions & 1 deletion pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,11 +514,20 @@ def _wrap_result(self, result):
"""
Potentially wrap any results.
"""
obj = self.obj
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add # GH 47705 here?

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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to combine this with test_empty above using pytest.mark.parametrize?

# 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)