Skip to content

Commit

Permalink
BUG: .loc assignment of datetime with tz is coercing to naive pandas-…
Browse files Browse the repository at this point in the history
  • Loading branch information
taeold committed Oct 19, 2015
1 parent 5d953e3 commit 4b8149a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.17.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
13 changes: 7 additions & 6 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']:
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4b8149a

Please sign in to comment.