Skip to content

Commit 4182105

Browse files
coroajreback
authored andcommitted
BUG: Setting values on Series using .loc with a TZ-aware DatetimeIndex fails, #12050
1 parent 2450306 commit 4182105

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

doc/source/whatsnew/v0.18.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -536,3 +536,5 @@ of columns didn't match the number of series provided (:issue:`12039`).
536536

537537
- Removed ``millisecond`` property of ``DatetimeIndex``. This would always raise a ``ValueError`` (:issue:`12019`).
538538
- Bug in ``Series`` constructor with read-only data (:issue:`11502`)
539+
540+
- Bug in ``.loc`` setitem indexer preventing the use of a TZ-aware DatetimeIndex (:issue:`12050`)

pandas/core/indexing.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,8 @@ def _convert_to_indexer(self, obj, axis=0, is_setter=False):
11141114
return inds
11151115
else:
11161116
if isinstance(obj, Index):
1117-
objarr = obj.values
1117+
# want Index objects to pass through untouched
1118+
objarr = obj
11181119
else:
11191120
objarr = _asarray_tuplesafe(obj)
11201121

pandas/tests/test_indexing.py

+49
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,55 @@ def f():
970970
df.loc[df.new_col == 'new', 'time'] = v
971971
assert_series_equal(df.loc[df.new_col == 'new', 'time'], v)
972972

973+
def test_indexing_with_datetimeindex_tz(self):
974+
975+
# GH 12050
976+
# indexing on a series with a datetimeindex with tz
977+
index = pd.date_range('2015-01-01', periods=2, tz='utc')
978+
979+
ser = pd.Series(range(2), index=index)
980+
981+
# list-like indexing
982+
983+
for sel in (index, list(index)):
984+
# getitem
985+
assert_series_equal(ser[sel], ser)
986+
987+
# setitem
988+
result = ser.copy()
989+
result[sel] = 1
990+
expected = pd.Series(1, index=index)
991+
assert_series_equal(result, expected)
992+
993+
# .loc getitem
994+
assert_series_equal(ser.loc[sel], ser)
995+
996+
# .loc setitem
997+
result = ser.copy()
998+
result.loc[sel] = 1
999+
expected = pd.Series(1, index=index)
1000+
assert_series_equal(result, expected)
1001+
1002+
# single element indexing
1003+
1004+
# getitem
1005+
self.assertEqual(ser[index[1]], 1)
1006+
1007+
# setitem
1008+
result = ser.copy()
1009+
result[index[1]] = 5
1010+
expected = pd.Series([0, 5], index=index)
1011+
assert_series_equal(result, expected)
1012+
1013+
# .loc getitem
1014+
self.assertEqual(ser.loc[index[1]], 1)
1015+
1016+
# .loc setitem
1017+
result = ser.copy()
1018+
result.loc[index[1]] = 5
1019+
expected = pd.Series([0, 5], index=index)
1020+
assert_series_equal(result, expected)
1021+
9731022
def test_loc_setitem_dups(self):
9741023

9751024
# GH 6541

0 commit comments

Comments
 (0)