Skip to content

CSV with encoding=iso-8859-1 fails #7714

Description

@indigoviolet

Polars version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of Polars.

Issue description

Even though the documentation suggests that encodings other than 'utf8' or 'utf8-lossy' are supported, this fails.

Reproducible example

echo -e "Column 1,Column 2\nVälüë 1,Vàlüë 2\nÄnöthër vãlüë,Ånd anøthér vãlüë" | iconv -t ISO-8859-1 > output.csv

gzip output.csv -c > output_gz.csv.gz
In [8]: pl.read_csv('output.csv', encoding='iso-8859-1')
Polars found a filename. Ensure you pass a path to the file instead of a python file object when possible for best performance.
---------------------------------------------------------------------------
ComputeError                              Traceback (most recent call last)
Cell In[8], line 1
----> 1 pl.read_csv('output.csv', encoding='iso-8859-1')
...

ComputeError: invalid utf-8 sequence in csv

In [9]: pl.read_csv('output_gz.csv.gz', encoding='iso-8859-1')
Polars found a filename. Ensure you pass a path to the file instead of a python file object when possible for best performance.
Out[9]: 
shape: (0, 1)
┌──────────────────────┐
│ s��)��S0q0���y
{… │
│ ---                  │
│ str                  │
╞══════════════════════╡
└──────────────────────┘


### Expected behavior

succeeds with `use_pyarrow`

```python
In [10]: pl.read_csv('output.csv', encoding='iso-8859-1', use_pyarrow=True)
Out[10]: 
shape: (2, 2)
┌───────────────┬───────────────────┐
│ Column 1      ┆ Column 2          │
│ ---           ┆ ---               │
│ str           ┆ str               │
╞═══════════════╪═══════════════════╡
│ Välüë 1       ┆ Vàlüë 2           │
│ Änöthër vãlüë ┆ Ånd anøthér vãlüë │
└───────────────┴───────────────────┘

In [11]: pl.read_csv('output_gz.csv.gz', encoding='iso-8859-1', use_pyarrow=True)
Out[11]: 
shape: (2, 2)
┌───────────────┬───────────────────┐
│ Column 1      ┆ Column 2          │
│ ---           ┆ ---               │
│ str           ┆ str               │
╞═══════════════╪═══════════════════╡
│ Välüë 1       ┆ Vàlüë 2           │
│ Änöthër vãlüë ┆ Ånd anøthér vãlüë │
└───────────────┴───────────────────┘

Installed versions

Details
---Version info---
Polars: 0.16.14
Index type: UInt32
Platform: Linux-6.2.0-76060200-generic-x86_64-with-glibc2.35
Python: 3.9.16 | packaged by conda-forge | (main, Feb  1 2023, 21:39:03) 
[GCC 11.3.0]
---Optional dependencies---
numpy: 1.23.4
pandas: 1.5.3
pyarrow: 11.0.0
connectorx: <not installed>
deltalake: <not installed>
fsspec: 2023.1.0
matplotlib: 3.7.0
xlsx2csv: <not installed>
xlsxwriter: <not installed>

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentationenhancementNew feature or an improvement of an existing featurepythonRelated to Python Polars

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions