Skip to content

Commit e44daed

Browse files
authored
REGR: DataFrame.resample fails on a frame with no columns (#52615)
* REGR: DataFrame.resample fails on a frame with no columns * Use intp * revert * check dtype when non-empty * revert * Don't check dtype on Windows * Add comment * Use check_index_type
1 parent 381b189 commit e44daed

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

doc/source/whatsnew/v2.0.1.rst

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ def _wrap_result(self, result):
483483
obj = self.obj
484484
if (
485485
isinstance(result, ABCDataFrame)
486-
and result.empty
486+
and len(result) == 0
487487
and not isinstance(result.index, PeriodIndex)
488488
):
489489
result = result.set_index(

pandas/tests/resample/test_resample_api.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,3 +1007,24 @@ def test_series_axis_param_depr():
10071007
)
10081008
with tm.assert_produces_warning(FutureWarning, match=warning_msg):
10091009
test_series.resample("H", axis=0)
1010+
1011+
1012+
def test_resample_empty():
1013+
# GH#52484
1014+
df = DataFrame(
1015+
index=pd.to_datetime(
1016+
["2018-01-01 00:00:00", "2018-01-01 12:00:00", "2018-01-02 00:00:00"]
1017+
)
1018+
)
1019+
expected = DataFrame(
1020+
index=pd.to_datetime(
1021+
[
1022+
"2018-01-01 00:00:00",
1023+
"2018-01-01 08:00:00",
1024+
"2018-01-01 16:00:00",
1025+
"2018-01-02 00:00:00",
1026+
]
1027+
)
1028+
)
1029+
result = df.resample("8H").mean()
1030+
tm.assert_frame_equal(result, expected)

pandas/tests/resample/test_resampler_grouper.py

Lines changed: 33 additions & 1 deletion
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
@@ -525,7 +526,7 @@ def test_groupby_resample_with_list_of_keys():
525526

526527

527528
@pytest.mark.parametrize("keys", [["a"], ["a", "b"]])
528-
def test_resample_empty_Dataframe(keys):
529+
def test_resample_no_index(keys):
529530
# GH 47705
530531
df = DataFrame([], columns=["a", "b", "date"])
531532
df["date"] = pd.to_datetime(df["date"])
@@ -542,6 +543,37 @@ def test_resample_empty_Dataframe(keys):
542543
tm.assert_frame_equal(result, expected)
543544

544545

546+
def test_resample_no_columns():
547+
# GH#52484
548+
df = DataFrame(
549+
index=Index(
550+
pd.to_datetime(
551+
["2018-01-01 00:00:00", "2018-01-01 12:00:00", "2018-01-02 00:00:00"]
552+
),
553+
name="date",
554+
)
555+
)
556+
result = df.groupby([0, 0, 1]).resample(rule=pd.to_timedelta("06:00:00")).mean()
557+
index = pd.to_datetime(
558+
[
559+
"2018-01-01 00:00:00",
560+
"2018-01-01 06:00:00",
561+
"2018-01-01 12:00:00",
562+
"2018-01-02 00:00:00",
563+
]
564+
)
565+
expected = DataFrame(
566+
index=pd.MultiIndex(
567+
levels=[np.array([0, 1], dtype=np.intp), index],
568+
codes=[[0, 0, 0, 1], [0, 1, 2, 3]],
569+
names=[None, "date"],
570+
)
571+
)
572+
573+
# GH#52710 - Index comes out as 32-bit on 64-bit Windows
574+
tm.assert_frame_equal(result, expected, check_index_type=not is_platform_windows())
575+
576+
545577
def test_groupby_resample_size_all_index_same():
546578
# GH 46826
547579
df = DataFrame(

0 commit comments

Comments
 (0)