From 1fc197c5a494594b0e40e0a34e6f20ddd240cc6f Mon Sep 17 00:00:00 2001 From: Stephen Lin Date: Wed, 30 Jan 2013 13:02:17 -0500 Subject: [PATCH 1/2] BUG: fillna with method segfaults on zero-length input --- pandas/src/generate_code.py | 12 ++++++++ pandas/src/generated.pyx | 60 +++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/pandas/src/generate_code.py b/pandas/src/generate_code.py index c7897e7def4d3..8395e60d1e6aa 100644 --- a/pandas/src/generate_code.py +++ b/pandas/src/generate_code.py @@ -388,6 +388,9 @@ def pad_inplace_%(name)s(ndarray[%(c_type)s] values, N = len(values) + if N == 0: + return + if limit is None: lim = N else: @@ -419,6 +422,9 @@ def pad_2d_inplace_%(name)s(ndarray[%(c_type)s, ndim=2] values, K, N = ( values).shape + if N == 0: + return + if limit is None: lim = N else: @@ -451,6 +457,9 @@ def backfill_2d_inplace_%(name)s(ndarray[%(c_type)s, ndim=2] values, K, N = ( values).shape + if N == 0: + return + if limit is None: lim = N else: @@ -483,6 +492,9 @@ def backfill_inplace_%(name)s(ndarray[%(c_type)s] values, N = len(values) + if N == 0: + return + if limit is None: lim = N else: diff --git a/pandas/src/generated.pyx b/pandas/src/generated.pyx index 5ecd8439a13ec..a736941ca7f47 100644 --- a/pandas/src/generated.pyx +++ b/pandas/src/generated.pyx @@ -826,6 +826,9 @@ def pad_inplace_float64(ndarray[float64_t] values, N = len(values) + if N == 0: + return + if limit is None: lim = N else: @@ -855,6 +858,9 @@ def pad_inplace_object(ndarray[object] values, N = len(values) + if N == 0: + return + if limit is None: lim = N else: @@ -884,6 +890,9 @@ def pad_inplace_int32(ndarray[int32_t] values, N = len(values) + if N == 0: + return + if limit is None: lim = N else: @@ -913,6 +922,9 @@ def pad_inplace_int64(ndarray[int64_t] values, N = len(values) + if N == 0: + return + if limit is None: lim = N else: @@ -942,6 +954,9 @@ def pad_inplace_bool(ndarray[uint8_t] values, N = len(values) + if N == 0: + return + if limit is None: lim = N else: @@ -972,6 +987,9 @@ def backfill_inplace_float64(ndarray[float64_t] values, N = len(values) + if N == 0: + return + if limit is None: lim = N else: @@ -1000,6 +1018,9 @@ def backfill_inplace_object(ndarray[object] values, N = len(values) + if N == 0: + return + if limit is None: lim = N else: @@ -1028,6 +1049,9 @@ def backfill_inplace_int32(ndarray[int32_t] values, N = len(values) + if N == 0: + return + if limit is None: lim = N else: @@ -1056,6 +1080,9 @@ def backfill_inplace_int64(ndarray[int64_t] values, N = len(values) + if N == 0: + return + if limit is None: lim = N else: @@ -1084,6 +1111,9 @@ def backfill_inplace_bool(ndarray[uint8_t] values, N = len(values) + if N == 0: + return + if limit is None: lim = N else: @@ -1113,6 +1143,9 @@ def pad_2d_inplace_float64(ndarray[float64_t, ndim=2] values, K, N = ( values).shape + if N == 0: + return + if limit is None: lim = N else: @@ -1143,6 +1176,9 @@ def pad_2d_inplace_object(ndarray[object, ndim=2] values, K, N = ( values).shape + if N == 0: + return + if limit is None: lim = N else: @@ -1173,6 +1209,9 @@ def pad_2d_inplace_int32(ndarray[int32_t, ndim=2] values, K, N = ( values).shape + if N == 0: + return + if limit is None: lim = N else: @@ -1203,6 +1242,9 @@ def pad_2d_inplace_int64(ndarray[int64_t, ndim=2] values, K, N = ( values).shape + if N == 0: + return + if limit is None: lim = N else: @@ -1233,6 +1275,9 @@ def pad_2d_inplace_bool(ndarray[uint8_t, ndim=2] values, K, N = ( values).shape + if N == 0: + return + if limit is None: lim = N else: @@ -1264,6 +1309,9 @@ def backfill_2d_inplace_float64(ndarray[float64_t, ndim=2] values, K, N = ( values).shape + if N == 0: + return + if limit is None: lim = N else: @@ -1294,6 +1342,9 @@ def backfill_2d_inplace_object(ndarray[object, ndim=2] values, K, N = ( values).shape + if N == 0: + return + if limit is None: lim = N else: @@ -1324,6 +1375,9 @@ def backfill_2d_inplace_int32(ndarray[int32_t, ndim=2] values, K, N = ( values).shape + if N == 0: + return + if limit is None: lim = N else: @@ -1354,6 +1408,9 @@ def backfill_2d_inplace_int64(ndarray[int64_t, ndim=2] values, K, N = ( values).shape + if N == 0: + return + if limit is None: lim = N else: @@ -1384,6 +1441,9 @@ def backfill_2d_inplace_bool(ndarray[uint8_t, ndim=2] values, K, N = ( values).shape + if N == 0: + return + if limit is None: lim = N else: From 4abf98a957af24f9f04bbbb3181ce561893db303 Mon Sep 17 00:00:00 2001 From: Stephen Lin Date: Thu, 31 Jan 2013 15:16:28 -0500 Subject: [PATCH 2/2] TST: add test for empty object Series fillna --- pandas/src/generate_code.py | 4 ++++ pandas/src/generated.pyx | 20 ++++++++++++++++++++ pandas/tests/test_frame.py | 6 ++++++ 3 files changed, 30 insertions(+) diff --git a/pandas/src/generate_code.py b/pandas/src/generate_code.py index 8395e60d1e6aa..9e1830618f14e 100644 --- a/pandas/src/generate_code.py +++ b/pandas/src/generate_code.py @@ -388,6 +388,7 @@ def pad_inplace_%(name)s(ndarray[%(c_type)s] values, N = len(values) + # GH 2778 if N == 0: return @@ -422,6 +423,7 @@ def pad_2d_inplace_%(name)s(ndarray[%(c_type)s, ndim=2] values, K, N = ( values).shape + # GH 2778 if N == 0: return @@ -457,6 +459,7 @@ def backfill_2d_inplace_%(name)s(ndarray[%(c_type)s, ndim=2] values, K, N = ( values).shape + # GH 2778 if N == 0: return @@ -492,6 +495,7 @@ def backfill_inplace_%(name)s(ndarray[%(c_type)s] values, N = len(values) + # GH 2778 if N == 0: return diff --git a/pandas/src/generated.pyx b/pandas/src/generated.pyx index a736941ca7f47..ade5afffbf78f 100644 --- a/pandas/src/generated.pyx +++ b/pandas/src/generated.pyx @@ -826,6 +826,7 @@ def pad_inplace_float64(ndarray[float64_t] values, N = len(values) + # GH 2778 if N == 0: return @@ -858,6 +859,7 @@ def pad_inplace_object(ndarray[object] values, N = len(values) + # GH 2778 if N == 0: return @@ -890,6 +892,7 @@ def pad_inplace_int32(ndarray[int32_t] values, N = len(values) + # GH 2778 if N == 0: return @@ -922,6 +925,7 @@ def pad_inplace_int64(ndarray[int64_t] values, N = len(values) + # GH 2778 if N == 0: return @@ -954,6 +958,7 @@ def pad_inplace_bool(ndarray[uint8_t] values, N = len(values) + # GH 2778 if N == 0: return @@ -987,6 +992,7 @@ def backfill_inplace_float64(ndarray[float64_t] values, N = len(values) + # GH 2778 if N == 0: return @@ -1018,6 +1024,7 @@ def backfill_inplace_object(ndarray[object] values, N = len(values) + # GH 2778 if N == 0: return @@ -1049,6 +1056,7 @@ def backfill_inplace_int32(ndarray[int32_t] values, N = len(values) + # GH 2778 if N == 0: return @@ -1080,6 +1088,7 @@ def backfill_inplace_int64(ndarray[int64_t] values, N = len(values) + # GH 2778 if N == 0: return @@ -1111,6 +1120,7 @@ def backfill_inplace_bool(ndarray[uint8_t] values, N = len(values) + # GH 2778 if N == 0: return @@ -1143,6 +1153,7 @@ def pad_2d_inplace_float64(ndarray[float64_t, ndim=2] values, K, N = ( values).shape + # GH 2778 if N == 0: return @@ -1176,6 +1187,7 @@ def pad_2d_inplace_object(ndarray[object, ndim=2] values, K, N = ( values).shape + # GH 2778 if N == 0: return @@ -1209,6 +1221,7 @@ def pad_2d_inplace_int32(ndarray[int32_t, ndim=2] values, K, N = ( values).shape + # GH 2778 if N == 0: return @@ -1242,6 +1255,7 @@ def pad_2d_inplace_int64(ndarray[int64_t, ndim=2] values, K, N = ( values).shape + # GH 2778 if N == 0: return @@ -1275,6 +1289,7 @@ def pad_2d_inplace_bool(ndarray[uint8_t, ndim=2] values, K, N = ( values).shape + # GH 2778 if N == 0: return @@ -1309,6 +1324,7 @@ def backfill_2d_inplace_float64(ndarray[float64_t, ndim=2] values, K, N = ( values).shape + # GH 2778 if N == 0: return @@ -1342,6 +1358,7 @@ def backfill_2d_inplace_object(ndarray[object, ndim=2] values, K, N = ( values).shape + # GH 2778 if N == 0: return @@ -1375,6 +1392,7 @@ def backfill_2d_inplace_int32(ndarray[int32_t, ndim=2] values, K, N = ( values).shape + # GH 2778 if N == 0: return @@ -1408,6 +1426,7 @@ def backfill_2d_inplace_int64(ndarray[int64_t, ndim=2] values, K, N = ( values).shape + # GH 2778 if N == 0: return @@ -1441,6 +1460,7 @@ def backfill_2d_inplace_bool(ndarray[uint8_t, ndim=2] values, K, N = ( values).shape + # GH 2778 if N == 0: return diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index 09747ba3f09f0..039643c6889e2 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -4670,6 +4670,12 @@ def test_fillna(self): self.assertRaises(ValueError, self.tsframe.fillna) self.assertRaises(ValueError, self.tsframe.fillna, 5, method='ffill') + # empty frame (GH #2778) + df = DataFrame(columns=['x']) + for m in ['pad','backfill']: + df.x.fillna(method=m,inplace=1) + df.x.fillna(method=m) + def test_ffill(self): self.tsframe['A'][:5] = nan self.tsframe['A'][-5:] = nan