@@ -1606,7 +1606,7 @@ def notna(self) -> np.ndarray:
1606
1606
1607
1607
notnull = notna
1608
1608
1609
- def value_counts (self , dropna : bool = True ):
1609
+ def value_counts (self , dropna : bool = True , observed : bool = False ):
1610
1610
"""
1611
1611
Return a Series containing counts of each category.
1612
1612
@@ -1616,6 +1616,9 @@ def value_counts(self, dropna: bool = True):
1616
1616
----------
1617
1617
dropna : bool, default True
1618
1618
Don't include counts of NaN.
1619
+ observed : bool, default False
1620
+ If True, only include counts for observed categories.
1621
+ If False, include counts for all categories.
1619
1622
1620
1623
Returns
1621
1624
-------
@@ -1644,7 +1647,8 @@ def value_counts(self, dropna: bool = True):
1644
1647
ix = coerce_indexer_dtype (ix , self .dtype .categories )
1645
1648
ix = self ._from_backing_data (ix )
1646
1649
1647
- return Series (count , index = CategoricalIndex (ix ), dtype = "int64" )
1650
+ counts = Series (count , index = CategoricalIndex (ix ), dtype = "int64" )
1651
+ return counts [counts != 0 ] if observed else counts
1648
1652
1649
1653
# error: Argument 2 of "_empty" is incompatible with supertype
1650
1654
# "NDArrayBackedExtensionArray"; supertype defines the argument type as
0 commit comments