federleicht
v0.1.0
federleicht是一个Python软件包,可为pandas.DataFrame提供缓存装饰,并使用轻巧有效的pyarrow羽毛文件格式。
federleicht.cache_dataframe旨在装饰返回pandas.DataFrame对象的功能。装饰器将数据框保存到第一个呼叫上的羽毛文件中,并在文件存在时自动加载其在后续呼叫上加载。
pandas.DataFrame毫不费力地使用以速度和简单性而闻名的羽毛格式。要实施缓存到期, federleicht要求装饰功能的所有参数都是可序列化的。缓存将在以下条件下到期:
args / kwargs )更改,缓存将到期。os.PathLike对象作为参数传递时,如果文件大小和 /或修改时间更改,缓存将到期。timedelta年龄大时会过期。os.PathLikepandas.DataFramepandas.Seriesnumpy.ndarraydatetime.datetimetypes.FunctionType 从PYPI安装Federleicht:
pip install federleicht通常, md5用于哈希参数,但是对于更快的哈希,您可以尝试xxhash作为可选依赖性:
pip install federleicht[xxhash]这是一个快速示例:
import pandas as pd
from federleicht import cache_dataframe
@ cache_dataframe
def generate_large_dataframe ():
# Simulate a heavy computation
return pd . DataFrame ({ "col1" : range ( 10000 ), "col2" : range ( 10000 )})
df = generate_large_dataframe ()用于基准cache_dataframe装饰器的性能的功能。
def read_data ( file : str , ** kwargs ) -> pd . DataFrame :
"""
Read the earthquake dataset from a CSV file to Benchmark cache.
Perform some data type conversions and return the DataFrame.
"""
df = pd . read_csv (
file ,
header = 0 ,
dtype = {
"status" : "category" ,
"tsunami" : "boolean" ,
"data_type" : "category" ,
"state" : "category" ,
},
** kwargs ,
)
df [ "time" ] = pd . to_datetime ( df [ "time" ], unit = "ms" )
df [ "date" ] = pd . to_datetime ( df [ "date" ], format = "mixed" )
return df pandas.DataFrame没有attrs字典的dataframe将在.pandas_cache Directory中被缓存,并且只有在文件更改时才会到期。有关更多详细信息,请参见“缓存到期”部分。
@ cache_dataframe
def read_cache ( file : pathlib . Path , ** kwargs ) -> pd . DataFrame :
return read_data ( file , ** kwargs )结果在很大程度上取决于系统配置和文件系统。获得以下结果:
| nrows | read_data [s] | build_cache [s] | read_cache [s] |
|---|---|---|---|
| 10000 | 0.060 | 0.076 | 0.037 |
| 32170 | 0.172 | 0.193 | 0.033 |
| 103493 | 0.536 | 0.569 | 0.067 |
| 332943 | 1.658 | 1.791 | 0.143 |
| 1071093 | 5.383 | 5.465 | 0.366 |
| 3445752 | 16.750 | 17.720 | 1.141 |