-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
Better error for str.cat with listlike of wrong dtype. #26607
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cd9aa24
Better TypeError for wrong dtype in str.cat
h-vetinari fee9612
Merge remote-tracking branch 'upstream/master' into str_cat_err
h-vetinari fd710de
Review (jreback)
h-vetinari e7f0d7e
Merge remote-tracking branch 'upstream/master' into str_cat_err
h-vetinari bfca6d1
Merge remote-tracking branch 'upstream/master' into str_cat_err
h-vetinari 02f6429
Review (jreback & simonjayhawkins)
h-vetinari cb73704
Merge remote-tracking branch 'upstream/master' into str_cat_err
h-vetinari 3fb1411
Merge remote-tracking branch 'upstream/master' into str_cat_err
h-vetinari 9752aa7
Add typing to cat_core/cat_safe (review jreback)
h-vetinari File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,41 @@ def cat_core(list_of_columns, sep): | |
return np.sum(list_with_sep, axis=0) | ||
|
||
|
||
def cat_safe(list_of_columns, sep): | ||
""" | ||
Auxiliary function for :meth:`str.cat`. | ||
|
||
Same signature as cat_core, but handles TypeErrors in concatenation, which | ||
happen if the arrays in list_of columns have the wrong dtypes or content. | ||
|
||
Parameters | ||
---------- | ||
list_of_columns : list of numpy arrays | ||
List of arrays to be concatenated with sep; | ||
these arrays may not contain NaNs! | ||
sep : string | ||
The separator string for concatenating the columns | ||
|
||
Returns | ||
------- | ||
nd.array | ||
The concatenation of list_of_columns with sep | ||
""" | ||
try: | ||
result = cat_core(list_of_columns, sep) | ||
except TypeError: | ||
# if there are any non-string values (wrong dtype or hidden behind | ||
# object dtype), np.sum will fail; catch and return with better message | ||
for column in list_of_columns: | ||
dtype = lib.infer_dtype(column, skipna=True) | ||
if dtype not in ['string', 'empty']: | ||
raise TypeError( | ||
'Concatenation requires list-likes containing only ' | ||
'strings (or missing values). Offending values found in ' | ||
'column {}'.format(dtype)) from None | ||
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. is it worth having a |
||
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 +2349,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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
What's your expectation for typing the args? Just
List
? It would strictly speaking beList[np.array]
, but AFAICT, mypy resp. the typing module doesn't yet support numpy stubs natively.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes that would be fine