While using read_csv
with Pandas, if i want a given column to be converted to a type, a malformed value will interrupt the whole operation, without an indication about the offending value.
For example, running something like:
import pandas as pd
import numpy as np
df = pd.read_csv('my.csv', dtype={ 'my_column': np.int64 })
Will lead to a stack trace ending with the error:
ValueError: cannot safely convert passed user dtype of <i8 for object dtyped data in column ...
If i had the row number, or the offending value in the error message, i could add it to the list of known NaN
values, but this way there is nothing i can do.
Is there a way to tell the parser to ignore failures and return a np.nan
in that case?
Post Scriptum: Funnily enough, after parsing without any type suggestion (no dtype
argument), d['my_column'].value_counts()
seems to infer the dtype
right and put np.nan
correctly automatically, even though the actual dtype
for the series is a generic object
which will fail on almost every plotting and statistical operation