Description
Pandas version checks
-
I have checked that this issue has not already been reported.
-
I have confirmed this bug exists on the latest version of pandas.
-
I have confirmed this bug exists on the main branch of pandas.
Reproducible Example
import pathlib
import pandas
import pytest
def test_encoding_is_ignored(
tmp_path: pathlib.Path,
):
_test_csv = tmp_path / "test.csv"
with open(_test_csv, "w", encoding="latin1") as f:
f.write("A,B\nÜ,Ä\n")
# works
_direct_read = pandas.read_csv(_test_csv, encoding="latin1")
with open(_test_csv, "r") as f:
# fails
_indirect_read = pandas.read_csv(f, encoding="latin1")
Issue Description
If read_csv
is used with a FileLike object, the encoding is correctly delegated to the object, but the encoding parameter is silently ignored.
Expected Behavior
A warning should be created to make the user aware of this behavior, which might be expected if the encoding is given explicitly to the open
but not if it is using its default.
An easy way would be a comparison of the FileType's encoding to the one read_csv
would use, if the latter is not the default.
This checking for deviation of the default could also be done by setting encoding=None
in the definition and setting the internal encoding like encoding = encoding or "utf-8"
Installed Versions
pd.show_versions()
INSTALLED VERSIONS
commit : bdc79c1
python : 3.12.1.final.0
python-bits : 64
OS : Linux
OS-release : 6.2.0-26-generic
Version : #26-Ubuntu SMP PREEMPT_DYNAMIC Mon Jul 10 23:39:54 UTC 2023
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8
pandas : 2.2.1
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.9.0.post0
setuptools : 69.2.0
pip : 23.2.1
Cython : None
pytest : 8.1.1
hypothesis : 6.99.11
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : 8.22.2
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None