diff --git a/doc/source/whatsnew/v0.20.0.txt b/doc/source/whatsnew/v0.20.0.txt index cb9e2496757ef..f9b6cebb26693 100644 --- a/doc/source/whatsnew/v0.20.0.txt +++ b/doc/source/whatsnew/v0.20.0.txt @@ -999,6 +999,7 @@ Conversion - Bug in ``DataFrame.fillna()`` where the argument ``downcast`` was ignored when fillna value was of type ``dict`` (:issue:`15277`) - Bug in ``.asfreq()``, where frequency was not set for empty ``Series`` (:issue:`14320`) - Bug in ``DataFrame`` construction with nulls and datetimes in a list-like (:issue:`15869`) +- Bug in ``DataFrame.fillna()`` with tz-aware datetimes (:issue:`15855`) Indexing ^^^^^^^^ diff --git a/pandas/core/internals.py b/pandas/core/internals.py index 8db801f8e7212..57361886eab8c 100644 --- a/pandas/core/internals.py +++ b/pandas/core/internals.py @@ -2475,7 +2475,7 @@ def _try_coerce_result(self, result): if isinstance(result, np.ndarray): # allow passing of > 1dim if its trivial if result.ndim > 1: - result = result.reshape(len(result)) + result = result.reshape(np.prod(result.shape)) result = self.values._shallow_copy(result) return result diff --git a/pandas/tests/frame/test_missing.py b/pandas/tests/frame/test_missing.py index 93c3ba78a0abf..eacf032bbcc85 100644 --- a/pandas/tests/frame/test_missing.py +++ b/pandas/tests/frame/test_missing.py @@ -257,6 +257,20 @@ def test_fillna(self): result = df.fillna(value={'Date': df['Date2']}) assert_frame_equal(result, expected) + # with timezone + # GH 15855 + df = pd.DataFrame({'A': [pd.Timestamp('2012-11-11 00:00:00+01:00'), + pd.NaT]}) + exp = pd.DataFrame({'A': [pd.Timestamp('2012-11-11 00:00:00+01:00'), + pd.Timestamp('2012-11-11 00:00:00+01:00')]}) + assert_frame_equal(df.fillna(method='pad'), exp) + + df = pd.DataFrame({'A': [pd.NaT, + pd.Timestamp('2012-11-11 00:00:00+01:00')]}) + exp = pd.DataFrame({'A': [pd.Timestamp('2012-11-11 00:00:00+01:00'), + pd.Timestamp('2012-11-11 00:00:00+01:00')]}) + assert_frame_equal(df.fillna(method='bfill'), exp) + def test_fillna_downcast(self): # GH 15277 # infer int64 from float64 diff --git a/pandas/tests/series/test_missing.py b/pandas/tests/series/test_missing.py index 7174283494fe7..ea49abeee21c5 100644 --- a/pandas/tests/series/test_missing.py +++ b/pandas/tests/series/test_missing.py @@ -258,6 +258,18 @@ def test_datetime64_tz_fillna(self): self.assert_series_equal(expected, result) self.assert_series_equal(pd.isnull(s), null_loc) + # with timezone + # GH 15855 + df = pd.Series([pd.Timestamp('2012-11-11 00:00:00+01:00'), pd.NaT]) + exp = pd.Series([pd.Timestamp('2012-11-11 00:00:00+01:00'), + pd.Timestamp('2012-11-11 00:00:00+01:00')]) + assert_series_equal(df.fillna(method='pad'), exp) + + df = pd.Series([pd.NaT, pd.Timestamp('2012-11-11 00:00:00+01:00')]) + exp = pd.Series([pd.Timestamp('2012-11-11 00:00:00+01:00'), + pd.Timestamp('2012-11-11 00:00:00+01:00')]) + assert_series_equal(df.fillna(method='bfill'), exp) + def test_datetime64tz_fillna_round_issue(self): # GH 14872