I have many big CSV files for processing in Mathematica. And each time loading them into Mathematica takes a long time. Meanwhile, it takes much less time using Pandas under Python.
For example, Mathematica takes much longer to import this test file (download) than Pandas:
fn = "/Users/sunt05/Dropbox/Public/test.csv";
(*Mathematica*)
ds = Import[fn, "Dataset", "HeaderLines" -> 1]; // AbsoluteTiming
(*{7.48445, Null}*)
(*Pandas*)
session = StartExternalSession["Python"];
py = ExternalEvaluate[session];
py[StringTemplate["import pandas as pd; res=pd.read_csv('``')"][fn]] // AbsoluteTiming
(*{0.966052, Null}*)
So, how can we build a fast CSV reader in Mathematica?
Edit:
Thanks folks for the interests!
For using Dataset
in the comparison, I just wanted to make the comparison more fair from the user perspective with respect to functionality: Dataset
seems more analogous to pandas.DataFrame
if a user wants to further process the imported data.
OpenRead
withReadLine
to store the header and then callReadList
yields a 55% speedup, but it is not as fast as pandas unfortunately. $\endgroup$Dataset
is rather inefficient in my experience, and simply unsuitable for working with large data. $\endgroup$Dataset
. However, two things we may note here: 1.Dataset
may facilitate further processing once constructed thought with considerable overhead in the construction; 2. performance inDataset
has been noticeably improved in v12.3. So, overall I considerDataset
an ~acceptable structure for data processing now (i.e., since v12.3). PS, I used to be strongly againstDataset
but did love its claimed features. $\endgroup$Dataset
is that it is row-oriented, and therefore can't effectively use the type information to both store data efficiently and manipulate it faster. The other is generality:Dataset
does not have a well-defined and restricted query language, but instead allows pretty much all WL functions as parts of its query. This gives it a lot of power, but this at the same time severely limits its efficiency. Both these issues are unlikely to change soon. $\endgroup$