Skip to content

Commit 526d52f

Browse files
authored
BUG: to_string truncation column with index=False (#40907)
1 parent 024159b commit 526d52f

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

doc/source/whatsnew/v1.3.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,7 @@ I/O
750750
- Bug in :func:`read_hdf` returning unexpected records when filtering on categorical string columns using ``where`` parameter (:issue:`39189`)
751751
- Bug in :func:`read_sas` raising ``ValueError`` when ``datetimes`` were null (:issue:`39725`)
752752
- Bug in :func:`read_excel` dropping empty values from single-column spreadsheets (:issue:`39808`)
753+
- Bug in :meth:`DataFrame.to_string` misplacing the truncation column when ``index=False`` (:issue:`40907`)
753754

754755
Period
755756
^^^^^^

pandas/io/formats/string.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ def _insert_dot_separators(self, strcols: List[List[str]]) -> List[List[str]]:
7777
def _insert_dot_separator_horizontal(
7878
self, strcols: List[List[str]], index_length: int
7979
) -> List[List[str]]:
80-
strcols.insert(self.fmt.tr_col_num + 1, [" ..."] * index_length)
80+
tr_col_num = self.fmt.tr_col_num + 1 if self.fmt.index else self.fmt.tr_col_num
81+
strcols.insert(tr_col_num, [" ..."] * index_length)
8182
return strcols
8283

8384
def _insert_dot_separator_vertical(

pandas/tests/io/formats/test_to_string.py

+34
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,40 @@ def test_format_remove_leading_space_dataframe(input_array, expected):
106106
assert df == expected
107107

108108

109+
@pytest.mark.parametrize(
110+
"max_cols, expected",
111+
[
112+
(
113+
10,
114+
[
115+
" 0 1 2 3 4 ... 6 7 8 9 10",
116+
" 0 0 0 0 0 ... 0 0 0 0 0",
117+
" 0 0 0 0 0 ... 0 0 0 0 0",
118+
],
119+
),
120+
(
121+
9,
122+
[
123+
" 0 1 2 3 ... 7 8 9 10",
124+
" 0 0 0 0 ... 0 0 0 0",
125+
" 0 0 0 0 ... 0 0 0 0",
126+
],
127+
),
128+
(
129+
1,
130+
[
131+
" 0 ...",
132+
" 0 ...",
133+
" 0 ...",
134+
],
135+
),
136+
],
137+
)
138+
def test_truncation_col_placement_no_index(max_cols, expected):
139+
df = DataFrame([[0] * 11] * 2)
140+
assert df.to_string(index=False, max_cols=max_cols).split("\n") == expected
141+
142+
109143
def test_to_string_unicode_columns(float_frame):
110144
df = DataFrame({"\u03c3": np.arange(10.0)})
111145

0 commit comments

Comments
 (0)