【问题标题】:Fast reading of specified columns in df using pandas.to_hdf使用 pandas.to_hdf 快速读取 df 中的指定列
【发布时间】:2017-06-20 16:19:48
【问题描述】:

我有一个 2Gb 的数据帧,一次写入,多次读取 df。 我想在 pandas 中使用 df,因此我使用了固定格式的 df.read_hdfdf.to_hdf,在阅读和写作方面都很好。

但是,随着添加的列越来越多,df 也在增长,所以我想改用表格格式,这样我可以在读取数据时选择我需要的列。我认为这会给我带来速度优势,但从测试来看似乎并非如此。

这个例子:

import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.randn(10000000,9),columns=list('ABCDEFGHI'))
%time df.to_hdf("temp.h5", "temp", format ="fixed", mode="w")
%time df.to_hdf("temp2.h5", "temp2", format="table", mode="w")

显示固定格式稍快(在我的机器上为 6.8 秒对 5.9 秒)。

然后读取数据(稍作休息以确保文件已完全保存):

%time x = pd.read_hdf("temp.h5", "temp")
%time y = pd.read_hdf("temp2.h5", "temp2")
%time z = pd.read_hdf("temp2.h5", "temp2", columns=list("ABC"))

产量:

Wall time: 420 ms (fixed)   
Wall time: 557 ms (format)   
Wall time: 671 ms (format, specified columns)

我知道固定格式读取数据的速度更快,但为什么 具有指定列的 df 比读取完整数据帧慢?与固定格式相比,使用表格格式(有或没有指定列)有什么好处?

当 df 变得更大时,是否有内存优势?

【问题讨论】:

    标签: python pandas hdf5


    【解决方案1】:

    IMO 将format='table'data_columns=[list_of_indexed_columns] 结合使用的主要优点是能够有条件地(参见where="where clause" 参数)读取巨大的HDF5 文件。这样您就可以在读取时过滤数据,并分块处理数据以避免 MemoryError。

    您可以尝试将单个列或列组(大部分时间将被一起读取)保存在不同的 HDF 文件或具有不同键的同一文件中。

    我也会考虑使用“尖端”技术 - Feather-Format

    测试和时间安排:

    import feather
    

    以三种格式写入磁盘:(HDF5 固定、HDF% 表、羽化)

    df = pd.DataFrame(np.random.randn(10000000,9),columns=list('ABCDEFGHI'))
    df.to_hdf('c:/temp/fixed.h5', 'temp', format='f', mode='w')
    df.to_hdf('c:/temp/tab.h5', 'temp', format='t', mode='w')
    feather.write_dataframe(df, 'c:/temp/df.feather')
    

    从磁盘读取:

    In [122]: %timeit pd.read_hdf(r'C:\Temp\fixed.h5', "temp")
    1 loop, best of 3: 409 ms per loop
    
    In [123]: %timeit pd.read_hdf(r'C:\Temp\tab.h5', "temp")
    1 loop, best of 3: 558 ms per loop
    
    In [124]: %timeit pd.read_hdf(r'C:\Temp\tab.h5', "temp", columns=list('BDF'))
    The slowest run took 4.60 times longer than the fastest. This could mean that an intermediate result is being cached.
    1 loop, best of 3: 689 ms per loop
    
    In [125]: %timeit feather.read_dataframe('c:/temp/df.feather')
    The slowest run took 6.92 times longer than the fastest. This could mean that an intermediate result is being cached.
    1 loop, best of 3: 644 ms per loop
    
    In [126]: %timeit feather.read_dataframe('c:/temp/df.feather', columns=list('BDF'))
    1 loop, best of 3: 218 ms per loop  # WINNER !!!
    

    PS如果你在使用feather.write_dataframe(...)时遇到如下错误:

    FeatherError: Invalid: no support for strided data yet 
    

    这里有一个解决方法:

    df = df.copy()
    

    之后feather.write_dataframe(df, path) 应该可以正常工作...

    【讨论】:

    • 谢谢。您能否详细说明如何保存转置后的数据?这是否意味着我应该使用单独的键保存列,但保存到同一个 hdf 文件?你能举个例子吗?
    • @user6538642,你的“通常”大约是多少。 DF 形状?
    • shape = (6mln*50),列数扩大到几百。索引是多索引(日期(日期时间格式)和名称)
    • @user6538642,您是否总是(或大部分时间)阅读所有行?
    • 目前我正在选择所有行并在之后过滤熊猫。理想情况下,我会阅读一个样本,但这总是至少 100 万行
    猜你喜欢
    • 1970-01-01
    • 2014-03-21
    • 1970-01-01
    • 1970-01-01
    • 2016-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多