joblib.Memory 和 pandas.DataFrame 输入
我发现,当使用数据帧作为装饰函数的输入时, joblib.Memory.cache 会导致不可靠的缓存。经过一番尝试,我发现 joblib.hash
会导致哈希值不一致,至少在某些情况下是这样。如果我理解正确的话,joblib.hash
被joblib.Memory
使用,所以这可能是问题的根源。
当新列添加到数据帧并随后复制时,或者当数据帧保存并从磁盘加载时,似乎会出现问题。以下示例比较了应用于数据帧时不一致的哈希输出,或应用于等效 numpy 数据时的一致结果。
import pandas as pd
import joblib
df = pd.DataFrame({'A':[1,2,3],'B':[4.,5.,6.], })
df.index.name='MyInd'
df['B2'] = df['B']**2
df_copy = df.copy()
df_copy.to_csv("df.csv")
df_fromfile = pd.read_csv('df.csv').set_index('MyInd')
print("DataFrame Hashes:")
print(joblib.hash(df))
print(joblib.hash(df_copy))
print(joblib.hash(df_fromfile))
def _to_tuple(df):
return (df.values, df.columns.values, df.index.values, df.index.name)
print("Equivalent Numpy Hashes:")
print(joblib.hash(_to_tuple(df)))
print(joblib.hash(_to_tuple(df_copy)))
print(joblib.hash(_to_tuple(df_fromfile)))
输出结果:
DataFrame Hashes:
4e9352c1ffc14fb4bb5b1a5ad29a3def
2d149affd4da6f31bfbdf6bd721e06ef
6843f7020cda9d4d3cbf05dfc47542d4
Equivalent Numpy Hashes:
6ad89873c7ccbd3b76ae818b332c1042
6ad89873c7ccbd3b76ae818b332c1042
6ad89873c7ccbd3b76ae818b332c1042
“等效 Numpy 哈希”是我想要的行为。我猜这个问题是由于 DataFrame 使用某种复杂的内部元数据造成的。是否有任何规范的方法可以在 pandas DataFrames 上使用 joblib.Memory.cache ,以便它仅根据数据值进行缓存?
一个“足够好”的解决方法是,如果用户可以通过某种方式告诉 joblib.Memory.cache 使用类似于我上面的 _to_tuple 函数的特定参数。
I've been finding that joblib.Memory.cache
results in unreliable caching when using dataframes as inputs to the decorated functions. Playing around, I found that joblib.hash
results in inconsistent hashes, at least in some cases. If I understand correctly, joblib.hash
is used by joblib.Memory
, so this is probably the source of the problem.
Problems seem to occur when new columns are added to dataframes followed by a copy, or when a dataframe is saved and loaded from disk. The following example compares the inconsistent hash output when applied to dataframes, or the consistent results when applied to the equivalent numpy data.
import pandas as pd
import joblib
df = pd.DataFrame({'A':[1,2,3],'B':[4.,5.,6.], })
df.index.name='MyInd'
df['B2'] = df['B']**2
df_copy = df.copy()
df_copy.to_csv("df.csv")
df_fromfile = pd.read_csv('df.csv').set_index('MyInd')
print("DataFrame Hashes:")
print(joblib.hash(df))
print(joblib.hash(df_copy))
print(joblib.hash(df_fromfile))
def _to_tuple(df):
return (df.values, df.columns.values, df.index.values, df.index.name)
print("Equivalent Numpy Hashes:")
print(joblib.hash(_to_tuple(df)))
print(joblib.hash(_to_tuple(df_copy)))
print(joblib.hash(_to_tuple(df_fromfile)))
results in output:
DataFrame Hashes:
4e9352c1ffc14fb4bb5b1a5ad29a3def
2d149affd4da6f31bfbdf6bd721e06ef
6843f7020cda9d4d3cbf05dfc47542d4
Equivalent Numpy Hashes:
6ad89873c7ccbd3b76ae818b332c1042
6ad89873c7ccbd3b76ae818b332c1042
6ad89873c7ccbd3b76ae818b332c1042
The "Equivalent Numpy Hashes" is the behavior I'd like. I'm guessing the problem is due to some kind of complex internal metadata that DataFrames utililize. Is there any canonical way to use joblib.Memory.cache
on pandas DataFrames so it will cache based upon the data values only?
A "good enough" workaround would be if there is a way a user can tell joblib.Memory.cache
to utilize something like my _to_tuple
function above for specific arguments.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 df.sample 后我遇到了非常类似的问题:
输出
在没有 df.sample 的情况下运行相同的脚本会为 df 呈现相同的哈希值,并且它是复制。
作业库:1.2.0
熊猫:1.4.4
I have a very similar issue after using
df.sample
:Output
Running same script without
df.sample
renders the same hash for thedf
and it's copy.Joblib: 1.2.0
Pandas: 1.4.4