Skip to content

Commit 660979f

Browse files
amendents
1 parent 57a3d67 commit 660979f

File tree

2 files changed

+58
-22
lines changed

2 files changed

+58
-22
lines changed

pandas/io/formats/format.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -252,15 +252,15 @@ def _get_formatted_index(self):
252252

253253
def _get_formatted_values(self):
254254
values_to_format = self.tr_series._formatting_values()
255-
if self.index:
256-
return format_array(values_to_format, None,
257-
float_format=self.float_format,
258-
na_rep=self.na_rep)
255+
256+
if bool(self.index):
257+
leading_space = None
259258
else:
260-
return format_array(values_to_format, None,
261-
float_format=self.float_format,
262-
na_rep=self.na_rep,
263-
leading_space=False)
259+
leading_space = False
260+
return format_array(values_to_format, None,
261+
float_format=self.float_format,
262+
na_rep=self.na_rep,
263+
leading_space=leading_space)
264264

265265
def to_string(self):
266266
series = self.tr_series
@@ -710,9 +710,15 @@ def _format_col(self, i):
710710
frame = self.tr_frame
711711
formatter = self._get_formatter(i)
712712
values_to_format = frame.iloc[:, i]._formatting_values()
713+
714+
if bool(self.index):
715+
leading_space = None
716+
else:
717+
leading_space = False
713718
return format_array(values_to_format, formatter,
714719
float_format=self.float_format, na_rep=self.na_rep,
715-
space=self.col_space, decimal=self.decimal)
720+
space=self.col_space, decimal=self.decimal,
721+
leading_space=leading_space)
716722

717723
def to_html(self, classes=None, notebook=False, border=None):
718724
"""
@@ -1087,7 +1093,11 @@ def format_values_with(float_format):
10871093
# The default is otherwise to use str instead of a formatting string
10881094
if self.float_format is None:
10891095
if self.fixed_width:
1090-
float_format = partial('{value: .{digits:d}f}'.format,
1096+
if self.leading_space is not False:
1097+
fmt_str = '{value: .{digits:d}f}'
1098+
else:
1099+
fmt_str = '{value:.{digits:d}f}'
1100+
float_format = partial(fmt_str.format,
10911101
digits=self.digits)
10921102
else:
10931103
float_format = self.float_format
@@ -1119,7 +1129,11 @@ def format_values_with(float_format):
11191129
(abs_vals > 0)).any()
11201130

11211131
if has_small_values or (too_long and has_large_values):
1122-
float_format = partial('{value: .{digits:d}e}'.format,
1132+
if self.leading_space is not False:
1133+
fmt_str = '{value: .{digits:d}e}'
1134+
else:
1135+
fmt_str = '{value:.{digits:d}e}'
1136+
float_format = partial(fmt_str.format,
11231137
digits=self.digits)
11241138
formatted_values = format_values_with(float_format)
11251139

@@ -1136,7 +1150,12 @@ def _format_strings(self):
11361150
class IntArrayFormatter(GenericArrayFormatter):
11371151

11381152
def _format_strings(self):
1139-
formatter = self.formatter or (lambda x: '{x: d}'.format(x=x))
1153+
if self.leading_space is False:
1154+
fmt_str = '{x:d}'
1155+
else:
1156+
fmt_str = '{x: d}'
1157+
formatter = self.formatter or (lambda x: fmt_str.format(x=x))
1158+
# formatter = self.formatter or (lambda x: '{x: d}'.format(x=x))
11401159
fmt_values = [formatter(x) for x in self.values]
11411160
return fmt_values
11421161

pandas/tests/io/formats/test_format.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,15 +1291,15 @@ def test_to_string_no_index(self):
12911291

12921292
df_s = df.to_string(index=False)
12931293
# Leading space is expected for positive numbers.
1294-
expected = (" x y z\n"
1295-
" 11 33 AAA\n"
1296-
" 22 -44 ")
1294+
expected = (" x y z\n"
1295+
"11 33 AAA\n"
1296+
"22 -44 ")
12971297
assert df_s == expected
12981298

12991299
df_s = df[['y', 'x', 'z']].to_string(index=False)
1300-
expected = (" y x z\n"
1301-
" 33 11 AAA\n"
1302-
"-44 22 ")
1300+
expected = (" y x z\n"
1301+
" 33 11 AAA\n"
1302+
"-44 22 ")
13031303
assert df_s == expected
13041304

13051305
def test_to_string_line_width_no_index(self):
@@ -1314,7 +1314,7 @@ def test_to_string_line_width_no_index(self):
13141314
df = DataFrame({'x': [11, 22, 33], 'y': [4, 5, 6]})
13151315

13161316
df_s = df.to_string(line_width=1, index=False)
1317-
expected = " x \\\n 11 \n 22 \n 33 \n\n y \n 4 \n 5 \n 6 "
1317+
expected = " x \\\n11 \n22 \n33 \n\n y \n 4 \n 5 \n 6 "
13181318

13191319
assert df_s == expected
13201320

@@ -1887,7 +1887,7 @@ def test_to_string_without_index(self):
18871887
# GH 11729 Test index=False option
18881888
s = Series([1, 2, 3, 4])
18891889
result = s.to_string(index=False)
1890-
expected = (u(' 1\n') + ' 2\n' + ' 3\n' + ' 4')
1890+
expected = (u('1\n') + '2\n' + '3\n' + '4')
18911891
assert result == expected
18921892

18931893
def test_unicode_name_in_footer(self):
@@ -2781,9 +2781,26 @@ def test_format_percentiles():
27812781

27822782
@pytest.mark.parametrize("input_array, expected", [
27832783
("a", "a"),
2784-
(["a", "b"], "a\nb")
2784+
(["a", "b"], "a\nb"),
2785+
([1, "a"], "1\na"),
2786+
(1, "1"),
2787+
([0, -1], " 0\n-1"),
2788+
(1.0, '1.0')
27852789
])
2786-
def test_format_remove_leading_space(input_array, expected):
2790+
def test_format_remove_leading_space_series(input_array, expected):
27872791
# GH: 24980
27882792
s = pd.Series(input_array).to_string(index=False)
27892793
assert s == expected
2794+
2795+
2796+
@pytest.mark.parametrize("input_array, expected", [
2797+
({"A": ["a"]}, "A\na"),
2798+
({"A": ["a", "b"], "B": ["c", "dd"]}, "A B\na c\nb dd"),
2799+
({"A": ["a", 1], "B": ["aa", 1]}, "A B\na aa\n1 1")
2800+
])
2801+
def test_try(input_array, expected):
2802+
df = pd.DataFrame(input_array).to_string(index=False)
2803+
assert df == expected
2804+
2805+
df = pd.DataFrame({"A": ["a", "b"], "B": ["c", "dd"]}).to_string(index=False)
2806+
assert df == "A B\na c\nb dd"

0 commit comments

Comments
 (0)