Skip to content

Commit 3afd6c7

Browse files
committed
Merge pull request #9197 from mortada/kurt_corner_cases
fixes division by zero error for kurt()
2 parents 05f59dd + f761e3d commit 3afd6c7

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

doc/source/whatsnew/v0.16.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,4 @@ Bug Fixes
141141
- Bug in read_csv when using skiprows on a file with CR line endings with the c engine. (:issue:`9079`)
142142
- isnull now detects NaT in PeriodIndex (:issue:`9129`)
143143
- Bug in groupby ``.nth()`` with a multiple column groupby (:issue:`8979`)
144+
- Fixed division by zero error for ``Series.kurt()`` when all values are equal (:issue:`9197`)

pandas/core/nanops.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -514,17 +514,21 @@ def nankurt(values, axis=None, skipna=True):
514514
C = _zero_out_fperr(C)
515515
D = _zero_out_fperr(D)
516516

517+
if not isinstance(B, np.ndarray):
518+
# if B is a scalar, check these corner cases first before doing division
519+
if count < 4:
520+
return np.nan
521+
if B == 0:
522+
return 0
523+
517524
result = (((count * count - 1.) * D / (B * B) - 3 * ((count - 1.) ** 2)) /
518525
((count - 2.) * (count - 3.)))
526+
519527
if isinstance(result, np.ndarray):
520528
result = np.where(B == 0, 0, result)
521529
result[count < 4] = np.nan
522-
return result
523-
else:
524-
result = 0 if B == 0 else result
525-
if count < 4:
526-
return np.nan
527-
return result
530+
531+
return result
528532

529533

530534
@disallow('M8','m8')

pandas/tests/test_series.py

+24
Original file line numberDiff line numberDiff line change
@@ -2212,6 +2212,18 @@ def test_skew(self):
22122212
alt = lambda x: skew(x, bias=False)
22132213
self._check_stat_op('skew', alt)
22142214

2215+
# test corner cases, skew() returns NaN unless there's at least 3 values
2216+
min_N = 3
2217+
for i in range(1, min_N + 1):
2218+
s = Series(np.ones(i))
2219+
df = DataFrame(np.ones((i, i)))
2220+
if i < min_N:
2221+
self.assertTrue(np.isnan(s.skew()))
2222+
self.assertTrue(np.isnan(df.skew()).all())
2223+
else:
2224+
self.assertEqual(0, s.skew())
2225+
self.assertTrue((df.skew() == 0).all())
2226+
22152227
def test_kurt(self):
22162228
tm._skip_if_no_scipy()
22172229

@@ -2226,6 +2238,18 @@ def test_kurt(self):
22262238
s = Series(np.random.randn(6), index=index)
22272239
self.assertAlmostEqual(s.kurt(), s.kurt(level=0)['bar'])
22282240

2241+
# test corner cases, kurt() returns NaN unless there's at least 4 values
2242+
min_N = 4
2243+
for i in range(1, min_N + 1):
2244+
s = Series(np.ones(i))
2245+
df = DataFrame(np.ones((i, i)))
2246+
if i < min_N:
2247+
self.assertTrue(np.isnan(s.kurt()))
2248+
self.assertTrue(np.isnan(df.kurt()).all())
2249+
else:
2250+
self.assertEqual(0, s.kurt())
2251+
self.assertTrue((df.kurt() == 0).all())
2252+
22292253
def test_argsort(self):
22302254
self._check_accum_op('argsort')
22312255
argsorted = self.ts.argsort()

0 commit comments

Comments
 (0)