From 4b8149ab09c736fb70e9e9cb9403173465d00910 Mon Sep 17 00:00:00 2001 From: taeold Date: Mon, 19 Oct 2015 14:24:14 -0700 Subject: [PATCH] BUG: .loc assignment of datetime with tz is coercing to naive #11365 --- doc/source/whatsnew/v0.17.1.txt | 1 + pandas/core/common.py | 13 +++++++------ pandas/tests/test_indexing.py | 7 +++++++ 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/doc/source/whatsnew/v0.17.1.txt b/doc/source/whatsnew/v0.17.1.txt index ea2b85d983ade..2aaa6ea89fb6e 100755 --- a/doc/source/whatsnew/v0.17.1.txt +++ b/doc/source/whatsnew/v0.17.1.txt @@ -115,3 +115,4 @@ Bug Fixes - Fixed a bug that prevented the construction of an empty series of dtype ``datetime64[ns, tz]`` (:issue:`11245`). - Bug in ``DataFrame.to_dict()`` produces a ``np.datetime64`` object instead of ``Timestamp`` when only datetime is present in data (:issue:`11327`) +- Bug in .loc assignment of datetime with tz is coercing to naive (:issue:`11365`) diff --git a/pandas/core/common.py b/pandas/core/common.py index c2c50bce04309..873f9e009a880 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -986,14 +986,15 @@ def _infer_fill_value(val): if we are a NaT, return the correct dtyped element to provide proper block construction """ - if not is_list_like(val): val = [val] - val = np.array(val,copy=False) - if is_datetimelike(val): - return np.array('NaT',dtype=val.dtype) - elif is_object_dtype(val.dtype): - dtype = lib.infer_dtype(_ensure_object(val)) + v = np.array(val,copy=False) + if is_datetimelike(v): + if is_datetimetz(val): + return pd.DatetimeIndex(v, dtype=val.dtype) + return np.array('NaT',dtype=v.dtype) + elif is_object_dtype(v.dtype): + dtype = lib.infer_dtype(_ensure_object(v)) if dtype in ['datetime','datetime64']: return np.array('NaT',dtype=_NS_DTYPE) elif dtype in ['timedelta','timedelta64']: diff --git a/pandas/tests/test_indexing.py b/pandas/tests/test_indexing.py index 90f85b3f4576d..00e6fc929a7a1 100644 --- a/pandas/tests/test_indexing.py +++ b/pandas/tests/test_indexing.py @@ -3446,6 +3446,13 @@ def test_loc_setitem_datetime(self): expected = DataFrame({'one' : [100.0,200.0]},index=[dt1,dt2]) assert_frame_equal(df, expected) + def test_loc_setitem_datetimetz(self): + # GH 11365 + idx = pd.date_range('20130101',periods=3,tz='US/Eastern') + df = DataFrame({'A': idx}) + df.loc[[True,False,True],'B'] = idx + self.assert_equal(df['A'].dtype, df['B'].dtype) + def test_series_partial_set(self): # partial set with new index # Regression from GH4825