Skip to content

Commit daf0b13

Browse files
committed
BUG/ENH: reindex boolean Series passed to getitem/setitem, GH #429
1 parent 5e62a26 commit daf0b13

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

RELEASE.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ pandas 0.6.1
3131

3232
- Rename `names` argument in DataFrame.from_records to `columns`. Add
3333
deprecation warning
34+
- Boolean get/set operations on Series with boolean Series will reindex
35+
instead of requiring that the indexes be exactly equal (GH #429)
3436

3537
**New features / modules**
3638

pandas/core/series.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ def _index_with(indexer):
275275
# special handling of boolean data with NAs stored in object
276276
# arrays. Since we can't represent NA with dtype=bool
277277
if _is_bool_indexer(key):
278-
self._check_bool_indexer(key)
278+
key = self._check_bool_indexer(key)
279279
key = np.asarray(key, dtype=bool)
280280
return _index_with(key)
281281

@@ -392,17 +392,12 @@ def __setitem__(self, key, value):
392392
# Could not hash item
393393
pass
394394

395-
self._check_bool_indexer(key)
395+
key = self._check_bool_indexer(key)
396396

397397
# special handling of boolean data with NAs stored in object
398398
# arrays. Sort of an elaborate hack since we can't represent boolean
399399
# NA. Hmm
400400
if isinstance(key, np.ndarray) and key.dtype == np.object_:
401-
mask = isnull(key)
402-
if mask.any():
403-
raise ValueError('cannot index with vector containing '
404-
'NA / NaN values')
405-
406401
if set([True, False]).issubset(set(key)):
407402
key = np.asarray(key, dtype=bool)
408403
values[key] = value
@@ -413,10 +408,18 @@ def __setitem__(self, key, value):
413408
def _check_bool_indexer(self, key):
414409
# boolean indexing, need to check that the data are aligned, otherwise
415410
# disallowed
411+
result = key
416412
if isinstance(key, Series) and key.dtype == np.bool_:
417413
if not key.index.equals(self.index):
418-
raise Exception('can only boolean index with like-indexed '
419-
'Series or raw ndarrays')
414+
result = key.reindex(self.index)
415+
416+
if isinstance(result, np.ndarray) and result.dtype == np.object_:
417+
mask = isnull(result)
418+
if mask.any():
419+
raise ValueError('cannot index with vector containing '
420+
'NA / NaN values')
421+
422+
return result
420423

421424
def __setslice__(self, i, j, value):
422425
"""Set slice equal to given value(s)"""

pandas/tests/test_series.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,23 @@ def test_ix_setitem_corner(self):
477477
self.assertRaises(Exception, self.series.ix.__setitem__,
478478
inds + ['foo'], 5)
479479

480+
def test_get_set_boolean_different_order(self):
481+
ordered = self.series.order()
482+
483+
# setting
484+
copy = self.series.copy()
485+
copy[ordered > 0] = 0
486+
487+
expected = self.series.copy()
488+
expected[expected > 0] = 0
489+
490+
assert_series_equal(copy, expected)
491+
492+
# getting
493+
sel = self.series[ordered > 0]
494+
exp = self.series[self.series > 0]
495+
assert_series_equal(sel, exp)
496+
480497
def test_repr(self):
481498
str(self.ts)
482499
str(self.series)

0 commit comments

Comments
 (0)