@@ -1292,15 +1292,15 @@ def test_to_string_no_index(self):
1292
1292
1293
1293
df_s = df .to_string (index = False )
1294
1294
# Leading space is expected for positive numbers.
1295
- expected = (" x y z\n "
1296
- " 11 33 AAA\n "
1297
- " 22 -44 " )
1295
+ expected = (" x y z\n "
1296
+ "11 33 AAA\n "
1297
+ "22 -44 " )
1298
1298
assert df_s == expected
1299
1299
1300
1300
df_s = df [['y' , 'x' , 'z' ]].to_string (index = False )
1301
- expected = (" y x z\n "
1302
- " 33 11 AAA\n "
1303
- "-44 22 " )
1301
+ expected = (" y x z\n "
1302
+ " 33 11 AAA\n "
1303
+ "-44 22 " )
1304
1304
assert df_s == expected
1305
1305
1306
1306
def test_to_string_line_width_no_index (self ):
@@ -1315,7 +1315,7 @@ def test_to_string_line_width_no_index(self):
1315
1315
df = DataFrame ({'x' : [11 , 22 , 33 ], 'y' : [4 , 5 , 6 ]})
1316
1316
1317
1317
df_s = df .to_string (line_width = 1 , index = False )
1318
- expected = " x \\ \n 11 \n 22 \n 33 \n \n y \n 4 \n 5 \n 6 "
1318
+ expected = " x \\ \n 11 \n 22 \n 33 \n \n y \n 4 \n 5 \n 6 "
1319
1319
1320
1320
assert df_s == expected
1321
1321
@@ -1888,7 +1888,7 @@ def test_to_string_without_index(self):
1888
1888
# GH 11729 Test index=False option
1889
1889
s = Series ([1 , 2 , 3 , 4 ])
1890
1890
result = s .to_string (index = False )
1891
- expected = (u (' 1\n ' ) + ' 2\n ' + ' 3\n ' + ' 4' )
1891
+ expected = (u ('1\n ' ) + '2\n ' + '3\n ' + '4' )
1892
1892
assert result == expected
1893
1893
1894
1894
def test_unicode_name_in_footer (self ):
@@ -2380,6 +2380,15 @@ def test_to_string_header(self):
2380
2380
exp = '0 0\n ..\n 9 9'
2381
2381
assert res == exp
2382
2382
2383
+ @pytest .mark .parametrize ("inputs, expected" , [
2384
+ ([' a' , ' b' ], ' a\n b' ),
2385
+ (['.1' , '1' ], '.1\n 1' ),
2386
+ (['10' , '-10' ], ' 10\n -10' )
2387
+ ])
2388
+ def test_to_string_index_false_corner_case (self , inputs , expected ):
2389
+ s = pd .Series (inputs ).to_string (index = False )
2390
+ assert s == expected
2391
+
2383
2392
2384
2393
def _three_digit_exp ():
2385
2394
return '{x:.4g}' .format (x = 1.7e8 ) == '1.7e+008'
@@ -2780,6 +2789,31 @@ def test_format_percentiles():
2780
2789
fmt .format_percentiles ([0.1 , 0.5 , 'a' ])
2781
2790
2782
2791
2792
+ @pytest .mark .parametrize ("input_array, expected" , [
2793
+ ("a" , "a" ),
2794
+ (["a" , "b" ], "a\n b" ),
2795
+ ([1 , "a" ], "1\n a" ),
2796
+ (1 , "1" ),
2797
+ ([0 , - 1 ], " 0\n -1" ),
2798
+ (1.0 , '1.0' )
2799
+ ])
2800
+ def test_format_remove_leading_space_series (input_array , expected ):
2801
+ # GH: 24980
2802
+ s = pd .Series (input_array ).to_string (index = False )
2803
+ assert s == expected
2804
+
2805
+
2806
+ @pytest .mark .parametrize ("input_array, expected" , [
2807
+ ({"A" : ["a" ]}, "A\n a" ),
2808
+ ({"A" : ["a" , "b" ], "B" : ["c" , "dd" ]}, "A B\n a c\n b dd" ),
2809
+ ({"A" : ["a" , 1 ], "B" : ["aa" , 1 ]}, "A B\n a aa\n 1 1" )
2810
+ ])
2811
+ def test_format_remove_leading_space_dataframe (input_array , expected ):
2812
+ # GH: 24980
2813
+ df = pd .DataFrame (input_array ).to_string (index = False )
2814
+ assert df == expected
2815
+
2816
+
2783
2817
def test_repr_html_ipython_config (ip ):
2784
2818
code = textwrap .dedent ("""\
2785
2819
import pandas as pd
0 commit comments