Skip to content

Commit 101aa97

Browse files
Revert all changes related to switching to openpyxl as the default
1 parent 3a76a36 commit 101aa97

File tree

4 files changed

+24
-40
lines changed

4 files changed

+24
-40
lines changed

doc/source/whatsnew/v1.2.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ Deprecations
144144
~~~~~~~~~~~~
145145
- Deprecated parameter ``inplace`` in :meth:`MultiIndex.set_codes` and :meth:`MultiIndex.set_levels` (:issue:`35626`)
146146
- Deprecated parameter ``dtype`` in :~meth:`Index.copy` on method all index classes. Use the :meth:`Index.astype` method instead for changing dtype(:issue:`35853`)
147-
- :func:`read_excel` default engine "xlrd" is replaced by "openpyxl" because "xlrd" is deprecated (:issue:`28547`).
147+
- :func:`read_excel` "xlrd" engine is deprecated for all file types that can be handled by "openpyxl" because "xlrd" is no longer maintained (:issue:`28547`).
148148
-
149149
-
150150

pandas/io/excel/_base.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,8 @@ def _is_ods_stream(stream: Union[BufferedIOBase, RawIOBase]) -> bool:
826826
class ExcelFile:
827827
"""
828828
Class for parsing tabular excel sheets into DataFrame objects.
829-
Uses xlrd, openpyxl or odf. See read_excel for more documentation
829+
830+
Uses xlrd engine by default. See read_excel for more documentation
830831
831832
Parameters
832833
----------
@@ -837,7 +838,7 @@ class ExcelFile:
837838
engine : str, default None
838839
If io is not a buffer or path, this must be set to identify io.
839840
Supported engines: ``xlrd``, ``openpyxl``, ``odf``, ``pyxlsb``,
840-
default ``openpyxl``, ``xlrd`` for .xls files, ``odf`` for .ods files.
841+
default ``xlrd`` for .xls* files, ``odf`` for .ods files.
841842
Engine compatibility :
842843
- ``xlrd`` supports most old/new Excel file formats.
843844
- ``openpyxl`` supports newer Excel file formats.
@@ -860,19 +861,20 @@ class ExcelFile:
860861
def __init__(
861862
self, path_or_buffer, engine=None, storage_options: StorageOptions = None
862863
):
864+
ext = None
865+
if not isinstance(path_or_buffer, (BufferedIOBase, RawIOBase)):
866+
ext = os.path.splitext(str(path_or_buffer))[-1][1:]
867+
863868
if engine is None:
864-
engine = "openpyxl"
869+
engine = "xlrd"
865870
if isinstance(path_or_buffer, (BufferedIOBase, RawIOBase)):
866871
if _is_ods_stream(path_or_buffer):
867872
engine = "odf"
868873
else:
869-
ext = os.path.splitext(str(path_or_buffer))[-1]
870-
if ext == ".ods":
874+
if ext == "ods":
871875
engine = "odf"
872-
elif ext == ".xls":
873-
engine = "xlrd"
874876

875-
elif engine == "xlrd":
877+
elif engine == "xlrd" and ext in ("xlsx", "xlsm"):
876878
warnings.warn(
877879
'The Excel reader engine "xlrd" is deprecated, use "openpyxl" instead. '
878880
'Specify engine="openpyxl" to suppress this warning.',

pandas/tests/io/excel/test_readers.py

+11-18
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
pytest.param(
3838
None,
3939
marks=[
40-
td.skip_if_no("openpyxl"),
41-
pytest.mark.filterwarnings("ignore:.*html argument"),
40+
td.skip_if_no("xlrd"),
41+
pytest.mark.filterwarnings("ignore:.*(tree\\.iter|html argument)"),
4242
],
4343
),
4444
pytest.param("pyxlsb", marks=td.skip_if_no("pyxlsb")),
@@ -54,8 +54,6 @@ def _is_valid_engine_ext_pair(engine, read_ext: str) -> bool:
5454
engine = engine.values[0]
5555
if engine == "openpyxl" and read_ext == ".xls":
5656
return False
57-
if engine is None and read_ext == ".xls":
58-
return False
5957
if engine == "odf" and read_ext != ".ods":
6058
return False
6159
if read_ext == ".ods" and engine != "odf":
@@ -564,7 +562,7 @@ def test_date_conversion_overflow(self, read_ext):
564562
columns=["DateColWithBigInt", "StringCol"],
565563
)
566564

567-
if pd.read_excel.keywords["engine"] in ["openpyxl", None]:
565+
if pd.read_excel.keywords["engine"] == "openpyxl":
568566
pytest.xfail("Maybe not supported by openpyxl")
569567

570568
result = pd.read_excel("testdateoverflow" + read_ext)
@@ -969,19 +967,6 @@ def test_no_header_with_list_index_col(self, read_ext):
969967
)
970968
tm.assert_frame_equal(expected, result)
971969

972-
def test_excel_high_surrogate(self, engine, read_ext):
973-
# GH 23809
974-
if read_ext != ".xlsx":
975-
pytest.skip("Test is only applicable to .xlsx file")
976-
if engine in ["openpyxl", None]:
977-
pytest.skip("Test does not work for openpyxl")
978-
979-
expected = pd.DataFrame(["\udc88"], columns=["Column1"])
980-
981-
# should not produce a segmentation violation
982-
actual = pd.read_excel("high_surrogate.xlsx")
983-
tm.assert_frame_equal(expected, actual)
984-
985970

986971
class TestExcelFileRead:
987972
@pytest.fixture(autouse=True)
@@ -1137,6 +1122,14 @@ def test_excel_read_binary(self, engine, read_ext):
11371122
actual = pd.read_excel(data, engine=engine)
11381123
tm.assert_frame_equal(expected, actual)
11391124

1125+
def test_excel_high_surrogate(self, engine):
1126+
# GH 23809
1127+
expected = pd.DataFrame(["\udc88"], columns=["Column1"])
1128+
1129+
# should not produce a segmentation violation
1130+
actual = pd.read_excel("high_surrogate.xlsx")
1131+
tm.assert_frame_equal(expected, actual)
1132+
11401133
@pytest.mark.parametrize("filename", ["df_empty.xlsx", "df_equals.xlsx"])
11411134
def test_header_with_index_col(self, engine, filename):
11421135
# GH 33476

pandas/tests/io/excel/test_writers.py

+2-13
Original file line numberDiff line numberDiff line change
@@ -351,16 +351,12 @@ def test_excel_sheet_by_name_raise(self, path, engine):
351351
msg = "sheet 0 not found"
352352
with pytest.raises(ValueError, match=msg):
353353
pd.read_excel(xl, "0")
354-
elif engine == "xlwt":
354+
else:
355355
import xlrd
356356

357357
msg = "No sheet named <'0'>"
358358
with pytest.raises(xlrd.XLRDError, match=msg):
359359
pd.read_excel(xl, sheet_name="0")
360-
else: # openpyxl
361-
msg = "Worksheet 0 does not exist."
362-
with pytest.raises(KeyError, match=msg):
363-
pd.read_excel(xl, sheet_name="0")
364360

365361
def test_excel_writer_context_manager(self, frame, path):
366362
with ExcelWriter(path) as writer:
@@ -1216,15 +1212,8 @@ def test_bytes_io(self, engine):
12161212
df.to_excel(writer)
12171213
writer.save()
12181214

1219-
if engine == "xlwt":
1220-
read_engine = "xlrd"
1221-
elif engine == "xlsxwriter":
1222-
read_engine = "openpyxl"
1223-
else:
1224-
read_engine = engine
1225-
12261215
bio.seek(0)
1227-
reread_df = pd.read_excel(bio, index_col=0, engine=read_engine)
1216+
reread_df = pd.read_excel(bio, index_col=0)
12281217
tm.assert_frame_equal(df, reread_df)
12291218

12301219
def test_write_lists_dict(self, path):

0 commit comments

Comments
 (0)