Skip to content

pandas.read_excel() close the io stream #31726

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dujiaxin opened this issue Feb 5, 2020 · 1 comment
Closed

pandas.read_excel() close the io stream #31726

dujiaxin opened this issue Feb 5, 2020 · 1 comment

Comments

@dujiaxin
Copy link

dujiaxin commented Feb 5, 2020

Code Sample, a copy-pastable example if possible

with open(r'a.xlsx','rb') as f: # input the .xlsx
    df_top = pd.read_excel(f,'Sheet1',encoding = 'utf-8').iloc[:,0:2]
    df_less = pd.read_excel(f,'Sheet2',encoding = 'utf-8').iloc[:,0:2]

Problem description

The first read_excel() works fine. But the second read_excel() get the following:
Traceback (most recent call last):
File "xxx.py", line 127, in
df_less = pd.read_excel(f,'Sheet2',encoding = 'utf-8').iloc[:,0:2]
File "xxx\pandas\io\excel_base.py", line 304, in read_excel
io = ExcelFile(io, engine=engine)
File "xxx\pandas\io\excel_base.py", line 821, in init
self._reader = self._enginesengine
File "xxx\pandas\io\excel_xlrd.py", line 21, in init
super().init(filepath_or_buffer)
File "xxx\pandas\io\excel_base.py", line 350, in init
filepath_or_buffer.seek(0)
ValueError: seek of closed file

It seems the read_excel() change the io object "f". Is that some new feature in 1.0? Older versions don't have the problem.

I think it should be fixed because a function should not change the outer variable.

Expected Output

read_excel() should not change the io stream and should be able to read the same io stream several times.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.7.5.final.0
python-bits : 64
OS : Windows
OS-release : 10
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 13, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.None
pandas : 1.0.0
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 45.1.0.post20200127
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.4.2
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : 4.4.2
matplotlib : 3.1.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : 1.3.2
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 1.2.0
xlwt : None
xlsxwriter : None
numba : None

@mroeschke
Copy link
Member

Thanks. I think this is a duplicate of #31467

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants