Skip to content

BUG: replace() alters unrelated values #7304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 2, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/source/v0.14.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
14 changes: 6 additions & 8 deletions pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 .+"):
Expand Down
4 changes: 1 addition & 3 deletions pandas/tests/test_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down