Skip to content

Commit deab820

Browse files
committed
BUG: Index.str.partition not nan-safe (#23558)
1 parent 5edc439 commit deab820

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

pandas/_libs/lib.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -2274,7 +2274,7 @@ def to_object_array_tuples(rows: list):
22742274

22752275
k = 0
22762276
for i in range(n):
2277-
tmp = len(rows[i])
2277+
tmp = 1 if is_float(rows[i]) else len(rows[i])
22782278
if tmp > k:
22792279
k = tmp
22802280

@@ -2288,7 +2288,7 @@ def to_object_array_tuples(rows: list):
22882288
except Exception:
22892289
# upcast any subclasses to tuple
22902290
for i in range(n):
2291-
row = tuple(rows[i])
2291+
row = tuple(rows[i]) if not is_float(rows[i]) else (rows[i],)
22922292
for j in range(len(row)):
22932293
result[i, j] = row[j]
22942294

pandas/tests/test_strings.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -2493,7 +2493,7 @@ def test_partition_series(self):
24932493
('f g', ' ', 'h')])
24942494
tm.assert_series_equal(result, exp)
24952495

2496-
# Not splited
2496+
# Not split
24972497
values = Series(['abc', 'cde', NA, 'fgh'])
24982498
result = values.str.partition('_', expand=False)
24992499
exp = Series([('abc', '', ''), ('cde', '', ''), NA, ('fgh', '', '')])
@@ -2524,28 +2524,30 @@ def test_partition_series(self):
25242524
assert result == [v.rpartition('_') for v in values]
25252525

25262526
def test_partition_index(self):
2527-
values = Index(['a_b_c', 'c_d_e', 'f_g_h'])
2527+
values = Index(['a_b_c', 'c_d_e', 'f_g_h', np.nan])
25282528

25292529
result = values.str.partition('_', expand=False)
2530-
exp = Index(np.array([('a', '_', 'b_c'), ('c', '_', 'd_e'), ('f', '_',
2531-
'g_h')]))
2530+
exp = Index(np.array([('a', '_', 'b_c'), ('c', '_', 'd_e'),
2531+
('f', '_', 'g_h'), np.nan]))
25322532
tm.assert_index_equal(result, exp)
25332533
assert result.nlevels == 1
25342534

25352535
result = values.str.rpartition('_', expand=False)
2536-
exp = Index(np.array([('a_b', '_', 'c'), ('c_d', '_', 'e'), (
2537-
'f_g', '_', 'h')]))
2536+
exp = Index(np.array([('a_b', '_', 'c'), ('c_d', '_', 'e'),
2537+
('f_g', '_', 'h'), np.nan]))
25382538
tm.assert_index_equal(result, exp)
25392539
assert result.nlevels == 1
25402540

25412541
result = values.str.partition('_')
2542-
exp = Index([('a', '_', 'b_c'), ('c', '_', 'd_e'), ('f', '_', 'g_h')])
2542+
exp = Index([('a', '_', 'b_c'), ('c', '_', 'd_e'),
2543+
('f', '_', 'g_h'), (np.nan, np.nan, np.nan)])
25432544
tm.assert_index_equal(result, exp)
25442545
assert isinstance(result, MultiIndex)
25452546
assert result.nlevels == 3
25462547

25472548
result = values.str.rpartition('_')
2548-
exp = Index([('a_b', '_', 'c'), ('c_d', '_', 'e'), ('f_g', '_', 'h')])
2549+
exp = Index([('a_b', '_', 'c'), ('c_d', '_', 'e'),
2550+
('f_g', '_', 'h'), (np.nan, np.nan, np.nan)])
25492551
tm.assert_index_equal(result, exp)
25502552
assert isinstance(result, MultiIndex)
25512553
assert result.nlevels == 3

0 commit comments

Comments
 (0)