From 2cb670951c9d4471950c5f898322492e7ef58b55 Mon Sep 17 00:00:00 2001 From: Mauricio Jr Date: Thu, 1 Nov 2018 17:53:23 +0100 Subject: [PATCH 1/2] feat: Add support for encoding parameter on read_excel --- pandas/io/excel.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/pandas/io/excel.py b/pandas/io/excel.py index 7a7b801f4ba4a..f93ae68f93590 100644 --- a/pandas/io/excel.py +++ b/pandas/io/excel.py @@ -303,7 +303,7 @@ def read_excel(io, skipfooter=0, convert_float=True, **kwds): - + encoding = kwds.get("encoding") # Can't use _deprecate_kwarg since sheetname=None has a special meaning if is_integer(sheet_name) and sheet_name == 0 and 'sheetname' in kwds: warnings.warn("The `sheetname` keyword is deprecated, use " @@ -315,7 +315,7 @@ def read_excel(io, "`sheet`") if not isinstance(io, ExcelFile): - io = ExcelFile(io, engine=engine) + io = ExcelFile(io, engine=engine, encoding=encoding) return io.parse( sheet_name=sheet_name, @@ -358,7 +358,7 @@ class ExcelFile(object): def __init__(self, io, **kwds): err_msg = "Install xlrd >= 0.9.0 for Excel support" - + encoding = kwds.get("encoding") try: import xlrd except ImportError: @@ -400,9 +400,15 @@ def __init__(self, io, **kwds): pass data = io.read() - self.book = xlrd.open_workbook(file_contents=data) + if encoding: + self.book = xlrd.open_workbook(file_contents=data, encoding_override=encoding) + else: + self.book = xlrd.open_workbook(file_contents=data) elif isinstance(self._io, compat.string_types): - self.book = xlrd.open_workbook(self._io) + if encoding: + self.book = xlrd.open_workbook(self._io, encoding_override=encoding) + else: + self.book = xlrd.open_workbook(self._io) else: raise ValueError('Must explicitly set engine if not passing in' ' buffer or path for io.') From 077efe771b1e8a7af08579e012087a69e0bb1780 Mon Sep 17 00:00:00 2001 From: Mauricio Jr Date: Mon, 5 Nov 2018 15:00:15 +0100 Subject: [PATCH 2/2] enhancement: Wrapped encoding into kwargs for xlrd lib (closes #23444) --- pandas/io/excel.py | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/pandas/io/excel.py b/pandas/io/excel.py index f93ae68f93590..9837938ce374b 100644 --- a/pandas/io/excel.py +++ b/pandas/io/excel.py @@ -303,7 +303,6 @@ def read_excel(io, skipfooter=0, convert_float=True, **kwds): - encoding = kwds.get("encoding") # Can't use _deprecate_kwarg since sheetname=None has a special meaning if is_integer(sheet_name) and sheet_name == 0 and 'sheetname' in kwds: warnings.warn("The `sheetname` keyword is deprecated, use " @@ -315,7 +314,7 @@ def read_excel(io, "`sheet`") if not isinstance(io, ExcelFile): - io = ExcelFile(io, engine=engine, encoding=encoding) + io = ExcelFile(io, engine=engine, **kwds) return io.parse( sheet_name=sheet_name, @@ -356,9 +355,8 @@ class ExcelFile(object): """ def __init__(self, io, **kwds): - + kwargs = {'encoding_override': kwds.get("encoding")} err_msg = "Install xlrd >= 0.9.0 for Excel support" - encoding = kwds.get("encoding") try: import xlrd except ImportError: @@ -400,15 +398,9 @@ def __init__(self, io, **kwds): pass data = io.read() - if encoding: - self.book = xlrd.open_workbook(file_contents=data, encoding_override=encoding) - else: - self.book = xlrd.open_workbook(file_contents=data) + self.book = xlrd.open_workbook(file_contents=data, **kwargs) elif isinstance(self._io, compat.string_types): - if encoding: - self.book = xlrd.open_workbook(self._io, encoding_override=encoding) - else: - self.book = xlrd.open_workbook(self._io) + self.book = xlrd.open_workbook(self._io, **kwargs) else: raise ValueError('Must explicitly set engine if not passing in' ' buffer or path for io.')