Skip to content

Commit b04dad7

Browse files
authored
BUG: to_string truncation row with index=False (#41223)
1 parent 6dff995 commit b04dad7

File tree

3 files changed

+42
-24
lines changed

3 files changed

+42
-24
lines changed

doc/source/whatsnew/v1.3.0.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,8 @@ I/O
871871
- Bug in :func:`read_excel` dropping empty values from single-column spreadsheets (:issue:`39808`)
872872
- Bug in :func:`read_excel` loading trailing empty rows/columns for some filetypes (:issue:`41167`)
873873
- Bug in :func:`read_excel` raising ``AttributeError`` with ``MultiIndex`` header followed by two empty rows and no index, and bug affecting :func:`read_excel`, :func:`read_csv`, :func:`read_table`, :func:`read_fwf`, and :func:`read_clipboard` where one blank row after a ``MultiIndex`` header with no index would be dropped (:issue:`40442`)
874-
- Bug in :meth:`DataFrame.to_string` misplacing the truncation column when ``index=False`` (:issue:`40907`)
874+
- Bug in :meth:`DataFrame.to_string` misplacing the truncation column when ``index=False`` (:issue:`40904`)
875+
- Bug in :meth:`DataFrame.to_string` adding an extra dot and misaligning the truncation row when ``index=False`` (:issue:`40904`)
875876
- Bug in :func:`read_orc` always raising ``AttributeError`` (:issue:`40918`)
876877
- Bug in :func:`read_csv` and :func:`read_table` silently ignoring ``prefix`` if ``names`` and ``prefix`` are defined, now raising ``ValueError`` (:issue:`39123`)
877878
- Bug in :func:`read_csv` and :func:`read_excel` not respecting dtype for duplicated column name when ``mangle_dupe_cols`` is set to ``True`` (:issue:`35211`)

pandas/io/formats/string.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,14 @@ def _insert_dot_separators(self, strcols: List[List[str]]) -> List[List[str]]:
7474

7575
return strcols
7676

77+
@property
78+
def _adjusted_tr_col_num(self) -> int:
79+
return self.fmt.tr_col_num + 1 if self.fmt.index else self.fmt.tr_col_num
80+
7781
def _insert_dot_separator_horizontal(
7882
self, strcols: List[List[str]], index_length: int
7983
) -> List[List[str]]:
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)
84+
strcols.insert(self._adjusted_tr_col_num, [" ..."] * index_length)
8285
return strcols
8386

8487
def _insert_dot_separator_vertical(
@@ -90,7 +93,7 @@ def _insert_dot_separator_vertical(
9093
cwidth = self.adj.len(col[row_num])
9194

9295
if self.fmt.is_truncated_horizontally:
93-
is_dot_col = ix == self.fmt.tr_col_num + 1
96+
is_dot_col = ix == self._adjusted_tr_col_num
9497
else:
9598
is_dot_col = False
9699

@@ -99,7 +102,7 @@ def _insert_dot_separator_vertical(
99102
else:
100103
dots = ".."
101104

102-
if ix == 0:
105+
if ix == 0 and self.fmt.index:
103106
dot_mode = "left"
104107
elif is_dot_col:
105108
cwidth = 4

pandas/tests/io/formats/test_to_string.py

+33-19
Original file line numberDiff line numberDiff line change
@@ -107,37 +107,51 @@ def test_format_remove_leading_space_dataframe(input_array, expected):
107107

108108

109109
@pytest.mark.parametrize(
110-
"max_cols, expected",
110+
"max_cols, max_rows, expected",
111111
[
112112
(
113113
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-
],
114+
None,
115+
" 0 1 2 3 4 ... 6 7 8 9 10\n"
116+
" 0 0 0 0 0 ... 0 0 0 0 0\n"
117+
" 0 0 0 0 0 ... 0 0 0 0 0\n"
118+
" 0 0 0 0 0 ... 0 0 0 0 0\n"
119+
" 0 0 0 0 0 ... 0 0 0 0 0",
120+
),
121+
(
122+
None,
123+
2,
124+
" 0 1 2 3 4 5 6 7 8 9 10\n"
125+
" 0 0 0 0 0 0 0 0 0 0 0\n"
126+
" .. .. .. .. .. .. .. .. .. .. ..\n"
127+
" 0 0 0 0 0 0 0 0 0 0 0",
128+
),
129+
(
130+
10,
131+
2,
132+
" 0 1 2 3 4 ... 6 7 8 9 10\n"
133+
" 0 0 0 0 0 ... 0 0 0 0 0\n"
134+
" .. .. .. .. .. ... .. .. .. .. ..\n"
135+
" 0 0 0 0 0 ... 0 0 0 0 0",
119136
),
120137
(
121138
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-
],
139+
2,
140+
" 0 1 2 3 ... 7 8 9 10\n"
141+
" 0 0 0 0 ... 0 0 0 0\n"
142+
" .. .. .. .. ... .. .. .. ..\n"
143+
" 0 0 0 0 ... 0 0 0 0",
127144
),
128145
(
129146
1,
130-
[
131-
" 0 ...",
132-
" 0 ...",
133-
" 0 ...",
134-
],
147+
1,
148+
" 0 ...\n 0 ...\n.. ...",
135149
),
136150
],
137151
)
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
152+
def test_truncation_no_index(max_cols, max_rows, expected):
153+
df = DataFrame([[0] * 11] * 4)
154+
assert df.to_string(index=False, max_cols=max_cols, max_rows=max_rows) == expected
141155

142156

143157
def test_to_string_unicode_columns(float_frame):

0 commit comments

Comments
 (0)