Skip to content
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

Improve TypeError message for str.cat #22722

Closed
h-vetinari opened this issue Sep 15, 2018 · 4 comments

Comments

@h-vetinari
Copy link
Contributor

commented Sep 15, 2018

Currently,

s = pd.Series(['a', 'b', 'c'])
s.str.cat([1, 2, 3])

yields

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Axel Obermeier\eclipse-workspace\pddev\pandas\core\strings.py", line 2222, in cat
    res = str_cat(data, others=others, sep=sep, na_rep=na_rep)
  File "C:\Users\Axel Obermeier\eclipse-workspace\pddev\pandas\core\strings.py", line 111, in str_cat
    cats = [sep.join(tup) for tup in tuples]
  File "C:\Users\Axel Obermeier\eclipse-workspace\pddev\pandas\core\strings.py", line 111, in <listcomp>
    cats = [sep.join(tup) for tup in tuples]
TypeError: sequence item 1: expected str instance, int found

IMO, this should be improved to have a better error message, and shallower stack trace.

@WillAyd

This comment has been minimized.

Copy link
Member

commented Sep 15, 2018

What are you suggesting exactly? Error message reflects what you would get from a standard Python operation:

>>> "".join(['foo', 1])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sequence item 1: expected str instance, int found
@h-vetinari

This comment has been minimized.

Copy link
Contributor Author

commented Sep 16, 2018

@WillAyd

That's of course correct, but users are not dealing with str/int instances, but usually with Series/ndarray, etc., and so the mentioned warning gives the right hint, but is not necessarily the best we can do. I opened this issue as it was a side product of cleaning up some internals/tests for #22725.

@jreback jreback added this to the 0.24.0 milestone Oct 5, 2018

@h-vetinari

This comment has been minimized.

Copy link
Contributor Author

commented Oct 5, 2018

@jreback

I'll open another PR for this, but this will not be closed by #22725 (was split off due to review)

@h-vetinari

This comment has been minimized.

Copy link
Contributor Author

commented Oct 15, 2018

@WillAyd The code for this was splitt off of #22725 to focus that PR (I had edited the OP to reflect that) - this issue is still open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.