From 6d43e650d14227bf1ca69873b0f8d1d830b9f436 Mon Sep 17 00:00:00 2001 From: Mike Ronayne Date: Mon, 12 Apr 2021 13:05:27 -0700 Subject: [PATCH 1/4] _insert_dot_separator_horizontal now takes self.fmt.index into account --- pandas/io/formats/string.py | 5 ++++- pandas/tests/io/formats/test_to_string.py | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/pandas/io/formats/string.py b/pandas/io/formats/string.py index 84333cfc441b2..65b915f7db47d 100644 --- a/pandas/io/formats/string.py +++ b/pandas/io/formats/string.py @@ -77,7 +77,10 @@ def _insert_dot_separators(self, strcols: List[List[str]]) -> List[List[str]]: def _insert_dot_separator_horizontal( self, strcols: List[List[str]], index_length: int ) -> List[List[str]]: - strcols.insert(self.fmt.tr_col_num + 1, [" ..."] * index_length) + tr_col_num = ( + self.fmt.tr_col_num + 1 if self.fmt.index else self.fmt.tr_col_num + ) + strcols.insert(tr_col_num, [" ..."] * index_length) return strcols def _insert_dot_separator_vertical( diff --git a/pandas/tests/io/formats/test_to_string.py b/pandas/tests/io/formats/test_to_string.py index 551734f343dfa..25eb6b620d648 100644 --- a/pandas/tests/io/formats/test_to_string.py +++ b/pandas/tests/io/formats/test_to_string.py @@ -105,6 +105,23 @@ def test_format_remove_leading_space_dataframe(input_array, expected): df = DataFrame(input_array).to_string(index=False) assert df == expected +def test_truncation_col_placement_no_index(): + df = DataFrame([[0]*11] * 2) + + # Even max_cols. + assert df.to_string(index=False, max_cols=10)).split("\n") == [ + " 0 1 2 3 4 ... 6 7 8 9 10", + " 0 0 0 0 0 ... 0 0 0 0 0", + " 0 0 0 0 0 ... 0 0 0 0 0", + ] + + # Odd max_cols. + assert df.to_string(index=False, max_cols=9)).split("\n") == [ + " 0 1 2 3 4 ... 7 8 9 10", + " 0 0 0 0 0 ... 0 0 0 0", + " 0 0 0 0 0 ... 0 0 0 0", + ] + def test_to_string_unicode_columns(float_frame): df = DataFrame({"\u03c3": np.arange(10.0)}) From b294317ddc1a9e31d1927430b6a64c8a3b029a09 Mon Sep 17 00:00:00 2001 From: Mike Ronayne Date: Mon, 12 Apr 2021 13:53:46 -0700 Subject: [PATCH 2/4] Fixed test errors --- pandas/tests/io/formats/test_to_string.py | 47 +++++++++++++++-------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/pandas/tests/io/formats/test_to_string.py b/pandas/tests/io/formats/test_to_string.py index 25eb6b620d648..2d0098de448c0 100644 --- a/pandas/tests/io/formats/test_to_string.py +++ b/pandas/tests/io/formats/test_to_string.py @@ -105,22 +105,39 @@ def test_format_remove_leading_space_dataframe(input_array, expected): df = DataFrame(input_array).to_string(index=False) assert df == expected -def test_truncation_col_placement_no_index(): - df = DataFrame([[0]*11] * 2) - - # Even max_cols. - assert df.to_string(index=False, max_cols=10)).split("\n") == [ - " 0 1 2 3 4 ... 6 7 8 9 10", - " 0 0 0 0 0 ... 0 0 0 0 0", - " 0 0 0 0 0 ... 0 0 0 0 0", - ] - # Odd max_cols. - assert df.to_string(index=False, max_cols=9)).split("\n") == [ - " 0 1 2 3 4 ... 7 8 9 10", - " 0 0 0 0 0 ... 0 0 0 0", - " 0 0 0 0 0 ... 0 0 0 0", - ] +@pytest.mark.parametrize( + "max_cols, expected", + [ + ( + 10, + [ + " 0 1 2 3 4 ... 6 7 8 9 10", + " 0 0 0 0 0 ... 0 0 0 0 0", + " 0 0 0 0 0 ... 0 0 0 0 0", + ], + ), + ( + 9, + [ + " 0 1 2 3 ... 7 8 9 10", + " 0 0 0 0 ... 0 0 0 0", + " 0 0 0 0 ... 0 0 0 0", + ], + ), + ( + 1, + [ + " 0 ...", + " 0 ...", + " 0 ...", + ], + ), + ], +) +def test_truncation_col_placement_no_index(max_cols, expected): + df = DataFrame([[0]*11] * 2) + assert df.to_string(index=False, max_cols=max_cols).split("\n") == expected def test_to_string_unicode_columns(float_frame): From be0df57736ad0d486450244994ff4dea662ad104 Mon Sep 17 00:00:00 2001 From: Mike Ronayne Date: Mon, 12 Apr 2021 15:47:37 -0700 Subject: [PATCH 3/4] Added whatsnew. Fixed style issues. --- doc/source/whatsnew/v1.2.5.rst | 3 +-- pandas/io/formats/string.py | 4 +--- pandas/tests/io/formats/test_to_string.py | 2 +- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/doc/source/whatsnew/v1.2.5.rst b/doc/source/whatsnew/v1.2.5.rst index cdfc2e5686b91..8c18b821ff637 100644 --- a/doc/source/whatsnew/v1.2.5.rst +++ b/doc/source/whatsnew/v1.2.5.rst @@ -25,8 +25,7 @@ Fixed regressions Bug fixes ~~~~~~~~~ -- -- +- Fixed bug in :meth:`DataFrame.to_string` where ``index=False`` resulted in a misplaced truncation column (:issue:`40907`) .. --------------------------------------------------------------------------- diff --git a/pandas/io/formats/string.py b/pandas/io/formats/string.py index 65b915f7db47d..de53646b5f95f 100644 --- a/pandas/io/formats/string.py +++ b/pandas/io/formats/string.py @@ -77,9 +77,7 @@ def _insert_dot_separators(self, strcols: List[List[str]]) -> List[List[str]]: def _insert_dot_separator_horizontal( self, strcols: List[List[str]], index_length: int ) -> List[List[str]]: - tr_col_num = ( - self.fmt.tr_col_num + 1 if self.fmt.index else self.fmt.tr_col_num - ) + tr_col_num = self.fmt.tr_col_num + 1 if self.fmt.index else self.fmt.tr_col_num strcols.insert(tr_col_num, [" ..."] * index_length) return strcols diff --git a/pandas/tests/io/formats/test_to_string.py b/pandas/tests/io/formats/test_to_string.py index 2d0098de448c0..f9b3cac3527ef 100644 --- a/pandas/tests/io/formats/test_to_string.py +++ b/pandas/tests/io/formats/test_to_string.py @@ -136,7 +136,7 @@ def test_format_remove_leading_space_dataframe(input_array, expected): ], ) def test_truncation_col_placement_no_index(max_cols, expected): - df = DataFrame([[0]*11] * 2) + df = DataFrame([[0] * 11] * 2) assert df.to_string(index=False, max_cols=max_cols).split("\n") == expected From e0cd2da02545c75fc8e0eed5fbdc8108519fb4dd Mon Sep 17 00:00:00 2001 From: Mike Ronayne Date: Tue, 13 Apr 2021 07:43:59 -0700 Subject: [PATCH 4/4] Moved whatsnew addition to proper version --- doc/source/whatsnew/v1.2.5.rst | 3 ++- doc/source/whatsnew/v1.3.0.rst | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.2.5.rst b/doc/source/whatsnew/v1.2.5.rst index 8c18b821ff637..cdfc2e5686b91 100644 --- a/doc/source/whatsnew/v1.2.5.rst +++ b/doc/source/whatsnew/v1.2.5.rst @@ -25,7 +25,8 @@ Fixed regressions Bug fixes ~~~~~~~~~ -- Fixed bug in :meth:`DataFrame.to_string` where ``index=False`` resulted in a misplaced truncation column (:issue:`40907`) +- +- .. --------------------------------------------------------------------------- diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 0ec9758477eba..fcfac2d2d63f1 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -740,6 +740,7 @@ I/O - Bug in :func:`read_hdf` returning unexpected records when filtering on categorical string columns using ``where`` parameter (:issue:`39189`) - Bug in :func:`read_sas` raising ``ValueError`` when ``datetimes`` were null (:issue:`39725`) - Bug in :func:`read_excel` dropping empty values from single-column spreadsheets (:issue:`39808`) +- Bug in :meth:`DataFrame.to_string` misplacing the truncation column when ``index=False`` (:issue:`40907`) Period ^^^^^^