Skip to content

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

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

Open
2 of 3 tasks
shadchin opened this issue Sep 21, 2020 · 3 comments
Open
2 of 3 tasks
Labels
Bug Regression Functionality that used to work in a prior pandas version replace replace method

Comments

@shadchin
Copy link

  • 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

@shadchin shadchin added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 21, 2020
@shadchin
Copy link
Author

This behavior after PR #31946

@dsaxton
Copy link
Member

dsaxton commented Sep 21, 2020

I think the error message is fairly clear here, an arbitrary object doesn't really fit any of those types.

@dsaxton dsaxton added Usage Question and removed Needs Triage Issue that has not been reviewed by a pandas team member Bug labels Sep 21, 2020
@jorisvandenbossche
Copy link
Member

But still, you should be able to replace general Python objects in an object dtype column? (the example using object() is of course a bit fake, but in can be any custom Python class objects)

@jbrockmendel jbrockmendel added the replace replace method label Sep 21, 2020
@jorisvandenbossche jorisvandenbossche added the Regression Functionality that used to work in a prior pandas version label Nov 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Regression Functionality that used to work in a prior pandas version replace replace method
Projects
None yet
Development

No branches or pull requests

5 participants