提问人:Josh 提问时间:6/17/2014 最后编辑:Mateen UlhaqJosh 更新时间:11/22/2022 访问量:701865
Pandas read_csv:low_memory 和 dtype 选项
Pandas read_csv: low_memory and dtype options
问:
df = pd.read_csv('somefile.csv')
...给出错误:
.../site-packages/pandas/io/parsers.py:1130: DtypeWarning:列 (4,5,7,16) 具有混合类型。指定 dtype 导入时的选项或设置 low_memory=False。
为什么该选项与 相关,为什么可能会有所帮助?dtype
low_memory
low_memory=False
答:
尝试:
dashboard_df = pd.read_csv(p_file, sep=',', error_bad_lines=False, index_col=False, dtype='unicode')
根据 pandas 文档:
dtype :列 -> 类型的类型名称或字典
至于low_memory,默认情况下是 True,尚未记录。我不认为这无关紧要。错误消息是通用的,因此无论如何您都不需要弄乱low_memory。希望这有帮助,如果您有其他问题,请告诉我
评论
dtype=unicode
NameError: name 'unicode' is not defined
unicode
numpy.dtype('unicode')
numpy.dtype()
'unicode'
objects
dtype='object'
已弃用的 low_memory 选项
该选项没有被正确弃用,但它应该被弃用,因为它实际上并没有做任何不同的事情[来源low_memory
]
收到此警告的原因是,猜测每列的 dtypes 对内存的要求非常高。Pandas 尝试通过分析每列中的数据来确定要设置的 dtype。low_memory
Dtype 猜测(非常糟糕)
Pandas 只有在读取整个文件后才能确定列应该具有什么 dtype。这意味着在读取整个文件之前,实际上无法分析任何内容,除非您在读取最后一个值时必须更改该列的 dtype。
考虑一个文件的例子,该文件有一个名为 user_id 的列。 它包含 1000 万行,其中user_id始终是数字。 由于 pandas 无法知道它只是数字,因此它可能会将其保留为原始字符串,直到它读取整个文件。
指定 dtypes(应始终执行)
添加
dtype={'user_id': int}
对 pd.read_csv()
调用将使 pandas 知道它何时开始读取文件,这只是整数。
另外值得注意的是,如果文件中的最后一行已经写入列中,则如果指定了上述 dtype,则加载将崩溃。"foobar"
user_id
定义 dtypes 时中断的损坏数据示例
import pandas as pd
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
csvdata = """user_id,username
1,Alice
3,Bob
foobar,Caesar"""
sio = StringIO(csvdata)
pd.read_csv(sio, dtype={"user_id": int, "username": "string"})
ValueError: invalid literal for long() with base 10: 'foobar'
dtypes 通常是一个 numpy 的东西,在这里阅读更多关于它们的信息: http://docs.scipy.org/doc/numpy/reference/generated/numpy.dtype.html
存在哪些 dtypes?
我们可以访问 numpy dtypes:float、int、bool、timedelta64[ns] 和 datetime64[ns]。请注意,numpy 日期/时间 dtypes 无法识别时区。
Pandas 用自己的 dtypes 扩展了这组 dtypes:
'datetime64[ns, <tz>]'
这是时区感知时间戳。
'category',它本质上是一个枚举(字符串由整数键表示,以保存
'period[]' 不要与时间增量混淆,这些对象实际上锚定到特定的时间段
'Sparse', 'Sparse[int]', 'Sparse[float]' 用于稀疏数据或 '数据中有很多漏洞' 它不是在数据帧中保存 NaN 或 None,而是省略对象,从而节省空间。
“间隔”本身就是一个主题,但它的主要用途是用于索引。在这里查看更多
'Int8', 'Int16', 'Int32', 'Int64', 'UInt8', 'UInt16', 'UInt32', 'UInt64' 都是特定于 pandas 的整数,可以为 null,这与 numpy 变体不同。
“string”是用于处理字符串数据的特定 dtype,用于访问序列上的属性。.str
'boolean' 类似于 numpy 'bool',但它也支持缺失数据。
在此处阅读完整参考资料:
陷阱、警告、注释
设置将使上述警告静音,但不会使其更节省内存,只有处理效率(如果有的话)。dtype=object
设置不会做任何事情,因为对于 numpy,a 表示为 .dtype=unicode
unicode
object
转换器的使用
@sparrow正确地指出了转换器的用法,以避免在指定为 .我想补充一点,转换器在熊猫中使用真的很重且效率低下,应该作为最后的手段使用。这是因为read_csv进程是单个进程。'foobar'
int
CSV 文件可以逐行处理,因此可以通过简单地将文件切成段并运行多个进程来更有效地由多个转换器并行处理,这是 pandas 不支持的。但这是另一回事。
评论
dtype=object
error_bad_lines=False, warn_bad_lines=True
nrows=100
df.dtypes
try/except
df = pd.read_csv('somefile.csv', low_memory=False)
这应该可以解决问题。从 CSV 读取 1.8M 行时,我遇到了完全相同的错误。
评论
如前所述,firelynx如果明确指定了dtype,并且存在与该dtype不兼容的混合数据,则加载将崩溃。我使用这样的转换器作为解决方法来更改数据类型不兼容的值,以便仍然可以加载数据。
def conv(val):
if not val:
return 0
try:
return np.float64(val)
except:
return np.float64(0)
df = pd.read_csv(csv_file,converters={'COL_A':conv,'COL_B':conv})
我在 ~400MB 文件上遇到了类似的问题。设置对我来说是诀窍。首先做简单的事情,我会检查您的数据帧是否大于您的系统内存,重新启动,清除RAM,然后再继续。如果您仍然遇到错误,值得确保您的文件正常,请在 Excel 中快速查看并确保没有明显的损坏。损坏的原始数据可能会造成严重破坏......low_memory=False
.csv
它在导入 DataFrame 时对我有用。这就是对我有用的所有变化: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'] = ''
根据 pandas 文档,只要指定(这是默认值)是解决这个问题的合理解决方案。low_memory=False
engine='c'
如果 ,则将首先读入整个列,然后确定正确的类型。例如,列将根据需要保留为对象(字符串)以保留信息。low_memory=False
如果(默认值),则 pandas 会以行块的形式读取数据,然后将它们追加在一起。然后,某些列可能看起来像是混合在一起的整数块和字符串块,具体取决于在块 pandas 是否遇到任何无法转换为整数的内容(例如)。这可能会在以后引起问题。警告告诉您,这在读入时至少发生过一次,因此您应该小心。设置将使用更多内存,但可以避免问题。low_memory=True
low_memory=False
就我个人而言,我认为这是一个糟糕的默认设置,但我在一个使用比大型数据集多得多的小型数据集的领域工作,因此便利性比效率更重要。low_memory=True
下面的代码演示了一个示例,其中设置了混合类型,并且列具有混合类型。它通过@firelynxlow_memory=True
import pandas as pd
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
# make a big csv data file, following earlier approach by @firelynx
csvdata = """1,Alice
2,Bob
3,Caesar
"""
# we have to replicate the "integer column" user_id many many times to get
# pd.read_csv to actually chunk read. otherwise it just reads
# the whole thing in one chunk, because it's faster, and we don't get any
# "mixed dtype" issue. the 100000 below was chosen by experimentation.
csvdatafull = ""
for i in range(100000):
csvdatafull = csvdatafull + csvdata
csvdatafull = csvdatafull + "foobar,Cthlulu\n"
csvdatafull = "user_id,username\n" + csvdatafull
sio = StringIO(csvdatafull)
# the following line gives me the warning:
# C:\Users\rdisa\anaconda3\lib\site-packages\IPython\core\interactiveshell.py:3072: DtypeWarning: Columns (0) have mixed types.Specify dtype option on import or set low_memory=False.
# interactivity=interactivity, compiler=compiler, result=result)
# but it does not always give me the warning, so i guess the internal workings of read_csv depend on background factors
x = pd.read_csv(sio, low_memory=True) #, dtype={"user_id": int, "username": "string"})
x.dtypes
# this gives:
# Out[69]:
# user_id object
# username object
# dtype: object
type(x['user_id'].iloc[0]) # int
type(x['user_id'].iloc[1]) # int
type(x['user_id'].iloc[2]) # int
type(x['user_id'].iloc[10000]) # int
type(x['user_id'].iloc[299999]) # str !!!! (even though it's a number! so this chunk must have been read in as strings)
type(x['user_id'].iloc[300000]) # str !!!!!
题外话:举个例子,说明这是一个问题(也是我第一次遇到这个问题的严重问题),想象一下你运行了一个文件,然后想根据标识符删除重复项。假设标识符有时是数字,有时是字符串。一行可能是“81287”,另一行可能是“97324-32”。尽管如此,它们仍是唯一标识符。pd.read_csv()
使用 ,pandas 可能会在标识符列中读取如下内容:low_memory=True
81287
81287
81287
81287
81287
"81287"
"81287"
"81287"
"81287"
"97324-32"
"97324-32"
"97324-32"
"97324-32"
"97324-32"
仅仅因为它把东西分块了,所以有时标识符 81287 是一个数字,有时是一个字符串。当我尝试基于此删除重复项时,好吧,
81287 == "81287"
Out[98]: False
正如错误所说,您应该在使用该方法时指定数据类型。
所以,你应该写read_csv()
file = pd.read_csv('example.csv', dtype='unicode')
有时,当所有其他方法都失败时,你只想告诉熊猫闭嘴:
# Ignore DtypeWarnings from pandas' read_csv
warnings.filterwarnings('ignore', message="^Columns.*")
评论
这对我有用!
file = pd.read_csv('example.csv', engine='python')
评论
file = pd.read_csv('example.csv', encoding='utf-8', engine='python')
, on_bad_lines='warn'
在 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')
评论