Skip to content

Commit fd0dd18

Browse files
Rebwar Ali Omer BajallanRebwar Ali Omer Bajallan
Rebwar Ali Omer Bajallan
authored and
Rebwar Ali Omer Bajallan
committed
ADD: GroupBy kurtosis - fixes pandas-dev#40139
1 parent f4b67b5 commit fd0dd18

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

pandas/core/groupby/groupby.py

+61
Original file line numberDiff line numberDiff line change
@@ -1668,6 +1668,67 @@ def sem(self, ddof: int = 1):
16681668
result.iloc[:, result_ilocs] /= np.sqrt(counts.iloc[:, count_ilocs])
16691669
return result
16701670

1671+
@final
1672+
@Substitution(name="groupby")
1673+
def kurtosis(self, how: str = 'Fisher', numeric_only: bool = True):
1674+
"""
1675+
Compute kurtosis of groups, excluding missing values.
1676+
Kurtosis is defined as the fourth central moment divided
1677+
by the square of the variance.
1678+
1679+
Parameters
1680+
----------
1681+
method: str
1682+
'Fisher' or 'Pearson' kurtosis
1683+
numeric_only: bool
1684+
Only numeric values
1685+
1686+
Returns
1687+
-------
1688+
Series or DataFrame
1689+
kurtosis values within each group.
1690+
1691+
"""
1692+
def kurtosis_(obj: FrameOrSeries, axis: int = 1):
1693+
def kurtosis(x: Series) -> float:
1694+
"""
1695+
Compute the kurtosis of the pandas.Series, excluding missing values.
1696+
1697+
Parameters
1698+
----------
1699+
pandas.Series :
1700+
Series from which the kurtosis is calculated
1701+
1702+
Returns
1703+
-------
1704+
float
1705+
kurtosis
1706+
"""
1707+
1708+
x = x.dropna(inplace=False)
1709+
if not len(x):
1710+
return None
1711+
1712+
if how == 'Fisher':
1713+
return x.kurtosis()
1714+
elif how == 'Pearson':
1715+
return x.kurtosis() - 3
1716+
else:
1717+
raise ValueError(f'{how} is not a valid argument')
1718+
1719+
if isinstance(obj, DataFrame):
1720+
return obj.apply(kurtosis, axis=axis)
1721+
elif isinstance(obj, Series):
1722+
return kurtosis(obj)
1723+
else:
1724+
raise TypeError(type(obj))
1725+
1726+
return self._agg_general(
1727+
numeric_only=numeric_only,
1728+
alias="kurtosis",
1729+
npfunc=kurtosis_,
1730+
)
1731+
16711732
@final
16721733
@Substitution(name="groupby")
16731734
@Appender(_common_see_also)

pandas/core/groupby/ops.py

+1
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ def get_group_levels(self) -> List[Index]:
397397
"first": "group_nth",
398398
"last": "group_last",
399399
"ohlc": "group_ohlc",
400+
"kurtosis": "kurtosis"
400401
},
401402
"transform": {
402403
"cumprod": "group_cumprod",

0 commit comments

Comments
 (0)