diff --git a/doc/source/whatsnew/v0.20.0.txt b/doc/source/whatsnew/v0.20.0.txt index e7b2fc5a6505d..54824faf2c8e8 100644 --- a/doc/source/whatsnew/v0.20.0.txt +++ b/doc/source/whatsnew/v0.20.0.txt @@ -300,3 +300,4 @@ Bug Fixes - Bug in ``Series.unique()`` in which unsigned 64-bit integers were causing overflow (:issue:`14721`) - Require at least 0.23 version of cython to avoid problems with character encodings (:issue:`14699`) - Bug in converting object elements of array-like objects to unsigned 64-bit integers (:issue:`4471`) +- Bug in Series.ffill() with mixed dtypes containing tz-aware datetimes. (:issue:`14956`) diff --git a/pandas/src/inference.pyx b/pandas/src/inference.pyx index 97d786bf82b7c..b6b13def193ff 100644 --- a/pandas/src/inference.pyx +++ b/pandas/src/inference.pyx @@ -859,7 +859,7 @@ def maybe_convert_objects(ndarray[object] objects, bint try_float=0, # we try to coerce datetime w/tz but must all have the same tz if seen_datetimetz: - if len(set([ getattr(val, 'tz', None) for val in objects ])) == 1: + if len(set([getattr(val, 'tzinfo', None) for val in objects])) == 1: from pandas import DatetimeIndex return DatetimeIndex(objects) seen_object = 1 diff --git a/pandas/tests/series/test_missing.py b/pandas/tests/series/test_missing.py index ed558275674c7..3c82e4ed82969 100644 --- a/pandas/tests/series/test_missing.py +++ b/pandas/tests/series/test_missing.py @@ -386,6 +386,12 @@ def test_ffill(self): ts[2] = np.NaN assert_series_equal(ts.ffill(), ts.fillna(method='ffill')) + def test_ffill_mixed_dtypes_without_missing_data(self): + # GH14956 + series = pd.Series([datetime(2015, 1, 1, tzinfo=pytz.utc), 1]) + result = series.ffill() + assert_series_equal(series, result) + def test_bfill(self): ts = Series([0., 1., 2., 3., 4.], index=tm.makeDateIndex(5)) ts[2] = np.NaN diff --git a/pandas/tests/types/test_inference.py b/pandas/tests/types/test_inference.py index fb8f3ca0b5b58..43db84a5b8c62 100644 --- a/pandas/tests/types/test_inference.py +++ b/pandas/tests/types/test_inference.py @@ -11,6 +11,7 @@ import re from datetime import datetime, date, timedelta, time import numpy as np +import pytz import pandas as pd from pandas import lib, tslib @@ -275,6 +276,14 @@ def test_maybe_convert_objects_uint64(self): exp = np.array([2**63, -1], dtype=object) tm.assert_numpy_array_equal(lib.maybe_convert_objects(arr), exp) + def test_mixed_dtypes_remain_object_array(self): + # GH14956 + array = np.array([datetime(2015, 1, 1, tzinfo=pytz.utc), 1], + dtype=object) + result = lib.maybe_convert_objects(array) + tm.assert_numpy_array_equal(result, array) + self.assertTrue(result.dtype == object) + class TestTypeInference(tm.TestCase): _multiprocess_can_split_ = True