Skip to content

Commit 78acf94

Browse files
morottirmmancom
andauthored
BUG: DataFrame.resample is changing the index type to MultiIndex when the dataframe is empty (#61174)
* BUG: DataFrame.resample is changing the index type to MultiIndex when the dataframe is empty * reformat --------- Co-authored-by: rmorotti <[email protected]>
1 parent 65780ef commit 78acf94

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,7 @@ Groupby/resample/rolling
772772
- Bug in :meth:`.DataFrameGroupBy.quantile` when ``interpolation="nearest"`` is inconsistent with :meth:`DataFrame.quantile` (:issue:`47942`)
773773
- Bug in :meth:`.Resampler.interpolate` on a :class:`DataFrame` with non-uniform sampling and/or indices not aligning with the resulting resampled index would result in wrong interpolation (:issue:`21351`)
774774
- Bug in :meth:`DataFrame.ewm` and :meth:`Series.ewm` when passed ``times`` and aggregation functions other than mean (:issue:`51695`)
775+
- Bug in :meth:`DataFrame.resample` changing index type to :class:`MultiIndex` when the dataframe is empty and using an upsample method (:issue:`55572`)
775776
- Bug in :meth:`DataFrameGroupBy.agg` that raises ``AttributeError`` when there is dictionary input and duplicated columns, instead of returning a DataFrame with the aggregation of all duplicate columns. (:issue:`55041`)
776777
- Bug in :meth:`DataFrameGroupBy.apply` and :meth:`SeriesGroupBy.apply` for empty data frame with ``group_keys=False`` still creating output index using group keys. (:issue:`60471`)
777778
- Bug in :meth:`DataFrameGroupBy.apply` that was returning a completely empty DataFrame when all return values of ``func`` were ``None`` instead of returning an empty DataFrame with the original columns and dtypes. (:issue:`57775`)

pandas/core/resample.py

+12-11
Original file line numberDiff line numberDiff line change
@@ -507,22 +507,12 @@ def _wrap_result(self, result):
507507
"""
508508
Potentially wrap any results.
509509
"""
510-
# GH 47705
511-
obj = self.obj
512-
if (
513-
isinstance(result, ABCDataFrame)
514-
and len(result) == 0
515-
and not isinstance(result.index, PeriodIndex)
516-
):
517-
result = result.set_index(
518-
_asfreq_compat(obj.index[:0], freq=self.freq), append=True
519-
)
520-
521510
if isinstance(result, ABCSeries) and self._selection is not None:
522511
result.name = self._selection
523512

524513
if isinstance(result, ABCSeries) and result.empty:
525514
# When index is all NaT, result is empty but index is not
515+
obj = self.obj
526516
result.index = _asfreq_compat(obj.index[:0], freq=self.freq)
527517
result.name = getattr(obj, "name", None)
528518

@@ -1756,6 +1746,17 @@ def func(x):
17561746
return x.apply(f, *args, **kwargs)
17571747

17581748
result = self._groupby.apply(func)
1749+
1750+
# GH 47705
1751+
if (
1752+
isinstance(result, ABCDataFrame)
1753+
and len(result) == 0
1754+
and not isinstance(result.index, PeriodIndex)
1755+
):
1756+
result = result.set_index(
1757+
_asfreq_compat(self.obj.index[:0], freq=self.freq), append=True
1758+
)
1759+
17591760
return self._wrap_result(result)
17601761

17611762
_upsample = _apply

pandas/tests/resample/test_base.py

+18
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,24 @@ def test_resample_size_empty_dataframe(freq, index):
438438
tm.assert_series_equal(result, expected)
439439

440440

441+
@pytest.mark.parametrize("index", [DatetimeIndex([]), TimedeltaIndex([])])
442+
@pytest.mark.parametrize("freq", ["D", "h"])
443+
@pytest.mark.parametrize(
444+
"method", ["ffill", "bfill", "nearest", "asfreq", "interpolate", "mean"]
445+
)
446+
def test_resample_apply_empty_dataframe(index, freq, method):
447+
# GH#55572
448+
empty_frame_dti = DataFrame(index=index)
449+
450+
rs = empty_frame_dti.resample(freq)
451+
result = rs.apply(getattr(rs, method))
452+
453+
expected_index = _asfreq_compat(empty_frame_dti.index, freq)
454+
expected = DataFrame([], index=expected_index)
455+
456+
tm.assert_frame_equal(result, expected)
457+
458+
441459
@pytest.mark.parametrize(
442460
"index",
443461
[

0 commit comments

Comments
 (0)