From 2d8a66454e0c5b2bd39616ac68047083bc3d23b0 Mon Sep 17 00:00:00 2001 From: Marco Gorelli Date: Tue, 20 Oct 2020 01:51:56 +0100 Subject: [PATCH] Backport PR #37256: BUG: with integer column labels, .info() throws KeyError --- doc/source/whatsnew/v1.1.4.rst | 1 + pandas/io/formats/info.py | 4 ++-- pandas/tests/io/formats/test_info.py | 22 ++++++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.1.4.rst b/doc/source/whatsnew/v1.1.4.rst index ad59711b90f6e..7dd660374a6fc 100644 --- a/doc/source/whatsnew/v1.1.4.rst +++ b/doc/source/whatsnew/v1.1.4.rst @@ -30,6 +30,7 @@ Bug fixes - Bug causing ``groupby(...).sum()`` and similar to not preserve metadata (:issue:`29442`) - Bug in :meth:`Series.isin` and :meth:`DataFrame.isin` raising a ``ValueError`` when the target was read-only (:issue:`37174`) - Bug in :meth:`GroupBy.fillna` that introduced a performance regression after 1.0.5 (:issue:`36757`) +- Bug in :meth:`DataFrame.info` was raising a ``KeyError`` when the DataFrame has integer column names (:issue:`37245`) .. --------------------------------------------------------------------------- diff --git a/pandas/io/formats/info.py b/pandas/io/formats/info.py index 7a53b46a4ac0f..db6704f7a96a4 100644 --- a/pandas/io/formats/info.py +++ b/pandas/io/formats/info.py @@ -332,13 +332,13 @@ def _verbose_repr( ) for i, col in enumerate(ids): - dtype = dtypes[i] + dtype = dtypes.iloc[i] col = pprint_thing(col) line_no = _put_str(f" {i}", space_num) count = "" if show_counts: - count = counts[i] + count = counts.iloc[i] lines.append( line_no diff --git a/pandas/tests/io/formats/test_info.py b/pandas/tests/io/formats/test_info.py index 877bd1650ae60..5ef2ce9c47236 100644 --- a/pandas/tests/io/formats/test_info.py +++ b/pandas/tests/io/formats/test_info.py @@ -403,3 +403,25 @@ def test_info_categorical(): buf = StringIO() df.info(buf=buf) + + +def test_info_int_columns(): + # GH#37245 + df = DataFrame({1: [1, 2], 2: [2, 3]}, index=["A", "B"]) + buf = StringIO() + df.info(null_counts=True, buf=buf) + result = buf.getvalue() + expected = textwrap.dedent( + """\ + + Index: 2 entries, A to B + Data columns (total 2 columns): + # Column Non-Null Count Dtype + --- ------ -------------- ----- + 0 1 2 non-null int64 + 1 2 2 non-null int64 + dtypes: int64(2) + memory usage: 48.0+ bytes + """ + ) + assert result == expected