Skip to content

BUG: df.replace()  #35771

Closed
Closed
@wjziv

Description

@wjziv
  • 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

df = pd.DataFrame.from_dict(
    {
        'a_original': ['w', 'x', 'y', 'z', 'w', 'x', 'y', 'z'],
        'a_expected': ['w', 'x', 'y', 'z', 'w', 'x', 'y', 'z'],
        'a_result': ['w', 'x', 'y', 'z', 'w', 'x', 'y', 'z'],
        'b_original': ['w', 'x', 'y', 'z', 'z', 'y', 'x', 'w'],
        'b_expected': ['w', 'x', 'y', 'z', 'z', 'y', 'x', 'w'],
        'b_result': ['w', 'x', 'y', 'z', 'z', 'y', 'x', 'w']
    }
)

# Produces expectation.
df.replace(
    {column: {replace: None for replace in ['x', 'z']} for column in ['a_expected', 'b_expected']},
    inplace=True
)

# Does not produce expectation.
df.replace(
    {column: ['x', 'z'] for column in ['a_result', 'b_result']},
    {column: None for column in ['a_result', 'b_result']},
    inplace=True
)

>>
  a_original a_expected a_result b_original b_expected b_result
0          w          w        w          w          w        w
1          x       None        w          x       None        w
2          y          y        y          y          y        y
3          z       None        y          z       None        y
4          w          w        w          z       None        y
5          x       None        w          y          y        y
6          y          y        y          x       None        y
7          z       None        y          w          w        w

Problem description

df.replace() has multiple ways to define which values in which columns ought to be replaced. I expected the two uses of df.replace() above to be equivalent, but the second does not replace the values "x" and "z" in the declared columns with None. Instead, some form of filling/padding is used.

Expected Output

The second instance of df.replace() should be the same as the first; all instances of "x" and "z" should be replaced with None, but there seems to be a default usage of method underneath; this result remains the case, no matter what is entered into method.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.6.9.final.0
python-bits : 64
OS : Linux
OS-release : 4.19.112+
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.0.5
numpy : 1.18.5
pytz : 2018.9
dateutil : 2.8.1
pip : 19.3.1
setuptools : 49.2.0
Cython : 0.29.21
pytest : 3.6.4
hypothesis : None
sphinx : 1.8.5
blosc : None
feather : 0.4.1
xlsxwriter : 1.3.3
lxml.etree : 4.2.6
html5lib : 1.0.1
pymysql : None
psycopg2 : 2.7.6.1 (dt dec pq3 ext lo64)
jinja2 : 2.11.2
IPython : 5.5.0
pandas_datareader: 0.8.1
bs4 : 4.6.3
bottleneck : 1.3.2
fastparquet : None
gcsfs : None
lxml.etree : 4.2.6
matplotlib : 3.2.2
numexpr : 2.7.1
odfpy : None
openpyxl : 2.5.9
pandas_gbq : 0.11.0
pyarrow : 1.0.0
pytables : None
pytest : 3.6.4
pyxlsb : None
s3fs : 0.4.2
scipy : 1.4.1
sqlalchemy : 1.3.18
tables : 3.4.4
tabulate : 0.8.7
xarray : 0.15.1
xlrd : 1.1.0
xlwt : 1.3.0
xlsxwriter : 1.3.3
numba : 0.48.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions