Skip to content

Commit a30d6ee

Browse files
committed
Merge pull request #8644 from jreback/ix_consist
BUG: Bug in ix/loc block splitting on setitem (manifests with integer-like dtypes, eg. datetime64) (GH8607)
2 parents 46c52e2 + b65afd0 commit a30d6ee

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

doc/source/whatsnew/v0.15.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,5 @@ Bug Fixes
5050

5151
- Bug in ``cut``/``qcut`` when using ``Series`` and ``retbins=True`` (:issue:`8589`)
5252
- Bug in numeric index operations of add/sub with Float/Index Index with numpy arrays (:issue:`8608`)
53+
- Bug in ix/loc block splitting on setitem (manifests with integer-like dtypes, e.g. datetime64) (:issue:`8607`)
5354
- Fix ``shape`` attribute for ``MultiIndex`` (:issue:`8609`)

pandas/core/indexing.py

+8
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,14 @@ def can_do_equal_len():
482482
if isinstance(indexer, tuple):
483483
indexer = _maybe_convert_ix(*indexer)
484484

485+
# if we are setting on the info axis ONLY
486+
# set using those methods to avoid block-splitting
487+
# logic here
488+
if len(indexer) > info_axis and com.is_integer(indexer[info_axis]) and all(
489+
_is_null_slice(idx) for i, idx in enumerate(indexer) if i != info_axis):
490+
self.obj[item_labels[indexer[info_axis]]] = value
491+
return
492+
485493
if isinstance(value, ABCSeries):
486494
value = self._align_series(indexer, value)
487495

pandas/tests/test_frame.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,10 @@ def test_fancy_setitem_int_labels(self):
10001000
tmp = df.copy()
10011001
exp = df.copy()
10021002
tmp.ix[:, 2] = 5
1003-
exp.values[:, 2] = 5
1003+
1004+
# tmp correctly sets the dtype
1005+
# so match the exp way
1006+
exp[2] = 5
10041007
assert_frame_equal(tmp, exp)
10051008

10061009
def test_fancy_getitem_int_labels(self):

pandas/tests/test_indexing.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,8 @@ def test_iloc_setitem(self):
605605
expected = Series([0,1,0],index=[4,5,6])
606606
assert_series_equal(s, expected)
607607

608-
def test_loc_setitem(self):
608+
def test_ix_loc_setitem(self):
609+
609610
# GH 5771
610611
# loc with slice and series
611612
s = Series(0,index=[4,5,6])
@@ -627,6 +628,31 @@ def test_loc_setitem(self):
627628
expected = DataFrame({'a' : [0.5,-0.5,-1.5], 'b' : [0,1,2] })
628629
assert_frame_equal(df,expected)
629630

631+
# GH 8607
632+
# ix setitem consistency
633+
df = DataFrame(
634+
{'timestamp':[1413840976, 1413842580, 1413760580],
635+
'delta':[1174, 904, 161],
636+
'elapsed':[7673, 9277, 1470]
637+
})
638+
expected = DataFrame(
639+
{'timestamp':pd.to_datetime([1413840976, 1413842580, 1413760580], unit='s'),
640+
'delta':[1174, 904, 161],
641+
'elapsed':[7673, 9277, 1470]
642+
})
643+
644+
df2 = df.copy()
645+
df2['timestamp'] = pd.to_datetime(df['timestamp'], unit='s')
646+
assert_frame_equal(df2,expected)
647+
648+
df2 = df.copy()
649+
df2.loc[:,'timestamp'] = pd.to_datetime(df['timestamp'], unit='s')
650+
assert_frame_equal(df2,expected)
651+
652+
df2 = df.copy()
653+
df2.ix[:,2] = pd.to_datetime(df['timestamp'], unit='s')
654+
assert_frame_equal(df2,expected)
655+
630656
def test_loc_setitem_multiindex(self):
631657

632658
# GH7190

0 commit comments

Comments
 (0)