Skip to content

BUG: all-na corner case for str.cat #24045

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

Merged
merged 3 commits into from
Dec 2, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1365,6 +1365,7 @@ Strings
- Bug in :meth:`Index.str.partition` was not nan-safe (:issue:`23558`).
- Bug in :meth:`Index.str.split` was not nan-safe (:issue:`23677`).
- Bug :func:`Series.str.contains` not respecting the ``na`` argument for a ``Categorical`` dtype ``Series`` (:issue:`22158`)
- Bug in :meth:`Index.str.cat` when the result contained only ``NaN`` (:issue:`24044`)

Interval
^^^^^^^^
Expand Down
6 changes: 4 additions & 2 deletions pandas/core/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2260,9 +2260,11 @@ def cat(self, others=None, sep=None, na_rep=None, join=None):
result = cat_core(all_cols, sep)

if isinstance(self._orig, Index):
result = Index(result, name=self._orig.name)
# add dtype for case that result is all-NA
result = Index(result, dtype=object, name=self._orig.name)
else: # Series
result = Series(result, index=data.index, name=self._orig.name)
result = Series(result, dtype=object, index=data.index,
name=self._orig.name)
return result

_shared_docs['str_split'] = ("""
Expand Down
30 changes: 25 additions & 5 deletions pandas/tests/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,11 +630,31 @@ def test_str_cat_align_mixed_inputs(self, join):
with pytest.raises(ValueError, match=rgx):
s.str.cat([t, z], join=join)

def test_str_cat_raises(self):
# non-strings hiding behind object dtype
s = Series([1, 2, 3, 4], dtype='object')
with pytest.raises(TypeError, match="unsupported operand type.*"):
s.str.cat(s)
@pytest.mark.parametrize('box', [Series, Index])
@pytest.mark.parametrize('other', [Series, Index])
def test_str_cat_all_na(self, box, other):
# GH 24044

# check that all NaNs in caller / target work
s = Index(['a', 'b', 'c', 'd'])
s = s if box == Index else Series(s, index=s)
t = other([np.nan] * 4, dtype=object)
# add index of s for alignment
t = t if other == Index else Series(t, index=s)

# all-NA target
if box == Series:
expected = Series([np.nan] * 4, index=s.index, dtype=object)
else: # box == Index
expected = Index([np.nan] * 4, dtype=object)
result = s.str.cat(t, join='left')
assert_series_or_index_equal(result, expected)

# all-NA caller (only for Series)
if other == Series:
expected = Series([np.nan] * 4, dtype=object, index=t.index)
result = t.str.cat(s, join='left')
tm.assert_series_equal(result, expected)

def test_str_cat_special_cases(self):
s = Series(['a', 'b', 'c', 'd'])
Expand Down