df = pd.read_csv('somefile.csv')
...给出一个错误:
熊猫…/网站/ / io / parsers.py: 1130: DtypeWarning:列(4,5,7,16)为混合类型。指定dtype 选项导入或设置low_memory=False。
为什么dtype选项与low_memory相关,为什么low_memory=False帮助?
df = pd.read_csv('somefile.csv')
...给出一个错误:
熊猫…/网站/ / io / parsers.py: 1130: DtypeWarning:列(4,5,7,16)为混合类型。指定dtype 选项导入或设置low_memory=False。
为什么dtype选项与low_memory相关,为什么low_memory=False帮助?
当前回答
根据Jerald Achaibar给出的答案,我们可以检测混合Dytpes警告,并且只在警告发生时使用较慢的python引擎:
import warnings
# Force mixed datatype warning to be a python error so we can catch it and reattempt the
# load using the slower python engine
warnings.simplefilter('error', pandas.errors.DtypeWarning)
try:
df = pandas.read_csv(path, sep=sep, encoding=encoding)
except pandas.errors.DtypeWarning:
df = pandas.read_csv(path, sep=sep, encoding=encoding, engine="python")
其他回答
它为我工作与low_memory = False同时导入一个数据帧。这就是所有对我有效的改变:
df = pd.read_csv('export4_16.csv',low_memory=False)
在处理一个巨大的csv文件(600万行)时,我也遇到过类似的问题。我有三个问题:
文件包含奇怪字符(使用编码修复) 未指定数据类型(使用dtype属性修复) 使用上面的方法,我仍然面临一个问题,这与无法基于文件名定义的file_format有关(使用try ..除了. .)
df = pd.read_csv(csv_file,sep=';', encoding = 'ISO-8859-1',
names=['permission','owner_name','group_name','size','ctime','mtime','atime','filename','full_filename'],
dtype={'permission':str,'owner_name':str,'group_name':str,'size':str,'ctime':object,'mtime':object,'atime':object,'filename':str,'full_filename':str,'first_date':object,'last_date':object})
try:
df['file_format'] = [Path(f).suffix[1:] for f in df.filename.tolist()]
except:
df['file_format'] = ''
根据Jerald Achaibar给出的答案,我们可以检测混合Dytpes警告,并且只在警告发生时使用较慢的python引擎:
import warnings
# Force mixed datatype warning to be a python error so we can catch it and reattempt the
# load using the slower python engine
warnings.simplefilter('error', pandas.errors.DtypeWarning)
try:
df = pandas.read_csv(path, sep=sep, encoding=encoding)
except pandas.errors.DtypeWarning:
df = pandas.read_csv(path, sep=sep, encoding=encoding, engine="python")
这对我很管用!
dashboard_df = pd.read_csv(p_file, sep=';', error_bad_lines=False, index_col=False, dtype='unicode')
这对我很管用!
file = pd.read_csv('example.csv', engine='python')