Skip to content

Commit fee1c54

Browse files
committed
Improve tests/errors for str_cat; fix is_legal
1 parent a7fd1e6 commit fee1c54

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

pandas/core/strings.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def _length_check(others):
141141
elif len(x) != n:
142142
raise ValueError('All arrays must be same length')
143143
except TypeError:
144-
raise ValueError("Did you mean to supply a `sep` keyword?")
144+
raise ValueError('Must pass arrays containing strings to str_cat')
145145
return n
146146

147147

@@ -2003,7 +2003,6 @@ def _get_series_list(self, others, ignore_index=False):
20032003
# strings/NaN/None. Need to robustify check against
20042004
# x in nxt being list-like (otherwise ambiguous boolean).
20052005
is_legal = all((isinstance(x, compat.string_types)
2006-
or (is_list_like(x) and any(isnull(x)))
20072006
or (not is_list_like(x) and isnull(x))
20082007
or x is None)
20092008
for x in nxt)
@@ -2184,10 +2183,10 @@ def cat(self, others=None, sep=None, na_rep=None, join=None):
21842183
# turn anything in "others" into lists of Series
21852184
tmp = self._get_series_list(others, ignore_index=(join is None))
21862185
others, fut_warn = tmp
2187-
except ValueError: # let TypeError raised by _get_series_list pass
2186+
except ValueError: # do not catch TypeError raised by _get_series_list
21882187
if join is None:
2189-
# legacy warning
2190-
raise ValueError('All arrays must be same length')
2188+
raise ValueError('All arrays must be same length, except '
2189+
'those having an index if `join` is not None')
21912190
else:
21922191
raise ValueError('If `others` contains arrays or lists (or '
21932192
'other list-likes without an index), these '

pandas/tests/test_strings.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ def test_cat(self):
132132
exp = np.array(['aa', NA, 'bb', 'bd', 'cfoo', NA], dtype=np.object_)
133133
tm.assert_almost_equal(result, exp)
134134

135+
# error for incorrect lengths
136+
rgx = 'All arrays must be same length'
137+
three = Series(['1', '2', '3'])
138+
139+
with tm.assert_raises_regex(ValueError, rgx):
140+
strings.str_cat(one, three)
141+
142+
# error for incorrect type
143+
rgx = "Must pass arrays containing strings to str_cat"
144+
with tm.assert_raises_regex(ValueError, rgx):
145+
strings.str_cat(one, 'three')
146+
135147
@pytest.mark.parametrize('series_or_index', ['series', 'index'])
136148
def test_str_cat(self, series_or_index):
137149
# test_cat above tests "str_cat" from ndarray to ndarray;
@@ -183,7 +195,7 @@ def test_str_cat(self, series_or_index):
183195
assert_series_or_index_equal(s.str.cat(list(t), na_rep='-'), exp)
184196

185197
# errors for incorrect lengths
186-
rgx = 'All arrays must be same length'
198+
rgx = 'All arrays must be same length, except.*'
187199
z = Series(['1', '2', '3'])
188200

189201
with tm.assert_raises_regex(ValueError, rgx):
@@ -305,7 +317,7 @@ def test_str_cat_mixed_inputs(self, series_or_index):
305317
assert_series_or_index_equal(s.str.cat(iter([t.values, list(s)])), exp)
306318

307319
# errors for incorrect lengths
308-
rgx = 'All arrays must be same length'
320+
rgx = 'All arrays must be same length, except.*'
309321
z = Series(['1', '2', '3'])
310322
e = concat([z, z], axis=1)
311323

@@ -331,7 +343,7 @@ def test_str_cat_mixed_inputs(self, series_or_index):
331343

332344
# errors for incorrect arguments in list-like
333345
rgx = 'others must be Series, Index, DataFrame,.*'
334-
# make sure None/Nan also work as string-replacements
346+
# make sure None/NaN do not crash checks in _get_series_list
335347
u = Series(['a', np.nan, 'c', None])
336348

337349
# mix of string and Series

0 commit comments

Comments
 (0)