有没有python pandas的实现可以将数据缓存到磁盘上,这样我就可以避免每次都重现数据?
特别是,对于financial的get_yahoo_data
,是否有一种缓存方法?
一个非常好的优点是:
下载相同源的新数据时,只需向write
发布于 2018-07-09 05:25:52
实现这一点的方法有很多,但最简单的方法可能是使用内置方法来编写和读取Python pickles。您可以使用pandas.DataFrame.to_pickle
将DataFrame存储到磁盘,使用pandas.read_pickle
从磁盘读取存储的DataFrame。
pandas.DataFrame
的一个示例
# Store your DataFrame
df.to_pickle('cached_dataframe.pkl') # will be stored in current directory
# Read your DataFrame
df = pandas.read_pickle('cached_dataframe.pkl') # read from current directory
同样的方法也适用于pandas.Series
# Store your Series
series.to_pickle('cached_series.pkl') # will be stored in current directory
# Read your DataFrame
series = pandas.read_pickle('cached_series.pkl') # read from current directory
发布于 2018-07-09 08:53:22
根据不同的需求,有a dozen of methods来做这件事,来回,在CSV,Excel,JSON,Python Pickle格式,HDF5甚至SQL with DB等等。
就代码行而言,to/read
这些格式中的许多格式只是每个方向的一行代码。Python和Pandas已经让代码尽可能的干净,所以你不用担心这一点。
我认为没有一个单一的解决方案可以满足所有的需求,实际上是逐个案例:
用于保存数据的人类可读性的
如果你想每天更新股票价格,为了以后的使用,我更喜欢Pandas with SQL Queries,当然这会增加几行代码来建立数据库连接:
from sqlalchemy import create_engine
new_data = getting_daily_price()
# You can also choose other db drivers instead of `sqlalchemy`
engine = create_engine('sqlite:///:memory:')
with engine.connect() as conn:
new_data.to_sql('table_name', conn) # To Write
df = pd.read_sql_table('sql_query', conn) # To Read
发布于 2020-07-01 21:09:20
您可以使用Data cache包。
from data_cache import pandas_cache
@pandas_cache
def foo():
...
https://stackoverflow.com/questions/51235360
复制相似问题