Skip to content

Backport PR #52615 on branch 2.0.x (REGR: DataFrame.resample fails on a frame with no columns) #52804

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
Show file tree
Hide file tree
Changes from all commits
Commits
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/v2.0.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Fixed regressions
~~~~~~~~~~~~~~~~~
- Fixed regression for subclassed Series when constructing from a dictionary (:issue:`52445`)
- Fixed regression in :meth:`DataFrame.pivot` changing :class:`Index` name of input object (:issue:`52629`)
- Fixed regression in :meth:`DataFrame.resample` raising on a DataFrame with no columns (:issue:`52484`)
- Fixed regression in :meth:`DataFrame.sort_values` not resetting index when :class:`DataFrame` is already sorted and ``ignore_index=True`` (:issue:`52553`)
- Fixed regression in :meth:`MultiIndex.isin` raising ``TypeError`` for ``Generator`` (:issue:`52568`)
- Fixed regression in :meth:`Series.describe` showing ``RuntimeWarning`` for extension dtype :class:`Series` with one element (:issue:`52515`)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ def _wrap_result(self, result):
obj = self.obj
if (
isinstance(result, ABCDataFrame)
and result.empty
and len(result) == 0
and not isinstance(result.index, PeriodIndex)
):
result = result.set_index(
Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/resample/test_resample_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -971,3 +971,24 @@ def test_args_kwargs_depr(method, raises):
with tm.assert_produces_warning(FutureWarning, match=warn_msg):
with pytest.raises(TypeError, match=error_msg_type):
func(*args, 1, 2, 3)


def test_resample_empty():
# GH#52484
df = DataFrame(
index=pd.to_datetime(
["2018-01-01 00:00:00", "2018-01-01 12:00:00", "2018-01-02 00:00:00"]
)
)
expected = DataFrame(
index=pd.to_datetime(
[
"2018-01-01 00:00:00",
"2018-01-01 08:00:00",
"2018-01-01 16:00:00",
"2018-01-02 00:00:00",
]
)
)
result = df.resample("8H").mean()
tm.assert_frame_equal(result, expected)
34 changes: 33 additions & 1 deletion pandas/tests/resample/test_resampler_grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import numpy as np
import pytest

from pandas.compat import is_platform_windows
from pandas.util._test_decorators import async_mark

import pandas as pd
Expand Down Expand Up @@ -494,7 +495,7 @@ def test_groupby_resample_with_list_of_keys():


@pytest.mark.parametrize("keys", [["a"], ["a", "b"]])
def test_resample_empty_Dataframe(keys):
def test_resample_no_index(keys):
# GH 47705
df = DataFrame([], columns=["a", "b", "date"])
df["date"] = pd.to_datetime(df["date"])
Expand All @@ -509,6 +510,37 @@ def test_resample_empty_Dataframe(keys):
tm.assert_frame_equal(result, expected)


def test_resample_no_columns():
# GH#52484
df = DataFrame(
index=Index(
pd.to_datetime(
["2018-01-01 00:00:00", "2018-01-01 12:00:00", "2018-01-02 00:00:00"]
),
name="date",
)
)
result = df.groupby([0, 0, 1]).resample(rule=pd.to_timedelta("06:00:00")).mean()
index = pd.to_datetime(
[
"2018-01-01 00:00:00",
"2018-01-01 06:00:00",
"2018-01-01 12:00:00",
"2018-01-02 00:00:00",
]
)
expected = DataFrame(
index=pd.MultiIndex(
levels=[np.array([0, 1], dtype=np.intp), index],
codes=[[0, 0, 0, 1], [0, 1, 2, 3]],
names=[None, "date"],
)
)

# GH#52710 - Index comes out as 32-bit on 64-bit Windows
tm.assert_frame_equal(result, expected, check_index_type=not is_platform_windows())


def test_groupby_resample_size_all_index_same():
# GH 46826
df = DataFrame(
Expand Down