简体   繁体   English

将 python 中的缓存 pandas dataframe 传递给另一个缓存的 ZC1C425268E68385D1AB5074unhashable 类型错误“A:A:941AB5074unhash7”

[英]Passing cached pandas dataframe in python to another cached function give "unhashable type: dataFrame" error

I have three functions, for example:我有三个功能,例如:

from cachetools import cached, TTLCache
import pandas as pd


cache=TTLCache(10,1000)
@cached(cache)
def function1():
    df=pd.DataFrame({'one':range(5),'two':range(5,10)})  #just a little data, doesn't matter what
    return df

@cached(cache)
def function2(df):
    var1=df['one']
    var2=df['two']
    return var1, var2

def function3():
    df=function1() 
    var1,var2=function2(df)    #pass df to function 2 for some work
    
    print('this is var1[0]: '+str(var1[0]))
    print('this is var2[0]: '+str(var2[0]))
    
function3()

I want there to be a cached version of df, var1, and var2.我希望有 df、var1 和 var2 的缓存版本。 Basically, I want to reassign df inside of function3 only if it is not cached, then do the following for var1 and var2, which depend on df.基本上,我只想在 function3 内部重新分配 df,如果它没有被缓存,然后对 var1 和 var2 执行以下操作,这取决于 df。 Is there a way to do this?有没有办法做到这一点? When I remove @cached(cache) from function2 then the code works.当我从 function2 中删除@cached(cache)时,代码就可以工作了。

This is the error I get TypeError: 'DataFrame' objects are mutable, thus they cannot be hashed这是我得到的错误TypeError: 'DataFrame' objects are mutable, thus they cannot be hashed

Try to use cacheout lib, it worked for me尝试使用 cacheout lib,它对我有用

import pandas as pd
from cacheout import Cache
cache = Cache()


@cache.memoize()
def function1():
    df = pd.DataFrame({'one': range(5), 'two': range(5, 10)})
    return df


@cache.memoize()
def function2(df):
    var1 = df['one']
    var2 = df['two']
    return var1, var2


def function3():
    df = function1()
    var1, var2 = function2(df)

    print('this is var1[0]: ' + str(var1[0]))
    print('this is var2[0]: ' + str(var2[0]))


function3()

Output: Output:

this is var1[0]: 0
this is var2[0]: 5

As the accepted answer mentioned, the issue seems to be with cachetools.正如所提到的公认答案,问题似乎与缓存工具有关。 If you absolutely must you cachetools, then you can convert the df to a string and back, but that computational expense may be prohibitive.如果您绝对必须使用缓存工具,那么您可以将 df 转换为字符串并返回,但计算费用可能会令人望而却步。

cache=TTLCache(10,1000)
@cached(cache)
def function1():
    df=pd.DataFrame({'one':range(5),'two':range(5,10)})  #just a little data, doesn't matter what
    print('iran')
    return df.to_csv(index=False) #return df as string

@cached(cache)
def function2(df):
    df = pd.read_csv(StringIO(df)) #return string df to normal pandas df.
    var1=df['one']
    var2=df['two']
    print('iran2')
    return var1, var2

def function3():
    df=function1()
    var1,var2=function2(df)
    
    print('this is var1[0]: '+str(var1[0]))
    print('this is var2[0]: '+str(var2[0]))
    
function3()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 pandas dataframe 中的不可散列类型错误 - Unhashable type error in pandas dataframe Pandas从一个数据帧中删除不在另一个数据帧的索引中的列 - 错误TypeError:unhashable type:'numpy.ndarray' - Pandas remove columns from one dataframe that are not in the index of another dataframe - error TypeError: unhashable type: 'numpy.ndarray' 尝试使用DatetimeIndex标准化Pandas DataFrame中的列时出现错误`unhashable type:'Index'` - error `unhashable type: 'Index'` on attempting to standardize columns in Pandas DataFrame with DatetimeIndex 尝试从 Python 中的 Pandas 数据框列获取唯一值时,如何克服不可散列类型:“列表”错误 - How to overcome unhashable type: 'list' error, when trying to get unique values from a pandas dataframe column in Python TypeError:unhashable类型:'sl​​ice'pandas DataFrame列 - TypeError: unhashable type: 'slice' pandas DataFrame column 类型错误:Pandas Dataframe 应用函数,参数传递 - Type Error: Pandas Dataframe apply function, argument passing 数据框:尝试修复不可散列的类型:“列表”错误 - dataframe: Trying to fix unhashable type: 'list' error Unhashable DataFrame - Groupby 函数 - Unhashable DataFrame - Groupby function 键入错误:从特定列pandas dataframe中选择子集时不可用类型'list' - Type error: unhashable type 'list' while selecting subset from specific columns pandas dataframe Pandas dataFrame.nunique(): ("unhashable type: 'list'", 'occured at index columns') - Pandas dataFrame.nunique() : ("unhashable type : 'list'", 'occured at index columns')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM