From a56bbf879fea73aac58309bd34aeb6191c54938d Mon Sep 17 00:00:00 2001 From: Phillip Cloud Date: Sun, 1 Jun 2014 16:47:43 -0400 Subject: [PATCH] BUG: BoolBlock shouldn't replace truthy values --- doc/source/v0.14.1.txt | 2 ++ pandas/core/internals.py | 14 ++++++-------- pandas/tests/test_frame.py | 6 ++++++ pandas/tests/test_internals.py | 4 +--- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/doc/source/v0.14.1.txt b/doc/source/v0.14.1.txt index b430d2eed5a10..cda800b0f0111 100644 --- a/doc/source/v0.14.1.txt +++ b/doc/source/v0.14.1.txt @@ -77,3 +77,5 @@ Bug Fixes sign were being treated as temporaries attempting to be deleted (:issue:`7300`). - Bug in ``Float64Index`` which didn't allow duplicates (:issue:`7149`). +- Bug in ``DataFrame.replace()`` where truthy values were being replaced + (:issue:`7140`). diff --git a/pandas/core/internals.py b/pandas/core/internals.py index de93330f10271..52db14d43fe05 100644 --- a/pandas/core/internals.py +++ b/pandas/core/internals.py @@ -3,18 +3,17 @@ import re import operator from datetime import datetime, timedelta -from collections import defaultdict, deque +from collections import defaultdict import numpy as np from pandas.core.base import PandasObject -from pandas.hashtable import Factorizer -from pandas.core.common import (_possibly_downcast_to_dtype, isnull, notnull, +from pandas.core.common import (_possibly_downcast_to_dtype, isnull, _NS_DTYPE, _TD_DTYPE, ABCSeries, is_list_like, ABCSparseSeries, _infer_dtype_from_scalar, _is_null_datelike_scalar, is_timedelta64_dtype, is_datetime64_dtype,) -from pandas.core.index import Index, Int64Index, MultiIndex, _ensure_index +from pandas.core.index import Index, MultiIndex, _ensure_index from pandas.core.indexing import (_maybe_convert_indices, _length_of_indexer) import pandas.core.common as com from pandas.sparse.array import _maybe_to_sparse, SparseArray @@ -25,12 +24,10 @@ from pandas.tslib import Timestamp from pandas import compat -from pandas.compat import (range, lrange, lmap, callable, map, zip, u, - OrderedDict) +from pandas.compat import range, map, zip, u from pandas.tseries.timedeltas import _coerce_scalar_to_timedelta_type - from pandas.lib import BlockPlacement @@ -1020,6 +1017,7 @@ def equals(self, other): left, right = self.values, other.values return ((left == right) | (np.isnan(left) & np.isnan(right))).all() + class FloatBlock(FloatOrComplexBlock): __slots__ = () is_float = True @@ -1212,7 +1210,7 @@ def replace(self, to_replace, value, inplace=False, filter=None, regex=False): to_replace_values = np.atleast_1d(to_replace) if not np.can_cast(to_replace_values, bool): - to_replace = to_replace_values + return self return super(BoolBlock, self).replace(to_replace, value, inplace=inplace, filter=filter, regex=regex) diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index 7e87c07911353..170a64aa58482 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -8351,6 +8351,12 @@ def test_replace_with_dict_with_bool_keys(self): with tm.assertRaisesRegexp(TypeError, 'Cannot compare types .+'): df.replace({'asdf': 'asdb', True: 'yes'}) + def test_replace_truthy(self): + df = DataFrame({'a': [True, True]}) + r = df.replace([np.inf, -np.inf], np.nan) + e = df + tm.assert_frame_equal(r, e) + def test_replace_int_to_int_chain(self): df = DataFrame({'a': lrange(1, 5)}) with tm.assertRaisesRegexp(ValueError, "Replacement not allowed .+"): diff --git a/pandas/tests/test_internals.py b/pandas/tests/test_internals.py index a3217e2fe8b04..5962e2447fbc9 100644 --- a/pandas/tests/test_internals.py +++ b/pandas/tests/test_internals.py @@ -4,7 +4,7 @@ import numpy as np from pandas import Index, MultiIndex, DataFrame, Series -from pandas.compat import OrderedDict +from pandas.compat import OrderedDict, lrange from pandas.sparse.array import SparseArray from pandas.core.internals import * import pandas.core.internals as internals @@ -835,8 +835,6 @@ def assert_reindex_indexer_is_ok(mgr, axis, new_labels, indexer, # reindex_indexer(new_labels, indexer, axis) - - class TestBlockPlacement(tm.TestCase): _multiprocess_can_split_ = True