据我了解,您没有足够的 RAM 来创建具有 5400 万行和 100 列的数据框。一些问题:您将如何增量加载数据以使其适合内存?此外,一旦将数据保存到 HDF5 文件中,您将如何使用这些数据? (同样,因为它不适合内存)恕我直言,您加载和处理数据的方式将影响您写入和读取该数据的方式。
阅读文档并运行一些简单的测试后,(对我而言)不清楚您是否可以将列附加到 HDF5 文件(使用 Pandas 或 PyTables)中的现有表中。这似乎是 HDF5 对“表格”的限制(用于 2d 异构数据,类似于 Pandas 数据帧)。当我尝试将数据框系列附加为新列时,我收到了与您相同的错误消息。
您是否考虑过分批处理行(比如一次 1M 行)?有几个示例展示了如何追加行。如果使用此方法,您可以创建一个具有 100 列和 54M 行的单个 HDF5 数据集(表)。
有一种方法可以写入数据列 - 但您必须为每列单独的数据集(Pandas 术语中的“键”)。以下代码显示了如何执行此操作。它写入数据,然后将其读回新的数据帧。 (它还使用 2 种不同的方法将整个数据帧写入其他 2 个文件,以便您可以比较这些值。)
代码如下:
dates = ['2021-08-01','2021-08-02','2021-08-03','2021-08-04','2021-08-05',
'2021-08-06','2021-08-07','2021-08-08','2021-08-09','2021-08-10' ]
precip = [ 0.0, 0.02, 0.0, 0.12, 0.0, 0.0, 1.11, 0.0, 0.0, 0.05]
temps = [ 80.0, 71.2, 77.5, 85.4, 83.3, 90.0, 78.9, 80.1, 72.4, 88.8]
weather_df = pd.DataFrame({'dates': dates, 'precip': precip, 'temps': temps})
# write entire dataframe to 1 Table with pd.HDFStore()
with pd.HDFStore('file1-a.h5', mode='w') as store1:
store1.put("weather_data", weather_df, format='table')
# write entire dataframe to 1 Table with df.to_hdf()
weather_df.to_hdf('./file1-b.h5', 'weather_data', mode='w', format='table')
# write each series to a different Table with pd.HDFStore()
with pd.HDFStore('file2.h5', mode='w') as store2:
store2.put('dates', weather_df['dates'], format='table')
with pd.HDFStore('file2.h5', mode='a') as store2:
store2.put('precip', weather_df['precip'])
with pd.HDFStore('file2.h5', mode='a') as store2:
store2.append('temps', weather_df['temps'])
# load the HDF5 data into a new dataframe
with pd.HDFStore('file2.h5', mode='r') as store3:
w_df = pd.DataFrame({'dates': store3.get('dates'),
'precip': store3.get('precip'),
'temps': store3.get('temps') })