Skip to content

BUG: GH12050 Setting values on Series using .loc with a TZ-aware DatetimeIndex fails #12054

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
wants to merge 1 commit into from
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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.18.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -536,3 +536,5 @@ of columns didn't match the number of series provided (:issue:`12039`).

- Removed ``millisecond`` property of ``DatetimeIndex``. This would always raise a ``ValueError`` (:issue:`12019`).
- Bug in ``Series`` constructor with read-only data (:issue:`11502`)

- Bug in ``.loc`` setitem indexer preventing the use of a TZ-aware DatetimeIndex (:issue:`12050`)
3 changes: 2 additions & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1114,7 +1114,8 @@ def _convert_to_indexer(self, obj, axis=0, is_setter=False):
return inds
else:
if isinstance(obj, Index):
objarr = obj.values
# want Index objects to pass through untouched
objarr = obj
else:
objarr = _asarray_tuplesafe(obj)

Expand Down
49 changes: 49 additions & 0 deletions pandas/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,55 @@ def f():
df.loc[df.new_col == 'new', 'time'] = v
assert_series_equal(df.loc[df.new_col == 'new', 'time'], v)

def test_indexing_with_datetimeindex_tz(self):

# GH 12050
# indexing on a series with a datetimeindex with tz
index = pd.date_range('2015-01-01', periods=2, tz='utc')

ser = pd.Series(range(2), index=index)

# list-like indexing

for sel in (index, list(index)):
# getitem
assert_series_equal(ser[sel], ser)

# setitem
result = ser.copy()
result[sel] = 1
expected = pd.Series(1, index=index)
assert_series_equal(result, expected)

# .loc getitem
assert_series_equal(ser.loc[sel], ser)

# .loc setitem
result = ser.copy()
result.loc[sel] = 1
expected = pd.Series(1, index=index)
assert_series_equal(result, expected)

# single element indexing

# getitem
self.assertEqual(ser[index[1]], 1)

# setitem
result = ser.copy()
result[index[1]] = 5
expected = pd.Series([0, 5], index=index)
assert_series_equal(result, expected)

# .loc getitem
self.assertEqual(ser.loc[index[1]], 1)

# .loc setitem
result = ser.copy()
result.loc[index[1]] = 5
expected = pd.Series([0, 5], index=index)
assert_series_equal(result, expected)

def test_loc_setitem_dups(self):

# GH 6541
Expand Down