Skip to content

Commit e19cd10

Browse files
arw2019Kevin D Smith
authored and
Kevin D Smith
committed
BUG: propagate dropna in pd.Grouper (pandas-dev#36604)
1 parent fa943a3 commit e19cd10

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

pandas/core/groupby/grouper.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ class Grouper:
9999
100100
.. versionadded:: 1.1.0
101101
102+
dropna : bool, default True
103+
If True, and if group keys contain NA values, NA values together with
104+
row/column will be dropped. If False, NA values will also be treated as
105+
the key in groups.
106+
107+
.. versionadded:: 1.2.0
108+
102109
Returns
103110
-------
104111
A specification for a groupby instruction
@@ -820,7 +827,9 @@ def is_in_obj(gpr) -> bool:
820827
groupings.append(Grouping(Index([], dtype="int"), np.array([], dtype=np.intp)))
821828

822829
# create the internals grouper
823-
grouper = ops.BaseGrouper(group_axis, groupings, sort=sort, mutated=mutated)
830+
grouper = ops.BaseGrouper(
831+
group_axis, groupings, sort=sort, mutated=mutated, dropna=dropna
832+
)
824833
return grouper, exclusions, obj
825834

826835

pandas/core/groupby/ops.py

+2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ def __init__(
8787
group_keys: bool = True,
8888
mutated: bool = False,
8989
indexer: Optional[np.ndarray] = None,
90+
dropna: bool = True,
9091
):
9192
assert isinstance(axis, Index), axis
9293

@@ -97,6 +98,7 @@ def __init__(
9798
self.group_keys = group_keys
9899
self.mutated = mutated
99100
self.indexer = indexer
101+
self.dropna = dropna
100102

101103
@property
102104
def groupings(self) -> List["grouper.Grouping"]:

pandas/tests/groupby/test_groupby_dropna.py

+8
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,14 @@ def test_groupby_dropna_series_by(dropna, expected):
162162
tm.assert_series_equal(result, expected)
163163

164164

165+
@pytest.mark.parametrize("dropna", (False, True))
166+
def test_grouper_dropna_propagation(dropna):
167+
# GH 36604
168+
df = pd.DataFrame({"A": [0, 0, 1, None], "B": [1, 2, 3, None]})
169+
gb = df.groupby("A", dropna=dropna)
170+
assert gb.grouper.dropna == dropna
171+
172+
165173
@pytest.mark.parametrize(
166174
"dropna,df_expected,s_expected",
167175
[

0 commit comments

Comments
 (0)