Skip to content

Commit af9e938

Browse files
committed
Merge pull request #7304 from cpcloud/fix-replace-bool-yet-again-7140
BUG: replace() alters unrelated values
2 parents e2fd405 + a56bbf8 commit af9e938

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

doc/source/v0.14.1.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,5 @@ Bug Fixes
7777
sign were being treated as temporaries attempting to be deleted
7878
(:issue:`7300`).
7979
- Bug in ``Float64Index`` which didn't allow duplicates (:issue:`7149`).
80+
- Bug in ``DataFrame.replace()`` where truthy values were being replaced
81+
(:issue:`7140`).

pandas/core/internals.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,17 @@
33
import re
44
import operator
55
from datetime import datetime, timedelta
6-
from collections import defaultdict, deque
6+
from collections import defaultdict
77

88
import numpy as np
99
from pandas.core.base import PandasObject
1010

11-
from pandas.hashtable import Factorizer
12-
from pandas.core.common import (_possibly_downcast_to_dtype, isnull, notnull,
11+
from pandas.core.common import (_possibly_downcast_to_dtype, isnull,
1312
_NS_DTYPE, _TD_DTYPE, ABCSeries, is_list_like,
1413
ABCSparseSeries, _infer_dtype_from_scalar,
1514
_is_null_datelike_scalar,
1615
is_timedelta64_dtype, is_datetime64_dtype,)
17-
from pandas.core.index import Index, Int64Index, MultiIndex, _ensure_index
16+
from pandas.core.index import Index, MultiIndex, _ensure_index
1817
from pandas.core.indexing import (_maybe_convert_indices, _length_of_indexer)
1918
import pandas.core.common as com
2019
from pandas.sparse.array import _maybe_to_sparse, SparseArray
@@ -25,12 +24,10 @@
2524

2625
from pandas.tslib import Timestamp
2726
from pandas import compat
28-
from pandas.compat import (range, lrange, lmap, callable, map, zip, u,
29-
OrderedDict)
27+
from pandas.compat import range, map, zip, u
3028
from pandas.tseries.timedeltas import _coerce_scalar_to_timedelta_type
3129

3230

33-
3431
from pandas.lib import BlockPlacement
3532

3633

@@ -1020,6 +1017,7 @@ def equals(self, other):
10201017
left, right = self.values, other.values
10211018
return ((left == right) | (np.isnan(left) & np.isnan(right))).all()
10221019

1020+
10231021
class FloatBlock(FloatOrComplexBlock):
10241022
__slots__ = ()
10251023
is_float = True
@@ -1212,7 +1210,7 @@ def replace(self, to_replace, value, inplace=False, filter=None,
12121210
regex=False):
12131211
to_replace_values = np.atleast_1d(to_replace)
12141212
if not np.can_cast(to_replace_values, bool):
1215-
to_replace = to_replace_values
1213+
return self
12161214
return super(BoolBlock, self).replace(to_replace, value,
12171215
inplace=inplace, filter=filter,
12181216
regex=regex)

pandas/tests/test_frame.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8351,6 +8351,12 @@ def test_replace_with_dict_with_bool_keys(self):
83518351
with tm.assertRaisesRegexp(TypeError, 'Cannot compare types .+'):
83528352
df.replace({'asdf': 'asdb', True: 'yes'})
83538353

8354+
def test_replace_truthy(self):
8355+
df = DataFrame({'a': [True, True]})
8356+
r = df.replace([np.inf, -np.inf], np.nan)
8357+
e = df
8358+
tm.assert_frame_equal(r, e)
8359+
83548360
def test_replace_int_to_int_chain(self):
83558361
df = DataFrame({'a': lrange(1, 5)})
83568362
with tm.assertRaisesRegexp(ValueError, "Replacement not allowed .+"):

pandas/tests/test_internals.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import numpy as np
55

66
from pandas import Index, MultiIndex, DataFrame, Series
7-
from pandas.compat import OrderedDict
7+
from pandas.compat import OrderedDict, lrange
88
from pandas.sparse.array import SparseArray
99
from pandas.core.internals import *
1010
import pandas.core.internals as internals
@@ -835,8 +835,6 @@ def assert_reindex_indexer_is_ok(mgr, axis, new_labels, indexer,
835835
# reindex_indexer(new_labels, indexer, axis)
836836

837837

838-
839-
840838
class TestBlockPlacement(tm.TestCase):
841839
_multiprocess_can_split_ = True
842840

0 commit comments

Comments
 (0)