Skip to content

Commit f13553f

Browse files
committed
Code review: included suggestes improvements
1 parent 213e7b6 commit f13553f

File tree

3 files changed

+38
-34
lines changed

3 files changed

+38
-34
lines changed

doc/source/whatsnew/v0.24.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1192,7 +1192,7 @@ Numeric
11921192
Strings
11931193
^^^^^^^
11941194

1195-
- BUG Index.str.partition not nan-safe (:issue:`23558`)
1195+
- BUG in :func:`Index.str.partition` is not nan-safe (:issue:`23558`)
11961196
-
11971197
-
11981198

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 = 1 if util.is_nan(rows[i]) else len(rows[i])
2277+
tmp = 1 if checknull(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 = (rows[i],) if util.is_nan(rows[i]) else tuple(rows[i])
2291+
row = (rows[i],) if checknull(rows[i]) else tuple(rows[i])
22922292
for j in range(len(row)):
22932293
result[i, j] = row[j]
22942294

pandas/tests/test_strings.py

+35-31
Original file line numberDiff line numberDiff line change
@@ -2457,50 +2457,52 @@ def test_split_with_name(self):
24572457
tm.assert_index_equal(res, exp)
24582458

24592459
def test_partition_series(self):
2460-
values = Series(['a_b_c', 'c_d_e', NA, 'f_g_h'])
2460+
values = Series(['a_b_c', 'c_d_e', NA, 'f_g_h', None])
24612461

24622462
result = values.str.partition('_', expand=False)
24632463
exp = Series([('a', '_', 'b_c'), ('c', '_', 'd_e'), NA,
2464-
('f', '_', 'g_h')])
2464+
('f', '_', 'g_h'), None])
24652465
tm.assert_series_equal(result, exp)
24662466

24672467
result = values.str.rpartition('_', expand=False)
24682468
exp = Series([('a_b', '_', 'c'), ('c_d', '_', 'e'), NA,
2469-
('f_g', '_', 'h')])
2469+
('f_g', '_', 'h'), None])
24702470
tm.assert_series_equal(result, exp)
24712471

24722472
# more than one char
2473-
values = Series(['a__b__c', 'c__d__e', NA, 'f__g__h'])
2473+
values = Series(['a__b__c', 'c__d__e', NA, 'f__g__h', None])
24742474
result = values.str.partition('__', expand=False)
24752475
exp = Series([('a', '__', 'b__c'), ('c', '__', 'd__e'), NA,
2476-
('f', '__', 'g__h')])
2476+
('f', '__', 'g__h'), None])
24772477
tm.assert_series_equal(result, exp)
24782478

24792479
result = values.str.rpartition('__', expand=False)
24802480
exp = Series([('a__b', '__', 'c'), ('c__d', '__', 'e'), NA,
2481-
('f__g', '__', 'h')])
2481+
('f__g', '__', 'h'), None])
24822482
tm.assert_series_equal(result, exp)
24832483

24842484
# None
2485-
values = Series(['a b c', 'c d e', NA, 'f g h'])
2485+
values = Series(['a b c', 'c d e', NA, 'f g h', None])
24862486
result = values.str.partition(expand=False)
24872487
exp = Series([('a', ' ', 'b c'), ('c', ' ', 'd e'), NA,
2488-
('f', ' ', 'g h')])
2488+
('f', ' ', 'g h'), None])
24892489
tm.assert_series_equal(result, exp)
24902490

24912491
result = values.str.rpartition(expand=False)
24922492
exp = Series([('a b', ' ', 'c'), ('c d', ' ', 'e'), NA,
2493-
('f g', ' ', 'h')])
2493+
('f g', ' ', 'h'), None])
24942494
tm.assert_series_equal(result, exp)
24952495

24962496
# Not split
2497-
values = Series(['abc', 'cde', NA, 'fgh'])
2497+
values = Series(['abc', 'cde', NA, 'fgh', None])
24982498
result = values.str.partition('_', expand=False)
2499-
exp = Series([('abc', '', ''), ('cde', '', ''), NA, ('fgh', '', '')])
2499+
exp = Series([('abc', '', ''), ('cde', '', ''), NA,
2500+
('fgh', '', ''), None])
25002501
tm.assert_series_equal(result, exp)
25012502

25022503
result = values.str.rpartition('_', expand=False)
2503-
exp = Series([('', '', 'abc'), ('', '', 'cde'), NA, ('', '', 'fgh')])
2504+
exp = Series([('', '', 'abc'), ('', '', 'cde'), NA,
2505+
('', '', 'fgh'), None])
25042506
tm.assert_series_equal(result, exp)
25052507

25062508
# unicode
@@ -2524,59 +2526,61 @@ def test_partition_series(self):
25242526
assert result == [v.rpartition('_') for v in values]
25252527

