Skip to content

BUG: .loc assignment of datetime with tz is coercing to naive #11365 #11377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be v? (and not val)

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use assert_frame_equal and construct an expected frame


def test_series_partial_set(self):
# partial set with new index
# Regression from GH4825
Expand Down