Skip to content

Commit d21255a

Browse files
fix conflict
1 parent 5d1e633 commit d21255a

File tree

5 files changed

+79
-28
lines changed

5 files changed

+79
-28
lines changed

doc/source/whatsnew/v0.24.1.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Fixed Regressions
2222

2323
- Bug in :meth:`DataFrame.itertuples` with ``records`` orient raising an ``AttributeError`` when the ``DataFrame`` contained more than 255 columns (:issue:`24939`)
2424
- Bug in :meth:`DataFrame.itertuples` orient converting integer column names to strings prepended with an underscore (:issue:`24940`)
25-
- Fixed regression in :class:`Index.intersection` incorrectly sorting the values by default (:issue:`24959`).
25+
- Bug in :meth:`to_string` with ``index`` set to ``False``, leading space is added. (:issue:`24980`)
2626

2727
.. _whatsnew_0241.enhancements:
2828

pandas/core/generic.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -2834,9 +2834,9 @@ def to_latex(self, buf=None, columns=None, col_space=None, header=True,
28342834
... 'mask': ['red', 'purple'],
28352835
... 'weapon': ['sai', 'bo staff']})
28362836
>>> df.to_latex(index=False) # doctest: +NORMALIZE_WHITESPACE
2837-
'\\begin{tabular}{lll}\n\\toprule\n name & mask & weapon
2838-
\\\\\n\\midrule\n Raphael & red & sai \\\\\n Donatello &
2839-
purple & bo staff \\\\\n\\bottomrule\n\\end{tabular}\n'
2837+
'\\begin{tabular}{lll}\n\\toprule\n name & mask & weapon
2838+
\\\\\n\\midrule\n Raphael & red & sai \\\\\nDonatello &
2839+
purple & bo staff \\\\\n\\bottomrule\n\\end{tabular}\n'
28402840
"""
28412841
# Get defaults from the pandas config
28422842
if self.ndim == 1:

pandas/io/formats/format.py

+31-5
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,15 @@ def _get_formatted_index(self):
252252

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

258265
def to_string(self):
259266
series = self.tr_series
@@ -703,9 +710,15 @@ def _format_col(self, i):
703710
frame = self.tr_frame
704711
formatter = self._get_formatter(i)
705712
values_to_format = frame.iloc[:, i]._formatting_values()
713+
714+
if self.index:
715+
leading_space = None
716+
else:
717+
leading_space = False
706718
return format_array(values_to_format, formatter,
707719
float_format=self.float_format, na_rep=self.na_rep,
708-
space=self.col_space, decimal=self.decimal)
720+
space=self.col_space, decimal=self.decimal,
721+
leading_space=leading_space)
709722

710723
def to_html(self, classes=None, notebook=False, border=None):
711724
"""
@@ -1080,7 +1093,11 @@ def format_values_with(float_format):
10801093
# The default is otherwise to use str instead of a formatting string
10811094
if self.float_format is None:
10821095
if self.fixed_width:
1083-
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,
10841101
digits=self.digits)
10851102
else:
10861103
float_format = self.float_format
@@ -1112,7 +1129,11 @@ def format_values_with(float_format):
11121129
(abs_vals > 0)).any()
11131130

11141131
if has_small_values or (too_long and has_large_values):
1115-
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,
11161137
digits=self.digits)
11171138
formatted_values = format_values_with(float_format)
11181139

@@ -1129,7 +1150,12 @@ def _format_strings(self):
11291150
class IntArrayFormatter(GenericArrayFormatter):
11301151

11311152
def _format_strings(self):
1132-
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))
11331159
fmt_values = [formatter(x) for x in self.values]
11341160
return fmt_values
11351161

pandas/tests/io/formats/test_format.py

+33-8
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):
@@ -2777,3 +2777,28 @@ def test_format_percentiles():
27772777
fmt.format_percentiles([2, 0.1, 0.5])
27782778
with pytest.raises(ValueError, match=msg):
27792779
fmt.format_percentiles([0.1, 0.5, 'a'])
2780+
2781+
2782+
@pytest.mark.parametrize("input_array, expected", [
2783+
("a", "a"),
2784+
(["a", "b"], "a\nb"),
2785+
([1, "a"], "1\na"),
2786+
(1, "1"),
2787+
([0, -1], " 0\n-1"),
2788+
(1.0, '1.0')
2789+
])
2790+
def test_format_remove_leading_space_series(input_array, expected):
2791+
# GH: 24980
2792+
s = pd.Series(input_array).to_string(index=False)
2793+
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_format_remove_leading_space_dataframe(input_array, expected):
2802+
# GH: 24980
2803+
df = pd.DataFrame(input_array).to_string(index=False)
2804+
assert df == expected

pandas/tests/io/formats/test_to_latex.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ def test_to_latex(self, frame):
6464
withoutindex_result = df.to_latex(index=False)
6565
withoutindex_expected = r"""\begin{tabular}{rl}
6666
\toprule
67-
a & b \\
67+
a & b \\
6868
\midrule
69-
1 & b1 \\
70-
2 & b2 \\
69+
1 & b1 \\
70+
2 & b2 \\
7171
\bottomrule
7272
\end{tabular}
7373
"""
@@ -423,7 +423,7 @@ def test_to_latex_longtable(self, frame):
423423
withoutindex_result = df.to_latex(index=False, longtable=True)
424424
withoutindex_expected = r"""\begin{longtable}{rl}
425425
\toprule
426-
a & b \\
426+
a & b \\
427427
\midrule
428428
\endhead
429429
\midrule
@@ -433,8 +433,8 @@ def test_to_latex_longtable(self, frame):
433433
434434
\bottomrule
435435
\endlastfoot
436-
1 & b1 \\
437-
2 & b2 \\
436+
1 & b1 \\
437+
2 & b2 \\
438438
\end{longtable}
439439
"""
440440

@@ -490,8 +490,8 @@ def test_to_latex_no_header(self):
490490
withoutindex_result = df.to_latex(index=False, header=False)
491491
withoutindex_expected = r"""\begin{tabular}{rl}
492492
\toprule
493-
1 & b1 \\
494-
2 & b2 \\
493+
1 & b1 \\
494+
2 & b2 \\
495495
\bottomrule
496496
\end{tabular}
497497
"""
@@ -517,10 +517,10 @@ def test_to_latex_specified_header(self):
517517
withoutindex_result = df.to_latex(header=['AA', 'BB'], index=False)
518518
withoutindex_expected = r"""\begin{tabular}{rl}
519519
\toprule
520-
AA & BB \\
520+
AA & BB \\
521521
\midrule
522-
1 & b1 \\
523-
2 & b2 \\
522+
1 & b1 \\
523+
2 & b2 \\
524524
\bottomrule
525525
\end{tabular}
526526
"""

0 commit comments

Comments
 (0)