Skip to content

Commit 79d7caa

Browse files
Backport PR pandas-dev#37256: BUG: with integer column labels, .info() throws KeyError (pandas-dev#37285)
Co-authored-by: Marco Gorelli <[email protected]>
1 parent 5d310b8 commit 79d7caa

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
@@ -332,13 +332,13 @@ def _verbose_repr(
332332
)
333333

334334
for i, col in enumerate(ids):
335-
dtype = dtypes[i]
335+
dtype = dtypes.iloc[i]
336336
col = pprint_thing(col)
337337

338338
line_no = _put_str(f" {i}", space_num)
339339
count = ""
340340
if show_counts:
341-
count = counts[i]
341+
count = counts.iloc[i]
342342

343343
lines.append(
344344
line_no

pandas/tests/io/formats/test_info.py

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

404404
buf = StringIO()
405405
df.info(buf=buf)
406+
407+
408+
def test_info_int_columns():
409+
# GH#37245
410+
df = DataFrame({1: [1, 2], 2: [2, 3]}, index=["A", "B"])
411+
buf = StringIO()
412+
df.info(null_counts=True, buf=buf)
413+
result = buf.getvalue()
414+
expected = textwrap.dedent(
415+
"""\
416+
<class 'pandas.core.frame.DataFrame'>
417+
Index: 2 entries, A to B
418+
Data columns (total 2 columns):
419+
# Column Non-Null Count Dtype
420+
--- ------ -------------- -----
421+
0 1 2 non-null int64
422+
1 2 2 non-null int64
423+
dtypes: int64(2)
424+
memory usage: 48.0+ bytes
425+
"""
426+
)
427+
assert result == expected

0 commit comments

Comments
 (0)