Skip to content

Commit 80743cd

Browse files
BUG : update SeriesGroupBy.ohlc() to honor as_index=False (pandas-dev#65141)
1 parent 593c2df commit 80743cd

3 files changed

Lines changed: 23 additions & 0 deletions

File tree

doc/source/whatsnew/v3.1.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ Groupby/resample/rolling
305305
- Bug in :meth:`.GroupBy.any` and :meth:`.GroupBy.all` returning ``NaN`` with ``float64`` dtype for unobserved categorical groups on NumPy ``bool`` data instead of the boolean identity value with ``bool`` dtype (:issue:`65100`)
306306
- Bug in :meth:`.Rolling.skew` and :meth:`.Rolling.kurt` (and their :class:`GroupBy` counterparts) returning ``0.0`` and ``-3.0`` respectively for degenerate windows or groups; these now return ``NaN`` (:issue:`62864`)
307307
- Bug in :meth:`.Rolling.skew` and :meth:`.Rolling.kurt` returning ``NaN`` for low-variance windows (:issue:`62946`)
308+
- Bug in :meth:`.SeriesGroupBy.ohlc` ignoring ``as_index=False`` (:issue:`65140`)
308309
- Bug in :meth:`DataFrame.groupby` with a :class:`Grouper` with ``freq`` raising ``AttributeError`` when all grouping keys are ``NaT`` (:issue:`43486`)
309310
- Bug in :meth:`Series.resample` and :meth:`DataFrame.resample` where same-frequency resampling with monthly, quarterly, or annual frequencies bypassed aggregation, returning the original values instead of the aggregation result (:issue:`18553`)
310311

pandas/core/groupby/groupby.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3423,6 +3423,8 @@ def ohlc(self) -> DataFrame:
34233423
result = self.obj._constructor_expanddim(
34243424
res_values, index=self._grouper.result_index, columns=agg_names
34253425
)
3426+
if not self.as_index:
3427+
result = result.reset_index()
34263428
return result
34273429

34283430
result = self._apply_to_column_groupbys(lambda sgb: sgb.ohlc())

pandas/tests/groupby/test_groupby.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,26 @@ def test_as_index_series_return_frame(df):
594594
tm.assert_frame_equal(result2, expected2)
595595

596596

597+
def test_as_index_series_ohlc(df):
598+
# GH#65140
599+
grouped = df.groupby("A", as_index=False)
600+
grouped2 = df.groupby(["A", "B"], as_index=False)
601+
602+
result = grouped["C"].ohlc()
603+
expected = df.groupby("A")["C"].ohlc().reset_index()
604+
tm.assert_frame_equal(result, expected)
605+
606+
result2 = grouped2["C"].ohlc()
607+
expected2 = df.groupby(["A", "B"])["C"].ohlc().reset_index()
608+
tm.assert_frame_equal(result2, expected2)
609+
610+
df_conflict = DataFrame(
611+
{"open": ["A", "A", "B", "B"], "price": [1.0, 2.0, 3.0, 4.0]}
612+
)
613+
with pytest.raises(ValueError, match="cannot insert open, already exists"):
614+
df_conflict.groupby("open", as_index=False)["price"].ohlc()
615+
616+
597617
def test_as_index_series_column_slice_raises(df):
598618
# GH15072
599619
grouped = df.groupby("A", as_index=False)

0 commit comments

Comments
 (0)