Skip to content

Commit 6efe8d8

Browse files
MarcoGorelliKevin D Smith
authored and
Kevin D Smith
committed
BUG: with integer column labels, .info() throws KeyError (pandas-dev#37256)
1 parent 69097c7 commit 6efe8d8

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

doc/source/whatsnew/v1.1.4.rst

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Bug fixes
3030
- Bug causing ``groupby(...).sum()`` and similar to not preserve metadata (:issue:`29442`)
3131
- Bug in :meth:`Series.isin` and :meth:`DataFrame.isin` raising a ``ValueError`` when the target was read-only (:issue:`37174`)
3232
- Bug in :meth:`GroupBy.fillna` that introduced a performance regression after 1.0.5 (:issue:`36757`)
33+
- Bug in :meth:`DataFrame.info` was raising a ``KeyError`` when the DataFrame has integer column names (:issue:`37245`)
3334

3435
.. ---------------------------------------------------------------------------
3536

pandas/io/formats/info.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -340,13 +340,13 @@ def _verbose_repr(
340340
lines.append(top_separator)
341341

342342
for i, col in enumerate(ids):
343-
dtype = dtypes[i]
343+
dtype = dtypes.iloc[i]
344344
col = pprint_thing(col)
345345

346346
line_no = _put_str(f" {i}", space_num)
347347
count = ""
348348
if show_counts:
349-
count = counts[i]
349+
count = counts.iloc[i]
350350

351351
lines.append(
352352
line_no

pandas/tests/io/formats/test_info.py

+22
Original file line numberDiff line numberDiff line change
@@ -459,3 +459,25 @@ def test_info_categorical():
459459

460460
buf = StringIO()
461461
df.info(buf=buf)
462+
463+
464+
def test_info_int_columns():
465+
# GH#37245
466+
df = DataFrame({1: [1, 2], 2: [2, 3]}, index=["A", "B"])
467+
buf = StringIO()
468+
df.info(null_counts=True, buf=buf)
469+
result = buf.getvalue()
470+
expected = textwrap.dedent(
471+
"""\
472+
<class 'pandas.core.frame.DataFrame'>
473+
Index: 2 entries, A to B
474+
Data columns (total 2 columns):
475+
# Column Non-Null Count Dtype
476+
--- ------ -------------- -----
477+
0 1 2 non-null int64
478+
1 2 2 non-null int64
479+
dtypes: int64(2)
480+
memory usage: 48.0+ bytes
481+
"""
482+
)
483+
assert result == expected

0 commit comments

Comments
 (0)