Skip to content

Commit

Permalink
Backport PR #52615 on branch 2.0.x (REGR: DataFrame.resample fails on…
Browse files Browse the repository at this point in the history
… a frame with no columns) (#52804)

* Backport PR #52615: REGR: DataFrame.resample fails on a frame with no columns

* Update pandas/tests/resample/test_resample_api.py

* Update pandas/tests/resample/test_resample_api.py

---------

Co-authored-by: Richard Shadrach <45562402+rhshadrach@users.noreply.github.com>
  • Loading branch information
mroeschke and rhshadrach committed Apr 20, 2023
1 parent b49d6de commit 115e8a8
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
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

0 comments on commit 115e8a8

Please sign in to comment.