federleicht เป็นแพ็คเกจ Python ที่ให้บริการตกแต่งแคชสำหรับ pandas.DataFrame โดยใช้รูปแบบไฟล์ขนนก pyarrow ที่มีน้ำหนักเบาและมีประสิทธิภาพ
federleicht.cache_dataframe ได้รับการออกแบบมาเพื่อตกแต่งฟังก์ชั่นที่ส่งคืนวัตถุ pandas.DataFrame มัณฑนากรที่จะบันทึก dataframe ไปยังไฟล์ขนนกในการโทรครั้งแรกและโหลดโดยอัตโนมัติเมื่อมีการโทรที่ตามมาหากไฟล์มีอยู่
pandas.DataFrame ได้อย่างง่ายดายโดยใช้รูปแบบขนนกที่รู้จักกันดีในเรื่องความเร็วและความเรียบง่าย ในการใช้แคชหมดอายุ federleicht ต้องการข้อโต้แย้งทั้งหมดของฟังก์ชั่นที่ตกแต่งเพื่อให้เป็นอนุกรม แคชจะหมดอายุภายใต้เงื่อนไขดังต่อไปนี้:
args / kwargs ) ของการเปลี่ยนแปลงฟังก์ชั่นการตกแต่งos.PathLike ถูกส่งผ่านเป็นอาร์กิวเมนต์แคชจะหมดอายุหากขนาดไฟล์และ / หรือการเปลี่ยนแปลงเวลาเปลี่ยนไปtimedelta ที่กำหนดos.PathLikepandas.DataFramepandas.Seriesnumpy.ndarraydatetime.datetimetypes.FunctionType ติดตั้ง federleicht จาก PYPI:
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 จะถูกแคชในไดเรกทอรี .pandas_cache และจะหมดอายุเฉพาะเมื่อไฟล์เปลี่ยนไป สำหรับรายละเอียดเพิ่มเติมดูที่ส่วนของแคชหมดอายุ
@ cache_dataframe
def read_cache ( file : pathlib . Path , ** kwargs ) -> pd . DataFrame :
return read_data ( file , ** kwargs )ผลลัพธ์ขึ้นอยู่กับการกำหนดค่าระบบและระบบไฟล์ ผลลัพธ์ต่อไปนี้ได้รับบน:
| การเดินเล่น | read_data [s] | build_cache [s] | read_cache [s] |
|---|---|---|---|
| 10,000 | 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 |