25262528
def test_partition_index(self):
2527-
values = Index(['a_b_c', 'c_d_e', 'f_g_h', np.nan])
2529+
values = Index(['a_b_c', 'c_d_e', 'f_g_h', np.nan, None])
25282530

25292531
result = values.str.partition('_', expand=False)
25302532
exp = Index(np.array([('a', '_', 'b_c'), ('c', '_', 'd_e'),
2531-
('f', '_', 'g_h'), np.nan]))
2533+
('f', '_', 'g_h'), np.nan, None]))
25322534
tm.assert_index_equal(result, exp)
25332535
assert result.nlevels == 1
25342536

25352537
result = values.str.rpartition('_', expand=False)
25362538
exp = Index(np.array([('a_b', '_', 'c'), ('c_d', '_', 'e'),
2537-
('f_g', '_', 'h'), np.nan]))
2539+
('f_g', '_', 'h'), np.nan, None]))
25382540
tm.assert_index_equal(result, exp)
25392541
assert result.nlevels == 1
25402542

25412543
result = values.str.partition('_')
25422544
exp = Index([('a', '_', 'b_c'), ('c', '_', 'd_e'),
2543-
('f', '_', 'g_h'), (np.nan, np.nan, np.nan)])
2545+
('f', '_', 'g_h'), (np.nan, np.nan, np.nan),
2546+
(None, None, None)])
25442547
tm.assert_index_equal(result, exp)
25452548
assert isinstance(result, MultiIndex)
25462549
assert result.nlevels == 3
25472550

25482551
result = values.str.rpartition('_')
25492552
exp = Index([('a_b', '_', 'c'), ('c_d', '_', 'e'),
2550-
('f_g', '_', 'h'), (np.nan, np.nan, np.nan)])
2553+
('f_g', '_', 'h'), (np.nan, np.nan, np.nan),
2554+
(None, None, None)])
25512555
tm.assert_index_equal(result, exp)
25522556
assert isinstance(result, MultiIndex)
25532557
assert result.nlevels == 3
25542558

25552559
def test_partition_to_dataframe(self):
2556-
values = Series(['a_b_c', 'c_d_e', NA, 'f_g_h'])
2560+
values = Series(['a_b_c', 'c_d_e', NA, 'f_g_h', None])
25572561
result = values.str.partition('_')
2558-
exp = DataFrame({0: ['a', 'c', np.nan, 'f'],
2559-
1: ['_', '_', np.nan, '_'],
2560-
2: ['b_c', 'd_e', np.nan, 'g_h']})
2562+
exp = DataFrame({0: ['a', 'c', np.nan, 'f', None],
2563+
1: ['_', '_', np.nan, '_', None],
2564+
2: ['b_c', 'd_e', np.nan, 'g_h', None]})
25612565
tm.assert_frame_equal(result, exp)
25622566

25632567
result = values.str.rpartition('_')
2564-
exp = DataFrame({0: ['a_b', 'c_d', np.nan, 'f_g'],
2565-
1: ['_', '_', np.nan, '_'],
2566-
2: ['c', 'e', np.nan, 'h']})
2568+
exp = DataFrame({0: ['a_b', 'c_d', np.nan, 'f_g', None],
2569+
1: ['_', '_', np.nan, '_', None],
2570+
2: ['c', 'e', np.nan, 'h', None]})
25672571
tm.assert_frame_equal(result, exp)
25682572

2569-
values = Series(['a_b_c', 'c_d_e', NA, 'f_g_h'])
2573+
values = Series(['a_b_c', 'c_d_e', NA, 'f_g_h', None])
25702574
result = values.str.partition('_', expand=True)
2571-
exp = DataFrame({0: ['a', 'c', np.nan, 'f'],
2572-
1: ['_', '_', np.nan, '_'],
2573-
2: ['b_c', 'd_e', np.nan, 'g_h']})
2575+
exp = DataFrame({0: ['a', 'c', np.nan, 'f', None],
2576+
1: ['_', '_', np.nan, '_', None],
2577+
2: ['b_c', 'd_e', np.nan, 'g_h', None]})
25742578
tm.assert_frame_equal(result, exp)
25752579

25762580
result = values.str.rpartition('_', expand=True)
2577-
exp = DataFrame({0: ['a_b', 'c_d', np.nan, 'f_g'],
2578-
1: ['_', '_', np.nan, '_'],
2579-
2: ['c', 'e', np.nan, 'h']})
2581+
exp = DataFrame({0: ['a_b', 'c_d', np.nan, 'f_g', None],
2582+
1: ['_', '_', np.nan, '_', None],
2583+
2: ['c', 'e', np.nan, 'h', None]})
25802584
tm.assert_frame_equal(result, exp)
25812585

25822586
def test_partition_with_name(self):

0 commit comments

Comments
 (0)