Skip to content

Commit 115e8a8

Browse files
Backport PR #52615 on branch 2.0.x (REGR: DataFrame.resample fails on 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 <[email protected]>
1 parent b49d6de commit 115e8a8

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

doc/source/whatsnew/v2.0.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Fixed regressions
1515
~~~~~~~~~~~~~~~~~
1616
- Fixed regression for subclassed Series when constructing from a dictionary (:issue:`52445`)
1717
- Fixed regression in :meth:`DataFrame.pivot` changing :class:`Index` name of input object (:issue:`52629`)
18+
- Fixed regression in :meth:`DataFrame.resample` raising on a DataFrame with no columns (:issue:`52484`)
1819
- Fixed regression in :meth:`DataFrame.sort_values` not resetting index when :class:`DataFrame` is already sorted and ``ignore_index=True`` (:issue:`52553`)
1920
- Fixed regression in :meth:`MultiIndex.isin` raising ``TypeError`` for ``Generator`` (:issue:`52568`)
2021
- Fixed regression in :meth:`Series.describe` showing ``RuntimeWarning`` for extension dtype :class:`Series` with one element (:issue:`52515`)

pandas/core/resample.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ def _wrap_result(self, result):
467467
obj = self.obj
468468
if (
469469
isinstance(result, ABCDataFrame)
470-
and result.empty
470+
and len(result) == 0
471471
and not isinstance(result.index, PeriodIndex)
472472
):
473473
result = result.set_index(

pandas/tests/resample/test_resample_api.py

+21
Original file line numberDiff line numberDiff line change
@@ -971,3 +971,24 @@ def test_args_kwargs_depr(method, raises):
971971
with tm.assert_produces_warning(FutureWarning, match=warn_msg):
972972
with pytest.raises(TypeError, match=error_msg_type):
973973
func(*args, 1, 2, 3)
974+
975+
976+
def test_resample_empty():
977+
# GH#52484
978+
df = DataFrame(
979+
index=pd.to_datetime(
980+
["2018-01-01 00:00:00", "2018-01-01 12:00:00", "2018-01-02 00:00:00"]
981+
)
982+
)
983+
expected = DataFrame(
984+
index=pd.to_datetime(
985+
[
986+
"2018-01-01 00:00:00",
987+
"2018-01-01 08:00:00",
988+
"2018-01-01 16:00:00",
989+
"2018-01-02 00:00:00",
990+
]
991+
)
992+
)
993+
result = df.resample("8H").mean()
994+
tm.assert_frame_equal(result, expected)

pandas/tests/resample/test_resampler_grouper.py

+33-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import numpy as np
44
import pytest
55

6+
from pandas.compat import is_platform_windows
67
from pandas.util._test_decorators import async_mark
78

89
import pandas as pd
@@ -494,7 +495,7 @@ def test_groupby_resample_with_list_of_keys():
494495

495496

496497
@pytest.mark.parametrize("keys", [["a"], ["a", "b"]])
497-
def test_resample_empty_Dataframe(keys):
498+
def test_resample_no_index(keys):
498499
# GH 47705
499500
df = DataFrame([], columns=["a", "b", "date"])
500501
df["date"] = pd.to_datetime(df["date"])
@@ -509,6 +510,37 @@ def test_resample_empty_Dataframe(keys):
509510
tm.assert_frame_equal(result, expected)
510511

511512

513+
def test_resample_no_columns():
514+
# GH#52484
515+
df = DataFrame(
516+
index=Index(
517+
pd.to_datetime(
518+
["2018-01-01 00:00:00", "2018-01-01 12:00:00", "2018-01-02 00:00:00"]
519+
),
520+
name="date",
521+
)
522+
)
523+
result = df.groupby([0, 0, 1]).resample(rule=pd.to_timedelta("06:00:00")).mean()
524+
index = pd.to_datetime(
525+
[
526+
"2018-01-01 00:00:00",
527+
"2018-01-01 06:00:00",
528+
"2018-01-01 12:00:00",
529+
"2018-01-02 00:00:00",
530+
]
531+
)
532+
expected = DataFrame(
533+
index=pd.MultiIndex(
534+
levels=[np.array([0, 1], dtype=np.intp), index],
535+
codes=[[0, 0, 0, 1], [0, 1, 2, 3]],
536+
names=[None, "date"],
537+
)
538+
)
539+
540+
# GH#52710 - Index comes out as 32-bit on 64-bit Windows
541+
tm.assert_frame_equal(result, expected, check_index_type=not is_platform_windows())
542+
543+
512544
def test_groupby_resample_size_all_index_same():
513545
# GH 46826
514546
df = DataFrame(

0 commit comments

Comments
 (0)