Skip to content

Commit 40fbf53

Browse files
cruzzoeroberthdevries
authored andcommitted
Deprecate using xlrd engine
1 parent bf12604 commit 40fbf53

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

doc/source/whatsnew/v1.1.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,8 @@ Deprecations
818818
- :meth:`util.testing.assert_almost_equal` now accepts both relative and absolute
819819
precision through the ``rtol``, and ``atol`` parameters, thus deprecating the
820820
``check_less_precise`` parameter. (:issue:`13357`).
821+
-:func:`read_excel` engine argument "xlrd" will no longer be the default engine and
822+
will be replaced by "openpyxl" in a future version (:issue:`28547`).
821823

822824
.. ---------------------------------------------------------------------------
823825

pandas/io/excel/_base.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
from textwrap import fill
66
from typing import Union
7+
import warnings
78

89
from pandas._config import config
910

@@ -810,8 +811,7 @@ def _is_ods_stream(stream: Union[BufferedIOBase, RawIOBase]) -> bool:
810811
class ExcelFile:
811812
"""
812813
Class for parsing tabular excel sheets into DataFrame objects.
813-
814-
Uses xlrd engine by default. See read_excel for more documentation
814+
Uses xlrd, openpyxl or odf. See read_excel for more documentation
815815
816816
Parameters
817817
----------
@@ -852,6 +852,13 @@ def __init__(self, path_or_buffer, engine=None):
852852
ext = os.path.splitext(str(path_or_buffer))[-1]
853853
if ext == ".ods":
854854
engine = "odf"
855+
856+
if engine == "xlrd":
857+
warnings.warn(
858+
'The Excel reader engine will default to "openpyxl" in the future. '
859+
'Specify engine="openpyxl" to suppress this warning.',
860+
FutureWarning,
861+
)
855862
if engine not in self._engines:
856863
raise ValueError(f"Unknown engine: {engine}")
857864

pandas/tests/io/excel/test_readers.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,10 @@ def test_read_excel_squeeze(self, read_ext):
962962
expected = pd.Series([1, 2, 3], name="a")
963963
tm.assert_series_equal(actual, expected)
964964

965-
def test_deprecated_kwargs(self, read_ext):
965+
def test_deprecated_kwargs(self, engine, read_ext):
966+
if read_ext in [".xls", ".xlsx", ".xlsm"]:
967+
pytest.skip("xlrd produces a DeprecatedWarning on use")
968+
966969
with tm.assert_produces_warning(FutureWarning, raise_on_extra_warnings=False):
967970
pd.read_excel("test1" + read_ext, "Sheet1", 0)
968971

pandas/tests/io/excel/test_xlrd.py

+24
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import warnings
2+
13
import pytest
24

35
import pandas as pd
@@ -17,6 +19,9 @@ def skip_ods_and_xlsb_files(read_ext):
1719
pytest.skip("Not valid for xlrd")
1820

1921

22+
@pytest.mark.filterwarnings(
23+
"ignore:The Excel reader engine will default to:FutureWarning"
24+
)
2025
def test_read_xlrd_book(read_ext, frame):
2126
df = frame
2227

@@ -36,8 +41,27 @@ def test_read_xlrd_book(read_ext, frame):
3641

3742

3843
# TODO: test for openpyxl as well
44+
@pytest.mark.filterwarnings(
45+
"ignore:The Excel reader engine will default to:FutureWarning"
46+
)
3947
def test_excel_table_sheet_by_index(datapath, read_ext):
4048
path = datapath("io", "data", "excel", f"test1{read_ext}")
4149
with pd.ExcelFile(path) as excel:
4250
with pytest.raises(xlrd.XLRDError):
4351
pd.read_excel(excel, sheet_name="asdf")
52+
53+
54+
# See issue #29375
55+
def test_excel_file_warning_with_default_engine(datapath):
56+
path = datapath("io", "data", "excel", "test1.xls")
57+
with warnings.catch_warnings(record=True) as w:
58+
pd.ExcelFile(path)
59+
assert 'default to "openpyxl" in the future.' in str(w[-1].message)
60+
61+
62+
# See issue #29375
63+
def test_read_excel_warning_with_default_engine(tmpdir, datapath):
64+
path = datapath("io", "data", "excel", "test1.xls")
65+
with warnings.catch_warnings(record=True) as w:
66+
pd.read_excel(path, "Sheet1")
67+
assert 'default to "openpyxl" in the future.' in str(w[-1].message)

0 commit comments

Comments
 (0)