diff --git a/doc/source/whatsnew/v2.0.1.rst b/doc/source/whatsnew/v2.0.1.rst index 023cb68300433..d1eae1b00bf23 100644 --- a/doc/source/whatsnew/v2.0.1.rst +++ b/doc/source/whatsnew/v2.0.1.rst @@ -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`) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 50978275eb5e5..7a39891f74523 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -483,7 +483,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( diff --git a/pandas/tests/resample/test_resample_api.py b/pandas/tests/resample/test_resample_api.py index b1d4207b04250..4b86a25f9587d 100644 --- a/pandas/tests/resample/test_resample_api.py +++ b/pandas/tests/resample/test_resample_api.py @@ -1007,3 +1007,24 @@ def test_series_axis_param_depr(): ) with tm.assert_produces_warning(FutureWarning, match=warning_msg): test_series.resample("H", axis=0) + + +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) diff --git a/pandas/tests/resample/test_resampler_grouper.py b/pandas/tests/resample/test_resampler_grouper.py index 609d58096abca..f19b315400f69 100644 --- a/pandas/tests/resample/test_resampler_grouper.py +++ b/pandas/tests/resample/test_resampler_grouper.py @@ -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 @@ -525,7 +526,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"]) @@ -542,6 +543,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(