Skip to content

series.replace with empty dictlike raises ValueError: not enough values to unpack #15289

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

Closed
ExpHP opened this issue Feb 2, 2017 · 2 comments
Closed
Labels
Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Milestone

Comments

@ExpHP
Copy link
Contributor

ExpHP commented Feb 2, 2017

Problem description

DataFrame.replace and Series.replace fail unceremoniously on an empty Series or dict argument; I would expect it to simply do nothing.

While there is a fair bit of type-based wizardry going on in this function, I don't suppose there is any actual ambiguity in how this edge case should behave?

Code Sample, a copy-pastable example if possible

from pandas import Series
s = Series([0,0,1,1,2,2])
print(s.replace(Series([7,7])).tolist())
print(s.replace(Series([7])).tolist())
print(s.replace(Series([])).tolist())

Output

[7, 7, 7, 7, 2, 2]
[7, 7, 1, 1, 2, 2]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-82-d889d2100216> in <module>()
      3 print(s.replace(Series([7,7])).tolist())
      4 print(s.replace(Series([7])).tolist())
----> 5 print(s.replace(Series([])).tolist())

/usr/local/lib/python3.5/dist-packages/pandas-0.19.2-py3.5-linux-x86_64.egg/pandas/core/generic.py in replace(self, to_replace, value, inplace, limit, regex, method, axis)
   3438 
   3439             items = list(compat.iteritems(to_replace))
-> 3440             keys, values = zip(*items)
   3441 
   3442             are_mappings = [is_dict_like(v) for v in values]

ValueError: not enough values to unpack (expected 2, got 0)

Expected Output

[7, 7, 7, 7, 2, 2]
[7, 7, 1, 1, 2, 2]
[0, 0, 1, 1, 2, 2]

Output of pd.show_versions()

INSTALLED VERSIONS ------------------ commit: None python: 3.5.2.final.0 python-bits: 64 OS: Linux OS-release: 4.4.0-59-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: en_US.UTF-8

pandas: 0.19.2
nose: None
pip: 8.1.2
setuptools: 27.2.0
Cython: 0.24.1
numpy: 1.11.1
scipy: 0.18.0
statsmodels: None
xarray: None
IPython: 5.1.0
sphinx: None
patsy: None
dateutil: 2.5.3
pytz: 2016.6.1
blosc: None
bottleneck: 1.2.0
tables: None
numexpr: 2.6.2
matplotlib: 1.5.1
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: 0.7.3
lxml: None
bs4: 4.4.1
html5lib: 0.999
httplib2: 0.9.1
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.8
boto: None
pandas_datareader: None

@jreback
Copy link
Contributor

jreback commented Feb 2, 2017

yep, this look like a bug. pull-requests to fix welcome!

@jreback jreback added Difficulty Novice Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Reshaping Concat, Merge/Join, Stack/Unstack, Explode labels Feb 2, 2017
@jreback jreback added this to the 0.20.0 milestone Feb 2, 2017
@ExpHP
Copy link
Contributor Author

ExpHP commented Feb 2, 2017

I am on it

@ExpHP ExpHP changed the title series.replace(Series([])) raises ValueError: not enough values to unpack series.replace with empty dictlike raises ValueError: not enough values to unpack Feb 2, 2017
ExpHP added a commit to exphp-forks/pandas that referenced this issue Feb 2, 2017
ExpHP added a commit to exphp-forks/pandas that referenced this issue Feb 2, 2017
ExpHP added a commit to exphp-forks/pandas that referenced this issue Feb 2, 2017
ExpHP added a commit to exphp-forks/pandas that referenced this issue Feb 2, 2017
@jreback jreback closed this as completed in f6cfaab Feb 2, 2017
AnkurDedania pushed a commit to AnkurDedania/pandas that referenced this issue Mar 21, 2017
closes pandas-dev#15289

Author: Michael Lamparski <[email protected]>

Closes pandas-dev#15294 from ExpHP/bugfix-15289 and squashes the following commits:

f349e0a [Michael Lamparski] BUG: Support empty dict-likes in replace()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants