Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Apr 20, 2023
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 @@ -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(
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 @@ -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)
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 @@ -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"])
Expand All @@ -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()
Copy link
Member Author

@rhshadrach rhshadrach Apr 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some very odd behavior here. When I specify the groupings via a list ([0, 0, 1]), this gets encoded as a 32-bit int in the index of the result only on 64-bit Windows.

https://github.com/pandas-dev/pandas/actions/runs/4713463284/jobs/8359178465?pr=52615#step:5:267

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