gpt4 book ai didi

python - 如何在 Pandas 中将新类别附加到 HDF5?

转载 作者:太空狗 更新时间:2023-10-29 20:27:02 24 4
gpt4 key购买 nike

Answered: It appears that this datatype will not be suited for adding arbitrary strings into hdf5store.

背景

我使用一个脚本来生成单行结果,并以迭代的方式将它们附加到磁盘上的文件中。为了加快速度,我决定使用 HDF5 容器而不是 .csv。 A benchmarking然后发现字符串会减慢 HDF5 的速度。我是told将字符串转换为 categorical dtype 时可以减轻这种情况。

问题

我无法将带有新类别的分类行 附加到 HDF5。另外,我不知道如何控制 cat.codes 的数据类型,AFAIK 可以以某种方式完成。

可重现的例子:

1 - 使用分类数据创建大型数据框

import pandas as pd
import numpy as np
from pandas import HDFStore, DataFrame
import random, string

dummy_data = [''.join(random.sample(string.ascii_uppercase, 5)) for i in range(100000)]
df_big = pd.DataFrame(dummy_data, columns = ['Dummy_Data'])
df_big['Dummy_Data'] = df_big['Dummy_Data'].astype('category')

2 - 创建一行以追加

df_small = pd.DataFrame(['New_category'], columns = ['Dummy_Data'])
df_small['Dummy_Data'] = df_small['Dummy_Data'].astype('category')

3 - 将 (1) 保存到 HDF 并尝试附加 (2)

df_big.to_hdf('h5_file.h5', \
'symbols_dict', format = "table", data_columns = True, append = False, \
complevel = 9, complib ='blosc')

df_small.to_hdf('h5_file.h5', \
'symbols_dict', format = "table", data_columns = True, append = True, \
complevel = 9, complib ='blosc')

这会导致以下异常

ValueError: invalid combinate of [values_axes] on appending data [name->Dummy_Data,cname->Dummy_Data,dtype->int8,kind->integer,shape->(1,)] vs current table [name->Dummy_Data,cname->Dummy_Data,dtype->int32,kind->integer,shape->None]

我的修复尝试

我尝试调整 cat.catcodes 的数据类型:

df_big['Dummy_Data'] = df_big['Dummy_Data'].cat.codes.astype('int32')
df_small['Dummy_Data'] = df_small['Dummy_Data'].cat.codes.astype('int32')

当我这样做时,错误消失了,但分类数据类型也消失了:

df_test = pd.read_hdf('h5_file.h5', key='symbols_dict')
print df_mydict.info()

<class 'pandas.core.frame.DataFrame'>
Int64Index: 100001 entries, 0 to 0 # The appending worked now
Data columns (total 1 columns):
Dummy_Data 100001 non-null int32 # Categorical dtype gone
dtypes: int32(1) # I need to change dtype of cat.codes of categorical
memory usage: 1.1 MB # Not of categorical itself

另外,df_small.info()一开始就没有显示cat.codes的dtype,调试起来比较困难。我做错了什么?

问题

<强>1。如何正确更改cat.codes的数据类型?
2. 如何在 python 中将分类数据正确附加到 HDF5?

最佳答案

我不是这方面的专家,但至少我看过 h5py 模块,http://docs.h5py.org/en/latest/high/dataset.html , HDF5 支持 Numpy 数据类型,不包括任何分类数据类型。

PyTables 相同,这是 Pandas 使用的。

Categories 数据类型在 Pandas datatypes 中被引入和使用, 并被描述为:

A categorical variable takes on a limited, and usually fixed, number of possible values (categories; levels in R)

那么可能会发生什么情况,也许每次为了添加新类别,您都必须以某种方式重新读取 hdf5store 中的所有现有类别,以便 Pandas 对其重新编制索引?

然而,从一般文档来看,该数据类型似乎适合将任意字符串添加到 hdf5store 中,除非您确定在添加几次之后可能不会有新类别.

另外请注意,除非您的应用程序需要极高的性能,否则将数据存储在 SQL 中可能是更好的选择——一方面,SQL 对字符串有更好的支持。例如,虽然在某些 test 中发现 SQLite 比 HDF5 慢,它们不包括处理字符串。从 CSV 跳到 HDF5 听起来像是从马车跳到火箭,但也许汽车或飞机也能正常工作(或者更好,因为它有更多选择,可以延伸类比)?

关于python - 如何在 Pandas 中将新类别附加到 HDF5?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46271921/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com