Skip to content

BUG: Resampler.ohlc with empty data #53272

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 3 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -505,9 +505,11 @@ Groupby/resample/rolling
- Bug in :meth:`GroupBy.groups` with a datetime key in conjunction with another key produced incorrect number of group keys (:issue:`51158`)
- Bug in :meth:`GroupBy.quantile` may implicitly sort the result index with ``sort=False`` (:issue:`53009`)
- Bug in :meth:`GroupBy.var` failing to raise ``TypeError`` when called with datetime64, timedelta64 or :class:`PeriodDtype` values (:issue:`52128`, :issue:`53045`)
- Bug in :meth:`Resampler.ohlc` with empty object returning a :class:`Series` instead of empty :class:`DataFrame` (:issue:`42902`)
- Bug in :meth:`SeriesGroupBy.nth` and :meth:`DataFrameGroupBy.nth` after performing column selection when using ``dropna="any"`` or ``dropna="all"`` would not subset columns (:issue:`53518`)
- Bug in :meth:`SeriesGroupBy.nth` and :meth:`DataFrameGroupBy.nth` raised after performing column selection when using ``dropna="any"`` or ``dropna="all"`` resulted in rows being dropped (:issue:`53518`)
- Bug in :meth:`SeriesGroupBy.sum` and :meth:`DataFrameGroupby.sum` summing ``np.inf + np.inf`` and ``(-np.inf) + (-np.inf)`` to ``np.nan`` (:issue:`53606`)
-

Reshaping
^^^^^^^^^
Expand Down
18 changes: 18 additions & 0 deletions pandas/core/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
)
from pandas.core.groupby.grouper import Grouper
from pandas.core.groupby.ops import BinGrouper
from pandas.core.indexes.api import MultiIndex
from pandas.core.indexes.datetimes import (
DatetimeIndex,
date_range,
Expand Down Expand Up @@ -1460,6 +1461,23 @@ def ohlc(
):
maybe_warn_args_and_kwargs(type(self), "ohlc", args, kwargs)
nv.validate_resampler_func("ohlc", args, kwargs)

ax = self.ax
obj = self._obj_with_exclusions
if len(ax) == 0:
# GH#42902
obj = obj.copy()
obj.index = _asfreq_compat(obj.index, self.freq)
if obj.ndim == 1:
obj = obj.to_frame()
obj = obj.reindex(["open", "high", "low", "close"], axis=1)
else:
mi = MultiIndex.from_product(
[obj.columns, ["open", "high", "low", "close"]]
)
obj = obj.reindex(mi, axis=1)
return obj

return self._downsample("ohlc")

@doc(SeriesGroupBy.nunique)
Expand Down
33 changes: 21 additions & 12 deletions pandas/tests/resample/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from pandas import (
DataFrame,
MultiIndex,
NaT,
PeriodIndex,
Series,
Expand Down Expand Up @@ -100,16 +101,9 @@ def test_raises_on_non_datetimelike_index():

@all_ts
@pytest.mark.parametrize("freq", ["M", "D", "H"])
def test_resample_empty_series(freq, empty_series_dti, resample_method, request):
def test_resample_empty_series(freq, empty_series_dti, resample_method):
# GH12771 & GH12868

if resample_method == "ohlc" and isinstance(empty_series_dti.index, PeriodIndex):
request.node.add_marker(
pytest.mark.xfail(
reason=f"GH13083: {resample_method} fails for PeriodIndex"
)
)

ser = empty_series_dti
if freq == "M" and isinstance(ser.index, TimedeltaIndex):
msg = (
Expand All @@ -123,12 +117,19 @@ def test_resample_empty_series(freq, empty_series_dti, resample_method, request)
rs = ser.resample(freq)
result = getattr(rs, resample_method)()

expected = ser.copy()
expected.index = _asfreq_compat(ser.index, freq)
if resample_method == "ohlc":
expected = DataFrame(
[], index=ser.index[:0].copy(), columns=["open", "high", "low", "close"]
)
expected.index = _asfreq_compat(ser.index, freq)
tm.assert_frame_equal(result, expected, check_dtype=False)
else:
expected = ser.copy()
expected.index = _asfreq_compat(ser.index, freq)
tm.assert_series_equal(result, expected, check_dtype=False)

tm.assert_index_equal(result.index, expected.index)
assert result.index.freq == expected.index.freq
tm.assert_series_equal(result, expected, check_dtype=False)


@all_ts
Expand Down Expand Up @@ -203,7 +204,15 @@ def test_resample_empty_dataframe(empty_frame_dti, freq, resample_method):

rs = df.resample(freq, group_keys=False)
result = getattr(rs, resample_method)()
if resample_method != "size":
if resample_method == "ohlc":
# TODO: no tests with len(df.columns) > 0
mi = MultiIndex.from_product([df.columns, ["open", "high", "low", "close"]])
expected = DataFrame(
[], index=df.index[:0].copy(), columns=mi, dtype=np.float64
)
expected.index = _asfreq_compat(df.index, freq)

elif resample_method != "size":
expected = df.copy()
else:
# GH14962
Expand Down