joblib.Memory 和 pandas.DataFrame 输入

发布于 2025-01-11 06:47:43 字数 1435 浏览 4 评论 0原文

我发现,当使用数据帧作为装饰函数的输入时, joblib.Memory.cache 会导致不可靠的缓存。经过一番尝试,我发现 joblib.hash 会导致哈希值不一致,至少在某些情况下是这样。如果我理解正确的话,joblib.hashjoblib.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〆凄凉。 2025-01-18 06:47:43

使用 df.sample 后我遇到了非常类似的问题:

import joblib
import pandas as pd

df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4., 5., 6.],
})


df = df.sample(n=2, random_state=42)
df_copy = df.copy(deep=True)

pd.testing.assert_frame_equal(df_copy, df)

print("df hash:     ", joblib.hash(df)[:8])
print("df_copy hash:", joblib.hash(df_copy)[:8])

输出

df hash:       4fcae8aa
df_copy hash:  bdf63e40

在没有 df.sample 的情况下运行相同的脚本会为 df 呈现相同的哈希值,并且它是复制。

作业库:1.2.0
熊猫:1.4.4

I have a very similar issue after using df.sample:

import joblib
import pandas as pd

df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4., 5., 6.],
})


df = df.sample(n=2, random_state=42)
df_copy = df.copy(deep=True)

pd.testing.assert_frame_equal(df_copy, df)

print("df hash:     ", joblib.hash(df)[:8])
print("df_copy hash:", joblib.hash(df_copy)[:8])

Output

df hash:       4fcae8aa
df_copy hash:  bdf63e40

Running same script without df.sample renders the same hash for the df and it's copy.

Joblib: 1.2.0
Pandas: 1.4.4

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文