I'm using pandas to read_csv
a 3.8 Gig text file, pipe-delimited, but it errors when reading the file into memory.
Here is the full error thrown out of my read_in_files()
function:
Reading in file C:\Users\cdabel\Desktop\_Temp\Master_Extract_Data_Mart_201909240935.txt
Traceback (most recent call last):
File "<stdin>", line 10, in <module>
File "<stdin>", line 7, in read_in_files
File "c:\python36\lib\site-packages\pandas\io\parsers.py", line 685, in parser_f
return _read(filepath_or_buffer, kwds)
File "c:\python36\lib\site-packages\pandas\io\parsers.py", line 463, in _read
data = parser.read(nrows)
File "c:\python36\lib\site-packages\pandas\io\parsers.py", line 1154, in read
ret = self._engine.read(nrows)
File "c:\python36\lib\site-packages\pandas\io\parsers.py", line 2048, in read
data = self._reader.read(nrows)
File "pandas\_libs\parsers.pyx", line 879, in pandas._libs.parsers.TextReader.read
File "pandas\_libs\parsers.pyx", line 894, in pandas._libs.parsers.TextReader._read_low_memory
File "pandas\_libs\parsers.pyx", line 948, in pandas._libs.parsers.TextReader._read_rows
File "pandas\_libs\parsers.pyx", line 935, in pandas._libs.parsers.TextReader._tokenize_rows
File "pandas\_libs\parsers.pyx", line 2130, in pandas._libs.parsers.raise_parser_error
pandas.errors.ParserError: Error tokenizing data. C error: Unknown error in IO callback
What are possible causes of this error? Could it be memory-related? How can I troubleshoot this? Should I chunk this data?
I don't suspect a RAM issue, since I have over 7 GBs of unused RAM while the function is being called, as seen in my Windows 10 Task Manager Performance monitor. Also, I can't provide any examples of the underlying data, because it's health and PII data.
import os
import pandas as pd
# File
filepath = "C:\\Temp\\datafile.txt"
filename_w_ext = "datafile.txt"
# Read in TXT file
def read_in_files(filepath, filename_w_ext):
filename, file_ext = os.path.splitext(filename_w_ext)
print('Reading in file {}'.format(filepath))
with open(filepath, "r", newline='') as file:
global df_data
# Here's where it errors:
df_data = pd.read_csv(file, dtype=str, sep='|')
return df_data.columns.values.tolist(), df_data.values.tolist()
Googling for this specific error only gives the source code for the error handling in the pandas Tokenizer code
static int parser_buffer_bytes(parser_t *self, size_t nbytes) {
int status;
size_t bytes_read;
status = 0;
self->datapos = 0;
self->data = self->cb_io(self->source, nbytes, &bytes_read, &status);
TRACE((
"parser_buffer_bytes self->cb_io: nbytes=%zu, datalen: %d, status=%d\n",
nbytes, bytes_read, status));
self->datalen = bytes_read;
if (status != REACHED_EOF && self->data == NULL) {
int64_t bufsize = 200;
self->error_msg = (char *)malloc(bufsize);
if (status == CALLING_READ_FAILED) {
snprintf(self->error_msg, bufsize,
"Calling read(nbytes) on source failed. "
"Try engine='python'.");
} else {
snprintf(self->error_msg, bufsize, "Unknown error in IO callback");
}
return -1;
}
TRACE(("datalen: %d\n", self->datalen));
return status;
}
After testing on a much beefier server, I now realize this error is clearly due to the file requiring between 25 and 35 GB of free RAM for my 4 GB file having 114 columns in it. This should actually throw an out-of-memory error, but I imagine that the amount step-up in RAM outgrew the Tokenizer code's ability to check how close it was to running out of memory.