Skip to content

Commit 4ca076a

Browse files
committed
Merge pull request pandas-dev#6152 from jreback/loc
BUG: fix loc setitem with a dataframe on rhs, multiple items, and a datetimelike
2 parents 6d889d4 + 49501e2 commit 4ca076a

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

doc/source/release.rst

+2
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ Bug Fixes
165165
- Bug in ``DataFrame.apply`` when using mixed datelike reductions (:issue:`6125`)
166166
- Bug in ``DataFrame.append`` when appending a row with different columns (:issue:`6129`)
167167
- Bug in DataFrame construction with recarray and non-ns datetime dtype (:issue:`6140`)
168+
- Bug in ``.loc`` setitem indexing with a datafrme on rhs, multiple item setting, and
169+
a datetimelike (:issue:`6152`)
168170

169171
pandas 0.13.0
170172
-------------

pandas/core/common.py

+4
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,10 @@ def notnull(obj):
275275
return not res
276276
return -res
277277

278+
def _is_null_datelike_scalar(other):
279+
""" test whether the object is a null datelike, e.g. Nat
280+
but guard against passing a non-scalar """
281+
return (np.isscalar(other) and (isnull(other) or other == tslib.iNaT)) or other is pd.NaT or other is None
278282

279283
def array_equivalent(left, right):
280284
"""

pandas/core/internals.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from pandas.core.common import (_possibly_downcast_to_dtype, isnull, notnull,
1111
_NS_DTYPE, _TD_DTYPE, ABCSeries, is_list_like,
1212
ABCSparseSeries, _infer_dtype_from_scalar,
13-
_values_from_object)
13+
_values_from_object, _is_null_datelike_scalar)
1414
from pandas.core.index import (Index, MultiIndex, _ensure_index,
1515
_handle_legacy_indexes)
1616
from pandas.core.indexing import (_check_slice_bounds, _maybe_convert_indices,
@@ -1275,7 +1275,7 @@ def masker(v):
12751275

12761276
values = masker(values)
12771277

1278-
if isnull(other) or (np.isscalar(other) and other == tslib.iNaT):
1278+
if _is_null_datelike_scalar(other):
12791279
other = np.nan
12801280
elif isinstance(other, np.timedelta64):
12811281
other = _coerce_scalar_to_timedelta_type(other, unit='s').item()
@@ -1586,7 +1586,7 @@ def _try_coerce_args(self, values, other):
15861586
we are going to compare vs i8, so coerce to integer
15871587
values is always ndarra like, other may not be """
15881588
values = values.view('i8')
1589-
if isnull(other) or (np.isscalar(other) and other == tslib.iNaT):
1589+
if _is_null_datelike_scalar(other):
15901590
other = tslib.iNaT
15911591
elif isinstance(other, datetime):
15921592
other = lib.Timestamp(other).asm8.view('i8')

pandas/tests/test_indexing.py

+21
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,27 @@ def test_loc_setitem_frame(self):
646646
result = df.ix[:,1:]
647647
assert_frame_equal(result, expected)
648648

649+
def test_loc_setitem_frame_multiples(self):
650+
651+
# multiple setting
652+
df = DataFrame({ 'A' : ['foo','bar','baz'],
653+
'B' : range(3) })
654+
df.loc[0:1] = df.loc[1:2]
655+
expected = DataFrame({ 'A' : ['bar','baz','baz'],
656+
'B' : [1,2,2] })
657+
assert_frame_equal(df, expected)
658+
659+
660+
# multiple setting with frame on rhs (with M8)
661+
df = DataFrame({ 'date' : date_range('2000-01-01','2000-01-5'),
662+
'val' : range(5) })
663+
expected = DataFrame({ 'date' : [Timestamp('20000101'),Timestamp('20000102'),Timestamp('20000101'),
664+
Timestamp('20000102'),Timestamp('20000103')],
665+
'val' : [0,1,0,1,2] })
666+
667+
df.loc[2:4] = df.loc[0:2]
668+
assert_frame_equal(df, expected)
669+
649670
def test_iloc_getitem_frame(self):
650671
df = DataFrame(np.random.randn(10, 4), index=lrange(0, 20, 2), columns=lrange(0,8,2))
651672

0 commit comments

Comments
 (0)