Skip to content

Commit fc20f32

Browse files
committed
BUG: __setitem__ with a tuple induces NaN with a tz-aware DatetimeIndex (#16889)
1 parent 8d0c025 commit fc20f32

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

doc/source/whatsnew/v0.21.0.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ Indexing
196196
- Fix :func:`MultiIndex.sort_index` ordering when ``ascending`` argument is a list, but not all levels are specified, or are in a different order (:issue:`16934`).
197197
- Fixes bug where indexing with ``np.inf`` caused an ``OverflowError`` to be raised (:issue:`16957`)
198198
- Bug in reindexing on an empty ``CategoricalIndex`` (:issue:`16770`)
199-
199+
- Fixes ``DataFrame.loc`` for setting with alignment and tz-aware ``DatetimeIndex`` (:issue:`16889`)
200+
200201
I/O
201202
^^^
202203

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 .loc with alignment and tz-aware DatetimeIndex
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)