Skip to content

Commit 07667f3

Browse files
authored
BUG: GroupBy.ohlc with as_index=False (#51413)
* BUG: GroupBy.ohlc with as_index=False * GH ref
1 parent 0fb84ae commit 07667f3

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1370,6 +1370,7 @@ Groupby/resample/rolling
13701370
- Bug in :meth:`.DataFrameGroupBy.describe` produced incorrect results when data had duplicate columns (:issue:`50806`)
13711371
- Bug in :meth:`.DataFrameGroupBy.agg` with ``engine="numba"`` failing to respect ``as_index=False`` (:issue:`51228`)
13721372
- Bug in :meth:`DataFrameGroupBy.agg`, :meth:`SeriesGroupBy.agg`, and :meth:`Resampler.agg` would ignore arguments when passed a list of functions (:issue:`50863`)
1373+
- Bug in :meth:`DataFrameGroupBy.ohlc` ignoring ``as_index=False`` (:issue:`51413`)
13731374
-
13741375

13751376
Reshaping

pandas/core/groupby/groupby.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -2504,10 +2504,13 @@ def ohlc(self) -> DataFrame:
25042504
)
25052505
return self._reindex_output(result)
25062506

2507-
# TODO: 2023-02-05 all tests that get here have self.as_index
2508-
return self._apply_to_column_groupbys(
2507+
result = self._apply_to_column_groupbys(
25092508
lambda x: x.ohlc(), self._obj_with_exclusions
25102509
)
2510+
if not self.as_index:
2511+
result = self._insert_inaxis_grouper(result)
2512+
result.index = default_index(len(result))
2513+
return result
25112514

25122515
@doc(DataFrame.describe)
25132516
def describe(

pandas/tests/groupby/aggregate/test_aggregate.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,8 @@ def test_ohlc_ea_dtypes(any_numeric_ea_dtype):
586586
{"a": [1, 1, 2, 3, 4, 4], "b": [22, 11, pd.NA, 10, 20, pd.NA]},
587587
dtype=any_numeric_ea_dtype,
588588
)
589-
result = df.groupby("a").ohlc()
589+
gb = df.groupby("a")
590+
result = gb.ohlc()
590591
expected = DataFrame(
591592
[[22, 22, 11, 11], [pd.NA] * 4, [10] * 4, [20] * 4],
592593
columns=MultiIndex.from_product([["b"], ["open", "high", "low", "close"]]),
@@ -595,6 +596,11 @@ def test_ohlc_ea_dtypes(any_numeric_ea_dtype):
595596
)
596597
tm.assert_frame_equal(result, expected)
597598

599+
gb2 = df.groupby("a", as_index=False)
600+
result2 = gb2.ohlc()
601+
expected2 = expected.reset_index()
602+
tm.assert_frame_equal(result2, expected2)
603+
598604

599605
@pytest.mark.parametrize("dtype", [np.int64, np.uint64])
600606
@pytest.mark.parametrize("how", ["first", "last", "min", "max", "mean", "median"])

0 commit comments

Comments
 (0)