Skip to content

BUG: Different behavior of .replace() method between pandas version 1.0.5 and 1.1.2 #36522

Open
@shadchin

Description

@shadchin
  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

import pandas as pd

o1 = object()
o2 = object()
o3 = object()

df = pd.DataFrame([{"a": o1, "b": o2}, {"a": o2, "b": o1}])
df.replace(to_replace={"a": o1}, value={"a": o3})

Problem description

In pandas 1.0.5

In [1]: import pandas as pd
   ...:
   ...: o1 = object()
   ...: o2 = object()
   ...: o3 = object()
   ...:
   ...: df = pd.DataFrame([{"a": o1, "b": o2}, {"a": o2, "b": o1}])
   ...: df.replace(to_replace={"a": o1}, value={"a": o3})
   ...:
Out[1]:
                                   a                                  b
0  <object object at 0x7fcc03c86a10>  <object object at 0x7fcc03c86a00>
1  <object object at 0x7fcc03c86a00>  <object object at 0x7fcc04b2c040>

In pandas 1.1.2

In [1]: import pandas as pd
   ...:
   ...: o1 = object()
   ...: o2 = object()
   ...: o3 = object()
   ...:
   ...: df = pd.DataFrame([{"a": o1, "b": o2}, {"a": o2, "b": o1}])
   ...: df.replace(to_replace={"a": o1}, value={"a": o3})
   ...:
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
contrib/python/ipython/py3/IPython/__init__.py in <module>
      6
      7 df = pd.DataFrame([{"a": o1, "b": o2}, {"a": o2, "b": o1}])
----> 8 df.replace(to_replace={"a": o1}, value={"a": o3})

contrib/python/pandas/py3/pandas/core/frame.py in replace(self, to_replace, value, inplace, limit, regex, method)
   4376         method="pad",
   4377     ):
-> 4378         return super().replace(
   4379             to_replace=to_replace,
   4380             value=value,

contrib/python/pandas/py3/pandas/core/generic.py in replace(self, to_replace, value, inplace, limit, regex, method)
   6513                         if col in value.keys() and col in self
   6514                     }
-> 6515                     return self._replace_columnwise(mapping, inplace, regex)
   6516
   6517                 # {'A': NA} -> 0

contrib/python/pandas/py3/pandas/core/frame.py in _replace_columnwise(self, mapping, inplace, regex)
   4412
   4413                 target, value = mapping[ax[i]]
-> 4414                 newobj = ser.replace(target, value, regex=regex)
   4415
   4416                 res.iloc[:, i] = newobj

contrib/python/pandas/py3/pandas/core/series.py in replace(self, to_replace, value, inplace, limit, regex, method)
   4561         method="pad",
   4562     ):
-> 4563         return super().replace(
   4564             to_replace=to_replace,
   4565             value=value,

contrib/python/pandas/py3/pandas/core/generic.py in replace(self, to_replace, value, inplace, limit, regex, method)
   6437             or is_list_like(to_replace)
   6438         ):
-> 6439             raise TypeError(
   6440                 "Expecting 'to_replace' to be either a scalar, array-like, "
   6441                 "dict or None, got invalid type "

TypeError: Expecting 'to_replace' to be either a scalar, array-like, dict or None, got invalid type 'object'

Is this expected behavior or bug?

Expected Output

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 2a7d332
python : 3.8.5.final.0
python-bits : 64
OS : Linux
OS-release : 4.4.171-70.1
Version : #1 SMP Thu Jan 31 10:46:27 UTC 2019
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.1.2
numpy : 1.16.6
pytz : 2020.1
dateutil : 2.8.1
pip : None
setuptools : 44.1.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.18.1
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugRegressionFunctionality that used to work in a prior pandas versionreplacereplace method

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions