From f5372a7bafb6b848bb07c93447258816413361d1 Mon Sep 17 00:00:00 2001 From: Ezra Brauner Date: Thu, 20 Jan 2022 10:52:16 -0500 Subject: [PATCH 1/3] Added newline, info() for empty dataframe --- pandas/io/formats/info.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/io/formats/info.py b/pandas/io/formats/info.py index 504b2d632860f..dbdb1b1b96984 100644 --- a/pandas/io/formats/info.py +++ b/pandas/io/formats/info.py @@ -812,7 +812,7 @@ def _fill_empty_info(self) -> None: """Add lines to the info table, pertaining to empty dataframe.""" self.add_object_type_line() self.add_index_range_line() - self._lines.append(f"Empty {type(self.data).__name__}") + self._lines.append(f"Empty {type(self.data).__name__}\n") @abstractmethod def _fill_non_empty_info(self) -> None: From 1bc282036da6cb3c0f767ecdd8e9ee4d20003c18 Mon Sep 17 00:00:00 2001 From: Ezra Brauner Date: Thu, 20 Jan 2022 11:51:19 -0500 Subject: [PATCH 2/3] Added test --- pandas/tests/io/formats/test_info.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pandas/tests/io/formats/test_info.py b/pandas/tests/io/formats/test_info.py index 5522631d222e1..b9647a1f15f06 100644 --- a/pandas/tests/io/formats/test_info.py +++ b/pandas/tests/io/formats/test_info.py @@ -490,3 +490,16 @@ def test_info_int_columns(): """ ) assert result == expected + + +def test_empty_df_newline(): + # GH#45494 + df = DataFrame() + buf = StringIO() + df.info(buf=buf) + out = buf.getvalue() + + assert ( + out + == "\nIndex: 0 entries\nEmpty DataFrame\n" + ) From beecba7842f3735ca0b260a1aea51a4ebaba140c Mon Sep 17 00:00:00 2001 From: Ezra Brauner Date: Thu, 20 Jan 2022 12:42:31 -0500 Subject: [PATCH 3/3] Test change and whatsnew --- doc/source/whatsnew/v1.5.0.rst | 2 +- pandas/tests/io/formats/test_info.py | 16 ++-------------- 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/doc/source/whatsnew/v1.5.0.rst b/doc/source/whatsnew/v1.5.0.rst index 1ae76984484af..3a8bbf420da1a 100644 --- a/doc/source/whatsnew/v1.5.0.rst +++ b/doc/source/whatsnew/v1.5.0.rst @@ -227,7 +227,7 @@ MultiIndex I/O ^^^ - Bug in :meth:`DataFrame.to_stata` where no error is raised if the :class:`DataFrame` contains ``-np.inf`` (:issue:`45350`) -- +- Bug in :meth:`DataFrame.info` where a new line at the end of the output is omitted when called on an empty :class:`DataFrame` (:issue:`45494`) Period ^^^^^^ diff --git a/pandas/tests/io/formats/test_info.py b/pandas/tests/io/formats/test_info.py index b9647a1f15f06..6bcf971e5bb05 100644 --- a/pandas/tests/io/formats/test_info.py +++ b/pandas/tests/io/formats/test_info.py @@ -29,6 +29,7 @@ def duplicate_columns_frame(): def test_info_empty(): + # GH #45494 df = DataFrame() buf = StringIO() df.info(buf=buf) @@ -37,7 +38,7 @@ def test_info_empty(): """\ Index: 0 entries - Empty DataFrame""" + Empty DataFrame\n""" ) assert result == expected @@ -490,16 +491,3 @@ def test_info_int_columns(): """ ) assert result == expected - - -def test_empty_df_newline(): - # GH#45494 - df = DataFrame() - buf = StringIO() - df.info(buf=buf) - out = buf.getvalue() - - assert ( - out - == "\nIndex: 0 entries\nEmpty DataFrame\n" - )