diff --git a/pandas/src/generate_code.py b/pandas/src/generate_code.py index c7897e7def4d3..9e1830618f14e 100644 --- a/pandas/src/generate_code.py +++ b/pandas/src/generate_code.py @@ -388,6 +388,10 @@ def pad_inplace_%(name)s(ndarray[%(c_type)s] values, N = len(values) + # GH 2778 + if N == 0: + return + if limit is None: lim = N else: @@ -419,6 +423,10 @@ def pad_2d_inplace_%(name)s(ndarray[%(c_type)s, ndim=2] values, K, N = ( values).shape + # GH 2778 + if N == 0: + return + if limit is None: lim = N else: @@ -451,6 +459,10 @@ def backfill_2d_inplace_%(name)s(ndarray[%(c_type)s, ndim=2] values, K, N = ( values).shape + # GH 2778 + if N == 0: + return + if limit is None: lim = N else: @@ -483,6 +495,10 @@ def backfill_inplace_%(name)s(ndarray[%(c_type)s] values, N = len(values) + # GH 2778 + 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..ade5afffbf78f 100644 --- a/pandas/src/generated.pyx +++ b/pandas/src/generated.pyx @@ -826,6 +826,10 @@ def pad_inplace_float64(ndarray[float64_t] values, N = len(values) + # GH 2778 + if N == 0: + return + if limit is None: lim = N else: @@ -855,6 +859,10 @@ def pad_inplace_object(ndarray[object] values, N = len(values) + # GH 2778 + if N == 0: + return + if limit is None: lim = N else: @@ -884,6 +892,10 @@ def pad_inplace_int32(ndarray[int32_t] values, N = len(values) + # GH 2778 + if N == 0: + return + if limit is None: lim = N else: @@ -913,6 +925,10 @@ def pad_inplace_int64(ndarray[int64_t] values, N = len(values) + # GH 2778 + if N == 0: + return + if limit is None: lim = N else: @@ -942,6 +958,10 @@ def pad_inplace_bool(ndarray[uint8_t] values, N = len(values) + # GH 2778 + if N == 0: + return + if limit is None: lim = N else: @@ -972,6 +992,10 @@ def backfill_inplace_float64(ndarray[float64_t] values, N = len(values) + # GH 2778 + if N == 0: + return + if limit is None: lim = N else: @@ -1000,6 +1024,10 @@ def backfill_inplace_object(ndarray[object] values, N = len(values) + # GH 2778 + if N == 0: + return + if limit is None: lim = N else: @@ -1028,6 +1056,10 @@ def backfill_inplace_int32(ndarray[int32_t] values, N = len(values) + # GH 2778 + if N == 0: + return + if limit is None: lim = N else: @@ -1056,6 +1088,10 @@ def backfill_inplace_int64(ndarray[int64_t] values, N = len(values) + # GH 2778 + if N == 0: + return + if limit is None: lim = N else: @@ -1084,6 +1120,10 @@ def backfill_inplace_bool(ndarray[uint8_t] values, N = len(values) + # GH 2778 + if N == 0: + return + if limit is None: lim = N else: @@ -1113,6 +1153,10 @@ def pad_2d_inplace_float64(ndarray[float64_t, ndim=2] values, K, N = ( values).shape + # GH 2778 + if N == 0: + return + if limit is None: lim = N else: @@ -1143,6 +1187,10 @@ def pad_2d_inplace_object(ndarray[object, ndim=2] values, K, N = ( values).shape + # GH 2778 + if N == 0: + return + if limit is None: lim = N else: @@ -1173,6 +1221,10 @@ def pad_2d_inplace_int32(ndarray[int32_t, ndim=2] values, K, N = ( values).shape + # GH 2778 + if N == 0: + return + if limit is None: lim = N else: @@ -1203,6 +1255,10 @@ def pad_2d_inplace_int64(ndarray[int64_t, ndim=2] values, K, N = ( values).shape + # GH 2778 + if N == 0: + return + if limit is None: lim = N else: @@ -1233,6 +1289,10 @@ def pad_2d_inplace_bool(ndarray[uint8_t, ndim=2] values, K, N = ( values).shape + # GH 2778 + if N == 0: + return + if limit is None: lim = N else: @@ -1264,6 +1324,10 @@ def backfill_2d_inplace_float64(ndarray[float64_t, ndim=2] values, K, N = ( values).shape + # GH 2778 + if N == 0: + return + if limit is None: lim = N else: @@ -1294,6 +1358,10 @@ def backfill_2d_inplace_object(ndarray[object, ndim=2] values, K, N = ( values).shape + # GH 2778 + if N == 0: + return + if limit is None: lim = N else: @@ -1324,6 +1392,10 @@ def backfill_2d_inplace_int32(ndarray[int32_t, ndim=2] values, K, N = ( values).shape + # GH 2778 + if N == 0: + return + if limit is None: lim = N else: @@ -1354,6 +1426,10 @@ def backfill_2d_inplace_int64(ndarray[int64_t, ndim=2] values, K, N = ( values).shape + # GH 2778 + if N == 0: + return + if limit is None: lim = N else: @@ -1384,6 +1460,10 @@ def backfill_2d_inplace_bool(ndarray[uint8_t, ndim=2] values, K, N = ( values).shape + # GH 2778 + if N == 0: + return + if limit is None: lim = N else: 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