@@ -556,7 +556,7 @@ class _GroupBy(PandasObject, SelectionMixin):
556
556
def __init__ (self , obj , keys = None , axis = 0 , level = None ,
557
557
grouper = None , exclusions = None , selection = None , as_index = True ,
558
558
sort = True , group_keys = True , squeeze = False ,
559
- observed = None , ** kwargs ):
559
+ observed = False , ** kwargs ):
560
560
561
561
self ._selection = selection
562
562
@@ -2907,7 +2907,7 @@ class Grouping(object):
2907
2907
"""
2908
2908
2909
2909
def __init__ (self , index , grouper = None , obj = None , name = None , level = None ,
2910
- sort = True , observed = None , in_axis = False ):
2910
+ sort = True , observed = False , in_axis = False ):
2911
2911
2912
2912
self .name = name
2913
2913
self .level = level
@@ -2964,12 +2964,6 @@ def __init__(self, index, grouper=None, obj=None, name=None, level=None,
2964
2964
# a passed Categorical
2965
2965
elif is_categorical_dtype (self .grouper ):
2966
2966
2967
- # observed can be True/False/None
2968
- # we treat None as False. If in the future
2969
- # we need to warn if observed is not passed
2970
- # then we have this option
2971
- # gh-20583
2972
-
2973
2967
self .all_grouper = self .grouper
2974
2968
self .grouper = self .grouper ._codes_for_groupby (
2975
2969
self .sort , observed )
@@ -3088,7 +3082,7 @@ def groups(self):
3088
3082
3089
3083
3090
3084
def _get_grouper (obj , key = None , axis = 0 , level = None , sort = True ,
3091
- observed = None , mutated = False , validate = True ):
3085
+ observed = False , mutated = False , validate = True ):
3092
3086
"""
3093
3087
create and return a BaseGrouper, which is an internal
3094
3088
mapping of how to create the grouper indexers.
@@ -4734,26 +4728,28 @@ def _wrap_agged_blocks(self, items, blocks):
4734
4728
4735
4729
def _reindex_output (self , result ):
4736
4730
"""
4737
- if we have categorical groupers, then we want to make sure that
4731
+ If we have categorical groupers, then we want to make sure that
4738
4732
we have a fully reindex-output to the levels. These may have not
4739
4733
participated in the groupings (e.g. may have all been
4740
- nan groups)
4734
+ nan groups);
4741
4735
4742
4736
This can re-expand the output space
4743
4737
"""
4744
4738
4745
- # TODO(jreback): remove completely
4746
- # when observed parameter is defaulted to True
4747
- # gh-20583
4748
-
4749
- if self .observed :
4750
- return result
4751
-
4739
+ # we need to re-expand the output space to accomodate all values
4740
+ # whether observed or not in the cartesian product of our groupes
4752
4741
groupings = self .grouper .groupings
4753
4742
if groupings is None :
4754
4743
return result
4755
4744
elif len (groupings ) == 1 :
4756
4745
return result
4746
+
4747
+ # if we only care about the observed values
4748
+ # we are done
4749
+ elif self .observed :
4750
+ return result
4751
+
4752
+ # reindexing only applies to a Categorical grouper
4757
4753
elif not any (isinstance (ping .grouper , (Categorical , CategoricalIndex ))
4758
4754
for ping in groupings ):
4759
4755
return result
0 commit comments