Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dask read_csv fails where pandas doesn't

Trying to use dask's read_csv on file where pandas's read_csv like this

dd.read_csv('data/ecommerce-new.csv')

fails with the following error:

pandas.errors.ParserError: Error tokenizing data. C error: EOF inside string starting at line 2

The file is csv file of scraped data using scrapy with two columns, one with the url and the other with the html(which is stored multiline using " as delimiter char). Being actually parsed by pandas means it should be actually well-formatted.

html,url
https://google.com,"<a href=""link"">
</a>"

Making the sample argument big enough to load the entire file in memory seems to work, which makes me believe it actually fails when trying to infer the datatypes(also there's this issue which should have been solved https://github.com/dask/dask/issues/1284)

Has anyone encountered this problem before? Is there a fix/workaround?

EDIT: Apparently this is a known problem with dask's read_csv if the file contains a newline character between quotes. A solution I found was to simply read it all in memory:

dd.from_pandas(pd.read_csv(input_file), chunksize=25)

This works, but at the cost of parallelism. Any other solution?

like image 398
nikitautiu Avatar asked Aug 18 '17 09:08

nikitautiu


1 Answers

For people coming here in 2020, the dd.read_csv works directly for newlines inside quotes. It has been fixed. Update to the latest version of Dask (2.18.1 and above) to get these benefits.

import dask.dataframe as dd
df = dd.read_csv('path_to_your_file.csv')
print(df.compute())

Gives,

                 html                    url
0  https://google.com  <a href="link">\n</a>

OR

For people who want to use an older version for some reason, as suggested by @mdurant you might wanna pass blocksize=None to dd.read_csv which will be at a cost of parallel loading.

like image 145
Vishnudev Avatar answered Oct 06 '22 10:10

Vishnudev