Skip to content

BUG: DataFrameGroupby std/sem modify grouped column when as_index=False #33630

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 2 commits into from
May 19, 2020
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: 1 addition & 1 deletion doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ Groupby/resample/rolling
- Bug in :meth:`Rolling.min` and :meth:`Rolling.max`: Growing memory usage after multiple calls when using a fixed window (:issue:`30726`)
- Bug in :meth:`GroupBy.agg`, :meth:`GroupBy.transform`, and :meth:`GroupBy.resample` where subclasses are not preserved (:issue:`28330`)
- Bug in :meth:`GroupBy.rolling.apply` ignores args and kwargs parameters (:issue:`33433`)

- Bug in :meth:`DataFrameGroupby.std` and :meth:`DataFrameGroupby.sem` would modify grouped-by columns when ``as_index=False`` (:issue:`10355`)

Reshaping
^^^^^^^^^
Expand Down
30 changes: 26 additions & 4 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,11 +649,11 @@ def _set_group_selection(self):
):
return

ax = self.obj._info_axis
groupers = [g.name for g in grp.groupings if g.level is None and g.in_axis]

if len(groupers):
# GH12839 clear selected obj cache when group selection changes
ax = self.obj._info_axis
self._group_selection = ax.difference(Index(groupers), sort=False).tolist()
self._reset_cache("_selected_obj")

Expand Down Expand Up @@ -1368,8 +1368,18 @@ def std(self, ddof: int = 1):
Series or DataFrame
Standard deviation of values within each group.
"""
# TODO: implement at Cython level?
return np.sqrt(self.var(ddof=ddof))
result = self.var(ddof=ddof)
if result.ndim == 1:
result = np.sqrt(result)
else:
cols = result.columns.get_indexer_for(
result.columns.difference(self.exclusions).unique()
)
# TODO(GH-22046) - setting with iloc broken if labels are not unique
# .values to remove labels
result.iloc[:, cols] = np.sqrt(result.iloc[:, cols]).values

return result

@Substitution(name="groupby")
@Appender(_common_see_also)
Expand Down Expand Up @@ -1416,7 +1426,19 @@ def sem(self, ddof: int = 1):
Series or DataFrame
Standard error of the mean of values within each group.
"""
return self.std(ddof=ddof) / np.sqrt(self.count())
result = self.std(ddof=ddof)
if result.ndim == 1:
result /= np.sqrt(self.count())
else:
cols = result.columns.get_indexer_for(
result.columns.difference(self.exclusions).unique()
)
# TODO(GH-22046) - setting with iloc broken if labels are not unique
# .values to remove labels
result.iloc[:, cols] = (
result.iloc[:, cols].values / np.sqrt(self.count().iloc[:, cols]).values
)
return result

@Substitution(name="groupby")
@Appender(_common_see_also)
Expand Down
22 changes: 22 additions & 0 deletions pandas/tests/groupby/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,28 @@ def test_ops_general(op, targop):
tm.assert_frame_equal(result, expected)


def test_ops_not_as_index(reduction_func):
# GH 10355
# Using as_index=False should not modify grouped column

if reduction_func in ("nth", "ngroup", "size",):
pytest.skip("Skip until behavior is determined (GH #5755)")

if reduction_func in ("corrwith", "idxmax", "idxmin", "mad", "nunique", "skew",):
pytest.xfail(
"_GroupBy._python_apply_general incorrectly modifies grouping columns"
)

df = DataFrame(np.random.randint(0, 5, size=(100, 2)), columns=["a", "b"])
expected = getattr(df.groupby("a"), reduction_func)().reset_index()

result = getattr(df.groupby("a", as_index=False), reduction_func)()
tm.assert_frame_equal(result, expected)

result = getattr(df.groupby("a", as_index=False)["b"], reduction_func)()
tm.assert_frame_equal(result, expected)


def test_max_nan_bug():
raw = """,Date,app,File
-04-23,2013-04-23 00:00:00,,log080001.log
Expand Down