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