简体   繁体   English

'DataFrame' object 在使用装饰器包装器进行运行时是不可调用错误

[英]'DataFrame' object is not callable error when using Decorator wrapper for run time

I am trying to return a csv file and using decorator for finding the running time.我正在尝试返回 csv 文件并使用装饰器来查找运行时间。 (The following code is the sample code for the same. ) But I am getting an error it is "'DataFrame' object is not callable " (以下代码是相同的示例代码。)但是我收到一个错误,它是“'DataFrame' object is not callable”

import pandas as pd
import time
import functools

def timer(func):
    @functools.wraps(func)
    def wrapper_timer():
        start_time = time.perf_counter()  
        value = func()
        end_time = time.perf_counter()  
        run_time = end_time - start_time  
        print(f"Finished {func.__name__!r} in {run_time:.4f} secs")
        return value
    return wrapper_timer()


@timer
def generate_df():
    file="file.csv"
    df = pd.read_csv(file)
    return df
df = generate_df()


if __name__ == '__main__':
    print(df.head())

You should be returning the function reference in the decorator您应该在装饰器中返回 function 参考

return wrapper_timer

Rather than calling it.而不是调用它。

A better way to write the code,编写代码的更好方法,

import pandas as pd
from time import time
import functools


def timer(func):
    @functools.wraps(func)
    def wrapper_timer(*args, **kwargs):
        start_time = time()
        value = func(*args, **kwargs)
        run_time = time() - start_time  
        print(f"Finished {func.__name__!r} in {run_time:.4f} secs")
        return value
    return wrapper_timer


@timer
def generate_df(file):
    df = pd.read_csv(file)
    return df


if __name__ == '__main__':
    filepath = "/home/vishnudev/Downloads/CF-Event-equities-21-Feb-2021.csv"
    df = generate_df(filepath)
    print(df.head())

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

相关问题 装饰器错误:NoneType 对象不可调用 - Decorator error: NoneType object is not callable TypeError: 'DataFrame' object is not callable error when using seaborn pairplot ? - TypeError: 'DataFrame' object is not callable error when using seaborn pairplot ? 热图错误:'NoneType' object 与 dataframe 一起使用时不可调用 - Heatmap error :'NoneType' object is not callable when using with dataframe 错误:“模块”对象在使用 logmmse 时不可调用 - error : 'module' object is not callable when using logmmse 装饰器“对象不可调用” - Decorator "object is not callable" 装饰器“ NoneType”对象不可调用 - decorator 'NoneType' object is not callable 当我尝试创建新的 df 时,“DataFrame”对象不可调用错误 - 'DataFrame' object is not callable error when I try to create a new df 将 pandas dataframe 转换为字符串时,“系列”object 是不可调用错误 - 'Series' object is not callable error when converting pandas dataframe to string 使用装饰器更新包装器时遇到错误 - Error encountered using decorator to update wrapper TypeError:使用pd.Series.map时无法调用“ DataFrame”对象 - TypeError: 'DataFrame' object is not callable when using pd.Series.map
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM