Skip to content

DOC: Added note about groupby excluding Decimal columns by default #18953

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 4 commits into from
Nov 8, 2018
Merged
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
27 changes: 27 additions & 0 deletions doc/source/groupby.rst
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,33 @@ Note that ``df.groupby('A').colname.std().`` is more efficient than
is only interesting over one column (here ``colname``), it may be filtered
*before* applying the aggregation function.

.. note::
Any object column, also if it contains numerical values such as ``Decimal``
objects, is considered as a "nuisance" columns. They are excluded from
aggregate functions automatically in groupby.

If you do wish to include decimal or object columns in an aggregation with
other non-nuisance data types, you must do so explicitly.

.. ipython:: python

from decimal import Decimal
df_dec = pd.DataFrame(
{'id': [1, 2, 1, 2],
'int_column': [1, 2, 3, 4],
'dec_column': [Decimal('0.50'), Decimal('0.15'), Decimal('0.25'), Decimal('0.40')]
}
)

# Decimal columns can be sum'd explicitly by themselves...
df_dec.groupby(['id'])[['dec_column']].sum()

# ...but cannot be combined with standard data types or they will be excluded
df_dec.groupby(['id'])[['int_column', 'dec_column']].sum()

# Use .agg function to aggregate over standard and "nuisance" data types at the same time
df_dec.groupby(['id']).agg({'int_column': 'sum', 'dec_column': 'sum'})

.. _groupby.observed:

Handling of (un)observed Categorical values
Expand Down