@@ -2330,24 +2330,35 @@ def test_split_to_dataframe(self):
2330
2330
s .str .split ('_' , expand = "not_a_boolean" )
2331
2331
2332
2332
def test_split_to_multiindex_expand (self ):
2333
- idx = Index (['nosplit' , 'alsonosplit' ])
2333
+ # https://github.com/pandas-dev/pandas/issues/23677
2334
+
2335
+ idx = Index (['nosplit' , 'alsonosplit' , np .nan ])
2334
2336
result = idx .str .split ('_' , expand = True )
2335
2337
exp = idx
2336
2338
tm .assert_index_equal (result , exp )
2337
2339
assert result .nlevels == 1
2338
2340
2339
- idx = Index (['some_equal_splits' , 'with_no_nans' ])
2341
+ idx = Index (['some_equal_splits' , 'with_no_nans' , np . nan , None ])
2340
2342
result = idx .str .split ('_' , expand = True )
2341
- exp = MultiIndex .from_tuples ([('some' , 'equal' , 'splits' ), (
2342
- 'with' , 'no' , 'nans' )])
2343
+ exp = MultiIndex .from_tuples ([('some' , 'equal' , 'splits' ),
2344
+ ('with' , 'no' , 'nans' ),
2345
+ [np .nan , np .nan , np .nan ],
2346
+ [None , None , None ]])
2343
2347
tm .assert_index_equal (result , exp )
2344
2348
assert result .nlevels == 3
2345
2349
2346
- idx = Index (['some_unequal_splits' , 'one_of_these_things_is_not' ])
2350
+ idx = Index (['some_unequal_splits' ,
2351
+ 'one_of_these_things_is_not' ,
2352
+ np .nan , None ])
2347
2353
result = idx .str .split ('_' , expand = True )
2348
- exp = MultiIndex .from_tuples ([('some' , 'unequal' , 'splits' , NA , NA , NA
2349
- ), ('one' , 'of' , 'these' , 'things' ,
2350
- 'is' , 'not' )])
2354
+ exp = MultiIndex .from_tuples ([('some' , 'unequal' , 'splits' ,
2355
+ NA , NA , NA ),
2356
+ ('one' , 'of' , 'these' ,
2357
+ 'things' , 'is' , 'not' ),
2358
+ (np .nan , np .nan , np .nan ,
2359
+ np .nan , np .nan , np .nan ),
2360
+ (None , None , None ,
2361
+ None , None , None )])
2351
2362
tm .assert_index_equal (result , exp )
2352
2363
assert result .nlevels == 6
2353
2364
@@ -2441,50 +2452,54 @@ def test_split_with_name(self):
2441
2452
tm .assert_index_equal (res , exp )
2442
2453
2443
2454
def test_partition_series (self ):
2444
- values = Series (['a_b_c' , 'c_d_e' , NA , 'f_g_h' ])
2455
+ # https://github.com/pandas-dev/pandas/issues/23558
2456
+
2457
+ values = Series (['a_b_c' , 'c_d_e' , NA , 'f_g_h' , None ])
2445
2458
2446
2459
result = values .str .partition ('_' , expand = False )
2447
2460
exp = Series ([('a' , '_' , 'b_c' ), ('c' , '_' , 'd_e' ), NA ,
2448
- ('f' , '_' , 'g_h' )])
2461
+ ('f' , '_' , 'g_h' ), None ])
2449
2462
tm .assert_series_equal (result , exp )
2450
2463
2451
2464
result = values .str .rpartition ('_' , expand = False )
2452
2465
exp = Series ([('a_b' , '_' , 'c' ), ('c_d' , '_' , 'e' ), NA ,
2453
- ('f_g' , '_' , 'h' )])
2466
+ ('f_g' , '_' , 'h' ), None ])
2454
2467
tm .assert_series_equal (result , exp )
2455
2468
2456
2469
# more than one char
2457
- values = Series (['a__b__c' , 'c__d__e' , NA , 'f__g__h' ])
2470
+ values = Series (['a__b__c' , 'c__d__e' , NA , 'f__g__h' , None ])
2458
2471
result = values .str .partition ('__' , expand = False )
2459
2472
exp = Series ([('a' , '__' , 'b__c' ), ('c' , '__' , 'd__e' ), NA ,
2460
- ('f' , '__' , 'g__h' )])
2473
+ ('f' , '__' , 'g__h' ), None ])
2461
2474
tm .assert_series_equal (result , exp )
2462
2475
2463
2476
result = values .str .rpartition ('__' , expand = False )
2464
2477
exp = Series ([('a__b' , '__' , 'c' ), ('c__d' , '__' , 'e' ), NA ,
2465
- ('f__g' , '__' , 'h' )])
2478
+ ('f__g' , '__' , 'h' ), None ])
2466
2479
tm .assert_series_equal (result , exp )
2467
2480
2468
2481
# None
2469
- values = Series (['a b c' , 'c d e' , NA , 'f g h' ])
2482
+ values = Series (['a b c' , 'c d e' , NA , 'f g h' , None ])
2470
2483
result = values .str .partition (expand = False )
2471
2484
exp = Series ([('a' , ' ' , 'b c' ), ('c' , ' ' , 'd e' ), NA ,
2472
- ('f' , ' ' , 'g h' )])
2485
+ ('f' , ' ' , 'g h' ), None ])
2473
2486
tm .assert_series_equal (result , exp )
2474
2487
2475
2488
result = values .str .rpartition (expand = False )
2476
2489
exp = Series ([('a b' , ' ' , 'c' ), ('c d' , ' ' , 'e' ), NA ,
2477
- ('f g' , ' ' , 'h' )])
2490
+ ('f g' , ' ' , 'h' ), None ])
2478
2491
tm .assert_series_equal (result , exp )
2479
2492
2480
- # Not splited
2481
- values = Series (['abc' , 'cde' , NA , 'fgh' ])
2493
+ # Not split
2494
+ values = Series (['abc' , 'cde' , NA , 'fgh' , None ])
2482
2495
result = values .str .partition ('_' , expand = False )
2483
- exp = Series ([('abc' , '' , '' ), ('cde' , '' , '' ), NA , ('fgh' , '' , '' )])
2496
+ exp = Series ([('abc' , '' , '' ), ('cde' , '' , '' ), NA ,
2497
+ ('fgh' , '' , '' ), None ])
2484
2498
tm .assert_series_equal (result , exp )
2485
2499
2486
2500
result = values .str .rpartition ('_' , expand = False )
2487
- exp = Series ([('' , '' , 'abc' ), ('' , '' , 'cde' ), NA , ('' , '' , 'fgh' )])
2501
+ exp = Series ([('' , '' , 'abc' ), ('' , '' , 'cde' ), NA ,
2502
+ ('' , '' , 'fgh' ), None ])
2488
2503
tm .assert_series_equal (result , exp )
2489
2504
2490
2505
# unicode
@@ -2508,57 +2523,65 @@ def test_partition_series(self):
2508
2523
assert result == [v .rpartition ('_' ) for v in values ]
2509
2524
2510
2525
def test_partition_index (self ):
2511
- values = Index (['a_b_c' , 'c_d_e' , 'f_g_h' ])
2526
+ # https://github.com/pandas-dev/pandas/issues/23558
2527
+
2528
+ values = Index (['a_b_c' , 'c_d_e' , 'f_g_h' , np .nan , None ])
2512
2529
2513
2530
result = values .str .partition ('_' , expand = False )
2514
- exp = Index (np .array ([('a' , '_' , 'b_c' ), ('c' , '_' , 'd_e' ), ( 'f' , '_' ,
2515
- 'g_h' )]))
2531
+ exp = Index (np .array ([('a' , '_' , 'b_c' ), ('c' , '_' , 'd_e' ),
2532
+ ( 'f' , '_' , 'g_h' ), np . nan , None ]))
2516
2533
tm .assert_index_equal (result , exp )
2517
2534
assert result .nlevels == 1
2518
2535
2519
2536
result = values .str .rpartition ('_' , expand = False )
2520
- exp = Index (np .array ([('a_b' , '_' , 'c' ), ('c_d' , '_' , 'e' ), (
2521
- 'f_g' , '_' , 'h' )]))
2537
+ exp = Index (np .array ([('a_b' , '_' , 'c' ), ('c_d' , '_' , 'e' ),
2538
+ ( 'f_g' , '_' , 'h' ), np . nan , None ]))
2522
2539
tm .assert_index_equal (result , exp )
2523
2540
assert result .nlevels == 1
2524
2541
2525
2542
result = values .str .partition ('_' )
2526
- exp = Index ([('a' , '_' , 'b_c' ), ('c' , '_' , 'd_e' ), ('f' , '_' , 'g_h' )])
2543
+ exp = Index ([('a' , '_' , 'b_c' ), ('c' , '_' , 'd_e' ),
2544
+ ('f' , '_' , 'g_h' ), (np .nan , np .nan , np .nan ),
2545
+ (None , None , None )])
2527
2546
tm .assert_index_equal (result , exp )
2528
2547
assert isinstance (result , MultiIndex )
2529
2548
assert result .nlevels == 3
2530
2549
2531
2550
result = values .str .rpartition ('_' )
2532
- exp = Index ([('a_b' , '_' , 'c' ), ('c_d' , '_' , 'e' ), ('f_g' , '_' , 'h' )])
2551
+ exp = Index ([('a_b' , '_' , 'c' ), ('c_d' , '_' , 'e' ),
2552
+ ('f_g' , '_' , 'h' ), (np .nan , np .nan , np .nan ),
2553
+ (None , None , None )])
2533
2554
tm .assert_index_equal (result , exp )
2534
2555
assert isinstance (result , MultiIndex )
2535
2556
assert result .nlevels == 3
2536
2557
2537
2558
def test_partition_to_dataframe (self ):
2538
- values = Series (['a_b_c' , 'c_d_e' , NA , 'f_g_h' ])
2559
+ # https://github.com/pandas-dev/pandas/issues/23558
2560
+
2561
+ values = Series (['a_b_c' , 'c_d_e' , NA , 'f_g_h' , None ])
2539
2562
result = values .str .partition ('_' )
2540
- exp = DataFrame ({0 : ['a' , 'c' , np .nan , 'f' ],
2541
- 1 : ['_' , '_' , np .nan , '_' ],
2542
- 2 : ['b_c' , 'd_e' , np .nan , 'g_h' ]})
2563
+ exp = DataFrame ({0 : ['a' , 'c' , np .nan , 'f' , None ],
2564
+ 1 : ['_' , '_' , np .nan , '_' , None ],
2565
+ 2 : ['b_c' , 'd_e' , np .nan , 'g_h' , None ]})
2543
2566
tm .assert_frame_equal (result , exp )
2544
2567
2545
2568
result = values .str .rpartition ('_' )
2546
- exp = DataFrame ({0 : ['a_b' , 'c_d' , np .nan , 'f_g' ],
2547
- 1 : ['_' , '_' , np .nan , '_' ],
2548
- 2 : ['c' , 'e' , np .nan , 'h' ]})
2569
+ exp = DataFrame ({0 : ['a_b' , 'c_d' , np .nan , 'f_g' , None ],
2570
+ 1 : ['_' , '_' , np .nan , '_' , None ],
2571
+ 2 : ['c' , 'e' , np .nan , 'h' , None ]})
2549
2572
tm .assert_frame_equal (result , exp )
2550
2573
2551
- values = Series (['a_b_c' , 'c_d_e' , NA , 'f_g_h' ])
2574
+ values = Series (['a_b_c' , 'c_d_e' , NA , 'f_g_h' , None ])
2552
2575
result = values .str .partition ('_' , expand = True )
2553
- exp = DataFrame ({0 : ['a' , 'c' , np .nan , 'f' ],
2554
- 1 : ['_' , '_' , np .nan , '_' ],
2555
- 2 : ['b_c' , 'd_e' , np .nan , 'g_h' ]})
2576
+ exp = DataFrame ({0 : ['a' , 'c' , np .nan , 'f' , None ],
2577
+ 1 : ['_' , '_' , np .nan , '_' , None ],
2578
+ 2 : ['b_c' , 'd_e' , np .nan , 'g_h' , None ]})
2556
2579
tm .assert_frame_equal (result , exp )
2557
2580
2558
2581
result = values .str .rpartition ('_' , expand = True )
2559
- exp = DataFrame ({0 : ['a_b' , 'c_d' , np .nan , 'f_g' ],
2560
- 1 : ['_' , '_' , np .nan , '_' ],
2561
- 2 : ['c' , 'e' , np .nan , 'h' ]})
2582
+ exp = DataFrame ({0 : ['a_b' , 'c_d' , np .nan , 'f_g' , None ],
2583
+ 1 : ['_' , '_' , np .nan , '_' , None ],
2584
+ 2 : ['c' , 'e' , np .nan , 'h' , None ]})
2562
2585
tm .assert_frame_equal (result , exp )
2563
2586
2564
2587
def test_partition_with_name (self ):
0 commit comments