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帮助?
当前回答
df = pd.read_csv('somefile.csv', low_memory=False)
这应该能解决问题。当从CSV中读取1.8M行时,我得到了完全相同的错误。
其他回答
根据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")
df = pd.read_csv('somefile.csv', low_memory=False)
这应该能解决问题。当从CSV中读取1.8M行时,我得到了完全相同的错误。
这对我很管用!
file = pd.read_csv('example.csv', engine='python')
正如错误所示,在使用read_csv()方法时应该指定数据类型。 所以,你应该写
file = pd.read_csv('example.csv', dtype='unicode')
有时候,当其他方法都失败时,你只想告诉熊猫闭嘴:
# Ignore DtypeWarnings from pandas' read_csv
warnings.filterwarnings('ignore', message="^Columns.*")