Skip to content

Commit 36a7b63

Browse files
committed
BUG: __setitem__ with a tuple induces NaN with a tz-aware DatetimeIndex (pandas-dev#16889)
1 parent 9d13227 commit 36a7b63

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

doc/source/whatsnew/v0.21.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ Bug Fixes
144144
~~~~~~~~~
145145

146146
- Fixes regression in 0.20, :func:`Series.aggregate` and :func:`DataFrame.aggregate` allow dictionaries as return values again (:issue:`16741`)
147+
- Fixes `__setitem__` with a tuple for dataframes with a tz-aware `DatetimeIndex` (:issue: `16889`)
147148

148149
Conversion
149150
^^^^^^^^^^

pandas/core/indexing.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -760,10 +760,12 @@ def _align_frame(self, indexer, df):
760760
for i, ix in enumerate(indexer):
761761
ax = self.obj.axes[i]
762762
if is_sequence(ix) or isinstance(ix, slice):
763+
if isinstance(ix, np.ndarray):
764+
ix = ix.ravel()
763765
if idx is None:
764-
idx = ax[ix].ravel()
766+
idx = ax[ix]
765767
elif cols is None:
766-
cols = ax[ix].ravel()
768+
cols = ax[ix]
767769
else:
768770
break
769771
else:

pandas/tests/indexing/test_datetime.py

+27
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,33 @@
88

99
class TestDatetimeIndex(object):
1010

11+
def test_setitem_with_datetime_tz(self):
12+
# 16889
13+
# support setitem with datetimeindex with tz
14+
mask = np.array([True, False, True, False])
15+
16+
idx = pd.date_range('20010101', periods=4, tz='UTC')
17+
df = pd.DataFrame({'a': np.arange(4)}, index=idx).astype('float64')
18+
19+
result = df.copy()
20+
result.loc[mask, :] = df.loc[mask, :]
21+
tm.assert_frame_equal(result, df)
22+
23+
result = df.copy()
24+
result.loc[mask] = df.loc[mask]
25+
tm.assert_frame_equal(result, df)
26+
27+
idx = pd.date_range('20010101', periods=4)
28+
df = pd.DataFrame({'a': np.arange(4)}, index=idx).astype('float64')
29+
30+
result = df.copy()
31+
result.loc[mask, :] = df.loc[mask, :]
32+
tm.assert_frame_equal(result, df)
33+
34+
result = df.copy()
35+
result.loc[mask] = df.loc[mask]
36+
tm.assert_frame_equal(result, df)
37+
1138
def test_indexing_with_datetime_tz(self):
1239

1340
# 8260

0 commit comments

Comments
 (0)