Skip to content

Small improvements to str_cat #12000

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
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ def str_cat(arr, others=None, sep=None, na_rep=None):

Examples
--------
When ``na_rep`` is `None` (default behavior), NaN value(s)
in the Series propagate and return value will be NaN.

>>> Series(['a','b',np.nan,'c']).str.cat(sep=' ')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think better to actually ignore nans (as indicated in the issue), rather than just document

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to just treat them as '' ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or as if they weren't present in the original array?

i.e. do a fillna then cat, or dropna then cat?

@jreback

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think .dropna (which doesn't exist on Index) - its pretty easy (there is an issue about it).

so you can emulate so it will just work (for Series or Index). If you already have the values then this would work as well.

si[pd.notnull(si))]

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But then you're changing the array length and what if someone supplies others w/ length equal to the original array (before dropna)...?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fillna way amounts to changing the default value of na_rep to ''.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe in that bad case for others you just throw a ValueError.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually you bring up a good point. doing .fillna(na_rep), THEN processing might just work and do the right thing.

yes if others is then inconsistent you need to raise.

pls add as many test cases as you think are needed to represent all behavior.

nan

>>> Series(['a','b',np.nan,'c']).str.cat(sep=' ', na_rep='?')
'a b ? c'

If ``others`` is specified, corresponding values are
concatenated with the separator. Result will be a Series of strings.

Expand Down Expand Up @@ -110,11 +119,13 @@ def str_cat(arr, others=None, sep=None, na_rep=None):
def _length_check(others):
n = None
for x in others:
if n is None:
n = len(x)
elif len(x) != n:
raise ValueError('All arrays must be same length')

try:
if n is None:
n = len(x)
elif len(x) != n:
raise ValueError('All arrays must be same length')
except TypeError:
raise ValueError("Did you mean to supply a `sep` keyword?")
return n


Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ def test_cat(self):
exp = ['aa', NA, 'bb', 'bd', 'cfoo', NA]
tm.assert_almost_equal(result, exp)



def test_count(self):
values = ['foo', 'foofoo', NA, 'foooofooofommmfoo']

Expand Down Expand Up @@ -2057,6 +2059,17 @@ def test_method_on_bytes(self):
'S2').astype(object))
tm.assert_series_equal(result, expected)

def test_str_cat_raises_intuitive_error(self):
s = Series(['a','b','c','d'])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add the issue number as a comment here

message = "Did you mean to supply a `sep` keyword?"
with tm.assertRaisesRegexp(ValueError, message):
s.str.cat('|')
with tm.assertRaisesRegexp(ValueError, message):
s.str.cat(' ')





if __name__ == '__main__':
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
Expand Down