Dask Dataframes coordinate many Pandas dataframes, partitioned along an index. They support a large subset of the Pandas API.
Starting the Dask Client is optional. It will provide a dashboard which is useful to gain insight on the computation.
The link to the dashboard will become visible when you create the client below. We recommend having it open on one side of your screen while using your notebook on the other side. This can take some effort to arrange your windows, but seeing them both at the same is very useful when learning.
from dask.distributed import Client, progress
client = Client(n_workers=2, threads_per_worker=2, memory_limit='1GB')
client
We create a random timeseries of data with the following attributes:
import dask.dataframe as dd
df = dd.demo.make_timeseries('2000-01-01', '2000-12-31', freq='10s', partition_freq='1M',
dtypes={'name': str, 'id': int, 'x': float, 'y': float})
df
df.head(3)
df.dtypes
Most common Pandas operations operate identically on Dask dataframes
df2 = df[df.y > 0]
df3 = df2.groupby('name').x.std()
df3
Call .compute()
when you want your result as a Pandas dataframe.
If you started Client()
above then you may want to watch the status page during computation.
df3.compute()
If you have the available RAM for your dataset then you can persist data in memory.
This allows future computations to be much faster.
df = df.persist()
Because we have a datetime index time-series operations work efficiently
%matplotlib inline
df[['x', 'y']].resample('1w').mean().head()
df[['x', 'y']].resample('1w').mean().compute().plot()
df[['x', 'y']].rolling(window='7d').mean().head()
Random access is cheap along the index
df['2000-05-05']
%time df['2000-05-05'].compute()
Data is sorted by the index column. This allows for faster access, joins, groupby-apply operations, etc.. However sorting data can be costly to do in parallel, so setting the index is both important to do, but only infrequently.
df = df.set_index('name')
df
Because computing this dataset is expensive and we can fit it in our available RAM, we persist the dataset to memory.
df = df.persist()
Dask now knows where all data lives, indexed cleanly by name. As a result oerations like random access are cheap and efficient
%time df.loc['Alice'].compute()
Now that our data is sorted by name we can easily do operations like random access on name, or groupby-apply with custom functions.
Here we train a different Scikit-Learn linear regression model on each name.
from sklearn.linear_model import LinearRegression
def train(partition):
est = LinearRegression()
est.fit(partition[['x']].values, partition.y.values)
return est
df.groupby('name').apply(train, meta=object).compute()