-
-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
BUG: Pandas closes user-provided file handles that it doesn't own. #36980
Comments
@tpllaha thank you for the report! I thought master wouldn't have that problem anymore (I was thinking about |
@tpllaha if you need a quick workaround, only the c engine is affected. Until it is fixed, you can use |
@twoertwein thanks. For now, I've worked around it by simply not passing the encoding parameter. It looks like the default encoding is utf-8 and my files are utf-8 encoded, but the bug only surfaces if the encoding is passed explicitly. |
read_csv closes a file handle when passed an open one with an encoding set (pandas-dev/pandas#36980) in pandas <1.2. Update pandas dependency to fix this bug.
pandas-dev/pandas#36980 Upgrade pandas to v1.2.5 fixes this issue
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of pandas.
(optional) I have confirmed this bug exists on the master branch of pandas.
read_csv
closes user-provided file handles in specific cases. In particular, if we pass aBytesIO
or a file opened in binary mode, and pass anencoding
kwarg. If theencoding
kwarg is not passed, pandas does not close the file handle (as expected).Much earlier (in 2016) a related issue has been reported and fixed. The test cases that were mentioned there still pass today, but only in case the file handle is opened in binary mode & the
encoding
kwarg is passed. (version1.1.2
& python3.8.5
)Code Sample, a copy-pastable example
prints:
Problem description
Expectation is that pandas should never close user-provided handles. If pandas didn't open the file, then it doesn't own it & therefore shouldn't close it.
Expected Output
I would expect the script above to output:
Output of
pd.show_versions()
INSTALLED VERSIONS
commit : d9fff27
python : 3.8.5.final.0
python-bits : 64
OS : Darwin
OS-release : 19.6.0
Version : Darwin Kernel Version 19.6.0: Thu Jun 18 20:49:00 PDT 2020; root:xnu-6153.141.1~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8
pandas : 1.1.0
numpy : 1.17.2
pytz : 2020.1
dateutil : 2.7.3
pip : 20.2.3
setuptools : 40.7.1
Cython : 0.29.7
pytest : 4.6.5
hypothesis : 4.55.2
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.5.2
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.18.1
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : 0.8.2
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : 0.3.5
scipy : 1.5.2
sqlalchemy : 1.3.10
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None
The text was updated successfully, but these errors were encountered: