Skip to content

Series.replace() should raise an exception if invalid argument is given #18634

@upkarlidder

Description

@upkarlidder

Series.replace() should potentially raise an exception if an invalid argument is given? I am new to pandas and this is more of a question. This is a user error on my part, but possible improvement to replace.

df = pd.DataFrame(
{
    'one': ['1','1 ','10'],
    'two': ['1 ', '20 ', '30 ']
})

# creates
     one  two
0    1    1
1    1    20
2    10    30

# I intentionally added a space in df.one. So df.one.value_counts() results in
1     1
10    1
1     1

# I want to strip the spaces around all values in df.one 
# so that value_counts() only has 1 and 10 with values 2 and 1 respectively. 
# The following does not work.

df.one.replace(lambda x: x.strip(), inplace=True)

Problem description

Series.replace() accepts "str, regex, list, dict, Series, numeric, or None". So I understand why the above does not work. But as @toobaz pointed out on gitter, when a lambda is passed, replace should probably raise an error?

Expected Output

Raises TypeError

Output of pd.show_versions()

[paste the output of pd.show_versions() here below this line]

INSTALLED VERSIONS

commit: None
python: 3.6.1.final.0
python-bits: 64
OS: Darwin
OS-release: 16.7.0
machine: x86_64
processor: i386
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.20.3
pytest: None
pip: 9.0.1
setuptools: 36.3.0
Cython: None
numpy: 1.13.1
scipy: 0.19.1
xarray: None
IPython: 6.1.0
sphinx: 1.6.3
patsy: None
dateutil: 2.6.1
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: 2.6.2
feather: None
matplotlib: 2.0.2
openpyxl: 2.4.8
xlrd: 1.0.0
xlwt: None
xlsxwriter: None
lxml: 3.8.0
bs4: None
html5lib: 0.999999999
sqlalchemy: 1.1.14
pymysql: 0.7.11.None
psycopg2: None
jinja2: 2.8.1
s3fs: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    API DesignStringsString extension data type and string data

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions