Skip to content

Commit 7a885ee

Browse files
h-vetinarijreback
authored andcommitted
BUG: all-na corner case for str.cat (#24045)
1 parent 92d25f0 commit 7a885ee

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

doc/source/whatsnew/v0.24.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1365,6 +1365,7 @@ Strings
13651365
- Bug in :meth:`Index.str.partition` was not nan-safe (:issue:`23558`).
13661366
- Bug in :meth:`Index.str.split` was not nan-safe (:issue:`23677`).
13671367
- Bug :func:`Series.str.contains` not respecting the ``na`` argument for a ``Categorical`` dtype ``Series`` (:issue:`22158`)
1368+
- Bug in :meth:`Index.str.cat` when the result contained only ``NaN`` (:issue:`24044`)
13681369

13691370
Interval
13701371
^^^^^^^^

pandas/core/strings.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -2260,9 +2260,11 @@ def cat(self, others=None, sep=None, na_rep=None, join=None):
22602260
result = cat_core(all_cols, sep)
22612261

22622262
if isinstance(self._orig, Index):
2263-
result = Index(result, name=self._orig.name)
2263+
# add dtype for case that result is all-NA
2264+
result = Index(result, dtype=object, name=self._orig.name)
22642265
else: # Series
2265-
result = Series(result, index=data.index, name=self._orig.name)
2266+
result = Series(result, dtype=object, index=data.index,
2267+
name=self._orig.name)
22662268
return result
22672269

22682270
_shared_docs['str_split'] = ("""

pandas/tests/test_strings.py

+25-5
Original file line numberDiff line numberDiff line change
@@ -630,11 +630,31 @@ def test_str_cat_align_mixed_inputs(self, join):
630630
with pytest.raises(ValueError, match=rgx):
631631
s.str.cat([t, z], join=join)
632632

633-
def test_str_cat_raises(self):
634-
# non-strings hiding behind object dtype
635-
s = Series([1, 2, 3, 4], dtype='object')
636-
with pytest.raises(TypeError, match="unsupported operand type.*"):
637-
s.str.cat(s)
633+
@pytest.mark.parametrize('box', [Series, Index])
634+
@pytest.mark.parametrize('other', [Series, Index])
635+
def test_str_cat_all_na(self, box, other):
636+
# GH 24044
637+
638+
# check that all NaNs in caller / target work
639+
s = Index(['a', 'b', 'c', 'd'])
640+
s = s if box == Index else Series(s, index=s)
641+
t = other([np.nan] * 4, dtype=object)
642+
# add index of s for alignment
643+
t = t if other == Index else Series(t, index=s)
644+
645+
# all-NA target
646+
if box == Series:
647+
expected = Series([np.nan] * 4, index=s.index, dtype=object)
648+
else: # box == Index
649+
expected = Index([np.nan] * 4, dtype=object)
650+
result = s.str.cat(t, join='left')
651+
assert_series_or_index_equal(result, expected)
652+
653+
# all-NA caller (only for Series)
654+
if other == Series:
655+
expected = Series([np.nan] * 4, dtype=object, index=t.index)
656+
result = t.str.cat(s, join='left')
657+
tm.assert_series_equal(result, expected)
638658

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

0 commit comments

Comments
 (0)