From de3fdd2f3328dbcb782a7c589e5d756244af035f Mon Sep 17 00:00:00 2001 From: Richard Shadrach <45562402+rhshadrach@users.noreply.github.com> Date: Thu, 20 Apr 2023 11:20:41 -0400 Subject: [PATCH 1/3] Backport PR #52615: REGR: DataFrame.resample fails on a frame with no columns --- doc/source/whatsnew/v2.0.1.rst | 1 + pandas/core/resample.py | 2 +- pandas/tests/resample/test_resample_api.py | 23 ++++++++++++- .../tests/resample/test_resampler_grouper.py | 34 ++++++++++++++++++- 4 files changed, 57 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v2.0.1.rst b/doc/source/whatsnew/v2.0.1.rst index 2e8e2345d4c0a..3dc003f1d3065 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 9ab56563135c6..546785fbf6f5c 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -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( diff --git a/pandas/tests/resample/test_resample_api.py b/pandas/tests/resample/test_resample_api.py index b36a6295248cd..91baf36e636d4 100644 --- a/pandas/tests/resample/test_resample_api.py +++ b/pandas/tests/resample/test_resample_api.py @@ -55,7 +55,7 @@ def test_api(): def test_groupby_resample_api(): # GH 12448 # .groupby(...).resample(...) hitting warnings - # when appropriate + # whegit an appropriate df = DataFrame( { "date": date_range(start="2016-01-01", periods=4, freq="W"), @@ -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) diff --git a/pandas/tests/resample/test_resampler_grouper.py b/pandas/tests/resample/test_resampler_grouper.py index fdc09246b479a..7e8efe77b5dbf 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 @@ -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"]) @@ -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( From 4ab4b05504f68b8b2fd2608c4c6eec4a6addbf1d Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Thu, 20 Apr 2023 08:58:43 -0700 Subject: [PATCH 2/3] Update pandas/tests/resample/test_resample_api.py --- pandas/tests/resample/test_resample_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/resample/test_resample_api.py b/pandas/tests/resample/test_resample_api.py index 91baf36e636d4..bf6b4995dcd42 100644 --- a/pandas/tests/resample/test_resample_api.py +++ b/pandas/tests/resample/test_resample_api.py @@ -55,7 +55,7 @@ def test_api(): def test_groupby_resample_api(): # GH 12448 # .groupby(...).resample(...) hitting warnings - # whegit an appropriate + # when an appropriate df = DataFrame( { "date": date_range(start="2016-01-01", periods=4, freq="W"), From 09abdbe7f464d64148227dff2f5db6e469c24445 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Thu, 20 Apr 2023 08:58:59 -0700 Subject: [PATCH 3/3] Update pandas/tests/resample/test_resample_api.py --- pandas/tests/resample/test_resample_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/resample/test_resample_api.py b/pandas/tests/resample/test_resample_api.py index bf6b4995dcd42..ae52cdf7b2f4a 100644 --- a/pandas/tests/resample/test_resample_api.py +++ b/pandas/tests/resample/test_resample_api.py @@ -55,7 +55,7 @@ def test_api(): def test_groupby_resample_api(): # GH 12448 # .groupby(...).resample(...) hitting warnings - # when an appropriate + # when appropriate df = DataFrame( { "date": date_range(start="2016-01-01", periods=4, freq="W"),