[英]'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.