Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions doc/source/whatsnew/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,7 @@ MultiIndex

I/O
^^^
-
-
- Bug in :func:`read_excel`, with ``engine="xlrd"`` (``xls`` files) erroring when file contains NaNs/Infs (:issue:`54564`)

Period
^^^^^^
Expand Down
9 changes: 6 additions & 3 deletions pandas/io/excel/_xlrd.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from datetime import time
import math
from typing import TYPE_CHECKING

import numpy as np
Expand Down Expand Up @@ -120,9 +121,11 @@ def _parse_cell(cell_contents, cell_typ):
elif cell_typ == XL_CELL_NUMBER:
# GH5394 - Excel 'numbers' are always floats
# it's a minimal perf hit and less surprising
val = int(cell_contents)
if val == cell_contents:
cell_contents = val
if math.isfinite(cell_contents):
# GH54564 - don't attempt to convert NaN/Inf
val = int(cell_contents)
if val == cell_contents:
cell_contents = val
return cell_contents

data = []
Expand Down
Binary file added pandas/tests/io/data/excel/test6.xls
Binary file not shown.
12 changes: 12 additions & 0 deletions pandas/tests/io/excel/test_xlrd.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import io

import numpy as np
import pytest

import pandas as pd
Expand Down Expand Up @@ -44,6 +45,17 @@ def test_read_xlsx_fails(datapath):
pd.read_excel(path, engine="xlrd")


def test_nan_in_xls(datapath):
# GH 54564
path = datapath("io", "data", "excel", "test6.xls")

expected = pd.DataFrame({0: np.r_[0, 2], 1: np.r_[1, np.nan]})

result = pd.read_excel(path, header=None)

tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize(
"file_header",
[
Expand Down