I am reading data stored in parquet files into dask, and then attempting to select the data by a categorical index field. When saving the data in parquet, I partition the data by the same field that I'm using as the index field in dask.
I expect the dask set_index operation to take some time, but it seems like subsequent "select" type operations based on the index field should then be very fast. However, they are quite slow. Here's a minimal example. Warning, this example will create about 9.5G of data on disk, and take a few minutes to run:
import dask
from dask import dataframe as ddf
from dask.distributed import Client, LocalCluster
import pandas as pd
import numpy as np
cluster = LocalCluster(n_workers=16)
client = Client(cluster)
# Create parquet files. Takes about 1.5 mins to run, creates ~9.5G of data
for i in range(0,100):
category = f'category-{i}'
data = np.random.random_sample((10000,1000))
df = pd.DataFrame(data)
df['category'] = category
df.columns = df.columns.map(str)
df.to_parquet('/tmp/dasktest/', partition_cols=['category'], compression=None)
# Load all data, index all data, and then attempt to read data for one category
df = ddf.read_parquet('/tmp/dasktest/', npartitions=16)
df.category = df.category.cat.as_ordered()
df = df.set_index(df.category) #takes only 8s to run, this is fine.
#the below statement takes about 35 seconds to run, even though the data is only 94M on disk, and the parquet store is partitioned by the "category" column
data_df = df.loc[df.category == 'category-1'].compute()
#manually reading the data for one category takes about 200ms
pd_data_df = pd.read_parquet('/tmp/dasktest/category=category-1')
Is there a better way to improve the speed using dask, without manually just reading from the appropriate partitioned folder?