-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Better error for str.cat with listlike of wrong dtype. #26607
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
Changes from 4 commits
cd9aa24
fee9612
fd710de
e7f0d7e
bfca6d1
02f6429
cb73704
3fb1411
9752aa7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -607,7 +607,7 @@ Strings | |
^^^^^^^ | ||
|
||
- Bug in the ``__name__`` attribute of several methods of :class:`Series.str`, which were set incorrectly (:issue:`23551`) | ||
- | ||
- Improved error message when passing ``Series`` of wrong dtype to :meth:`Series.str.cat` (:issue:`22722`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use |
||
- | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,27 @@ def cat_core(list_of_columns, sep): | |
return np.sum(list_with_sep, axis=0) | ||
|
||
|
||
def cat_safe(list_of_columns, sep): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you type the args & return value & add a Parameters / Returns section There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's your expectation for typing the args? Just There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes that would be fine |
||
""" | ||
Auxiliary function for :meth:`str.cat`. | ||
|
||
Same signature as cat_core, but handles TypeErrors in concatenation, which | ||
happen if the Series in list_of columns have the wrong dtypes or content. | ||
""" | ||
# if there are any non-string values (wrong dtype or hidden behind object | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move the comment to the except |
||
# dtype), np.sum will fail; catch error and return with better message | ||
try: | ||
result = cat_core(list_of_columns, sep) | ||
except TypeError: | ||
dtypes = [lib.infer_dtype(x, skipna=True) for x in list_of_columns] | ||
illegal = [x not in ('string', 'empty') for x in dtypes] | ||
first_offender = [x for x, y in zip(list_of_columns, illegal) if y][0] | ||
simonjayhawkins marked this conversation as resolved.
Show resolved
Hide resolved
|
||
raise TypeError('Concatenation requires list-likes containing only ' | ||
'strings (or missing values). Offending values found ' | ||
'in column {}'.format(first_offender)) from None | ||
return result | ||
|
||
|
||
def _na_map(f, arr, na_result=np.nan, dtype=object): | ||
# should really _check_ for NA | ||
return _map(f, arr, na_mask=True, na_value=na_result, dtype=dtype) | ||
|
@@ -2314,16 +2335,16 @@ def cat(self, others=None, sep=None, na_rep=None, join=None): | |
np.putmask(result, union_mask, np.nan) | ||
|
||
not_masked = ~union_mask | ||
result[not_masked] = cat_core([x[not_masked] for x in all_cols], | ||
result[not_masked] = cat_safe([x[not_masked] for x in all_cols], | ||
sep) | ||
elif na_rep is not None and union_mask.any(): | ||
# fill NaNs with na_rep in case there are actually any NaNs | ||
all_cols = [np.where(nm, na_rep, col) | ||
for nm, col in zip(na_masks, all_cols)] | ||
result = cat_core(all_cols, sep) | ||
result = cat_safe(all_cols, sep) | ||
else: | ||
# no NaNs - can just concatenate | ||
result = cat_core(all_cols, sep) | ||
result = cat_safe(all_cols, sep) | ||
|
||
if isinstance(self._orig, Index): | ||
# add dtype for case that result is all-NA | ||
|
Uh oh!
There was an error while loading. Please reload this page.