| 1 |
.. _dataframe.parquet: |
| 2 |
|
| 3 |
Dask Dataframe and Parquet |
| 4 |
========================== |
| 5 |
|
| 6 |
.. currentmodule:: dask.dataframe |
| 7 |
|
| 8 |
Parquet_ is a popular, columnar file format designed for efficient data storage |
| 9 |
and retrieval. Dask dataframe includes :func:`read_parquet` and |
| 10 |
:func:`to_parquet` functions/methods for reading and writing parquet files |
| 11 |
respectively. Here we document these methods, and provide some tips and best |
| 12 |
practices. |
| 13 |
|
| 14 |
Parquet I/O requires ``pyarrow`` to be installed. |
| 15 |
|
| 16 |
|
| 17 |
Reading Parquet Files |
| 18 |
--------------------- |
| 19 |
|
| 20 |
.. autosummary:: |
| 21 |
read_parquet |
| 22 |
|
| 23 |
Dask dataframe provides a :func:`read_parquet` function for reading one or more |
| 24 |
parquet files. Its first argument is one of: |
| 25 |
|
| 26 |
- A path to a single parquet file |
| 27 |
- A path to a directory of parquet files (files with ``.parquet`` or ``.parq`` extension) |
| 28 |
- A `glob string`_ expanding to one or more parquet file paths |
| 29 |
- A list of parquet file paths |
| 30 |
|
| 31 |
These paths can be local, or point to some remote filesystem (for example S3_ |
| 32 |
or GCS_) by prepending the path with a protocol. |
| 33 |
|
| 34 |
.. code-block:: python |
| 35 |
|
| 36 |
>>> import dask.dataframe as dd |
| 37 |
|
| 38 |
# Load a single local parquet file |
| 39 |
>>> df = dd.read_parquet("path/to/mydata.parquet") |
| 40 |
|
| 41 |
# Load a directory of local parquet files |
| 42 |
>>> df = dd.read_parquet("path/to/my/parquet/") |
| 43 |
|
| 44 |
# Load a directory of parquet files from S3 |
| 45 |
>>> df = dd.read_parquet("s3://bucket-name/my/parquet/") |
| 46 |
|
| 47 |
Note that for remote filesystems you may need to configure credentials. When |
| 48 |
possible we recommend handling these external to Dask through |
| 49 |
filesystem-specific configuration files/environment variables. For example, you |
| 50 |
may wish to store S3 credentials using the `AWS credentials file`_. |
| 51 |
Alternatively, you can pass configuration on to the fsspec_ backend through the |
| 52 |
``storage_options`` keyword argument: |
| 53 |
|
| 54 |
.. code-block:: python |
| 55 |
|
| 56 |
>>> df = dd.read_parquet( |
| 57 |
... "s3://bucket-name/my/parquet/", |
| 58 |
... storage_options={"anon": True} # passed to `s3fs.S3FileSystem` |
| 59 |
... ) |
| 60 |
|
| 61 |
For more information on connecting to remote data, see |
| 62 |
:doc:`how-to/connect-to-remote-data`. |
| 63 |
|
| 64 |
:func:`read_parquet` has many configuration options affecting both behavior and |
| 65 |
performance. Here we highlight a few common options. |
| 66 |
|
| 67 |
Metadata |
| 68 |
~~~~~~~~ |
| 69 |
|
| 70 |
When :func:`read_parquet` is used to read *multiple files*, it first loads |
| 71 |
metadata about the files in the dataset. This metadata may include: |
| 72 |
|
| 73 |
- The dataset schema |
| 74 |
- How the dataset is partitioned into files, and those files into row-groups |
| 75 |
|
| 76 |
Some parquet datasets include a ``_metadata`` file which aggregates per-file |
| 77 |
metadata into a single location. For small-to-medium sized datasets this *may* |
| 78 |
be useful because it makes accessing the row-group metadata possible without |
| 79 |
reading parts of *every* file in the dataset. Row-group metadata allows Dask to |
| 80 |
split large files into smaller in-memory partitions, and merge many small files |
| 81 |
into larger partitions, possibly leading to higher performance. |
| 82 |
|
| 83 |
However, for large datasets the ``_metadata`` file can be problematic because |
| 84 |
it may be too large for a single endpoint to parse! If this is true, you can |
| 85 |
disable loading the ``_metadata`` file by specifying |
| 86 |
``ignore_metadata_file=True``. |
| 87 |
|
| 88 |
.. code-block:: python |
| 89 |
|
| 90 |
>>> df = dd.read_parquet( |
| 91 |
... "s3://bucket-name/my/parquet/", |
| 92 |
... ignore_metadata_file=True # don't read the _metadata file |
| 93 |
... ) |
| 94 |
|
| 95 |
Partition Size |
| 96 |
~~~~~~~~~~~~~~ |
| 97 |
|
| 98 |
By default, Dask will use metadata from the first parquet file in the dataset |
| 99 |
to infer whether or not it is safe load each file individually as a partition |
| 100 |
in the Dask dataframe. If the uncompressed byte size of the parquet data |
| 101 |
exceeds ``blocksize`` (which is 256 MiB by default), then each partition will |
| 102 |
correspond to a range of parquet row-groups instead of the entire file. |
| 103 |
|
| 104 |
For best performance, use files that can be individually mapped to good |
| 105 |
dataframe partition sizes, and set ``blocksize`` accordingly. If individual |
| 106 |
files need to be divided into multiple row-group ranges, and the dataset |
| 107 |
does not contain a ``_metadata`` file, Dask will need to load all footer |
| 108 |
metadata up-front. |
| 109 |
|
| 110 |
We recommend aiming for 100-300 MiB in-memory size per file once loaded into |
| 111 |
pandas. Oversized partitions can lead to excessive memory usage on a single |
| 112 |
worker, while undersized partitions can lead to poor performance as the |
| 113 |
overhead of Dask dominates. |
| 114 |
|
| 115 |
If you know your parquet dataset comprises oversized files, you can pass |
| 116 |
``split_row_groups='adaptive'`` to ensure that Dask will attempt to keep |
| 117 |
each partition under the ``blocksize`` limit. Note that partitions may |
| 118 |
still exceed ``blocksize`` if one or more row-groups are too large. |
| 119 |
|
| 120 |
Column Selection |
| 121 |
~~~~~~~~~~~~~~~~ |
| 122 |
|
| 123 |
When loading parquet data, sometimes you don't need all the columns available |
| 124 |
in the dataset. In this case, you likely want to specify the subset of columns |
| 125 |
you need via the ``columns`` keyword argument. This is beneficial for a few |
| 126 |
reasons: |
| 127 |
|
| 128 |
- It lets Dask read less data from the backing filesystem, reducing IO costs |
| 129 |
- It lets Dask load less data into memory, reducing memory usage |
| 130 |
|
| 131 |
.. code-block:: python |
| 132 |
|
| 133 |
>>> dd.read_parquet( |
| 134 |
... "s3://path/to/myparquet/", |
| 135 |
... columns=["a", "b", "c"] # Only read columns 'a', 'b', and 'c' |
| 136 |
... ) |
| 137 |
|
| 138 |
Calculating Divisions |
| 139 |
~~~~~~~~~~~~~~~~~~~~~ |
| 140 |
|
| 141 |
By default, :func:`read_parquet` will **not** produce a collection with |
| 142 |
known divisions. However, you can pass ``calculate_divisions=True`` to |
| 143 |
tell Dask that you want to use row-group statistics from the footer |
| 144 |
metadata (or global ``_metadata`` file) to calculate the divisions at |
| 145 |
graph-creation time. Using this option will not produce known |
| 146 |
divisions if any of the necessary row-group statistics are missing, |
| 147 |
or if no index column is detected. Using the ``index`` argument is the |
| 148 |
best way to ensure that the desired field will be treated as the index. |
| 149 |
|
| 150 |
.. code-block:: python |
| 151 |
|
| 152 |
>>> dd.read_parquet( |
| 153 |
... "s3://path/to/myparquet/", |
| 154 |
... index="timestamp", # Specify a specific index column |
| 155 |
... calculate_divisions=True, # Calculate divisions from metadata |
| 156 |
... ) |
| 157 |
|
| 158 |
Although using ``calculate_divisions=True`` does not require any *real* |
| 159 |
data to be read from the parquet file(s), it does require Dask to load |
| 160 |
and process metadata for every row-group in the dataset. For this reason, |
| 161 |
calculating divisions should be avoided for large datasets without a |
| 162 |
global ``_metadata`` file. This is especially true for remote storage. |
| 163 |
|
| 164 |
For more information about divisions, see :ref:`dataframe.design`. |
| 165 |
|
| 166 |
Writing |
| 167 |
------- |
| 168 |
|
| 169 |
.. autosummary:: |
| 170 |
to_parquet |
| 171 |
DataFrame.to_parquet |
| 172 |
|
| 173 |
Dask dataframe provides a :func:`to_parquet` function and method for writing |
| 174 |
parquet files. |
| 175 |
|
| 176 |
In its simplest usage, this takes a path to the directory in which to write the |
| 177 |
dataset. This path may be local, or point to some remote filesystem (for |
| 178 |
example S3_ or GCS_) by prepending the path with a protocol. |
| 179 |
|
| 180 |
.. code-block:: python |
| 181 |
|
| 182 |
# Write to a local directory |
| 183 |
>>> df.to_parquet("path/to/my/parquet/") |
| 184 |
|
| 185 |
# Write to S3 |
| 186 |
>>> df.to_parquet("s3://bucket-name/my/parquet/") |
| 187 |
|
| 188 |
Note that for remote filesystems you may need to configure credentials. When |
| 189 |
possible we recommend handling these external to Dask through |
| 190 |
filesystem-specific configuration files/environment variables For example, you |
| 191 |
may wish to store S3 credentials using the `AWS credentials file`_. |
| 192 |
Alternatively, you can pass configuration on to the fsspec_ backend through the |
| 193 |
``storage_options`` keyword argument: |
| 194 |
|
| 195 |
.. code-block:: python |
| 196 |
|
| 197 |
>>> df.to_parquet( |
| 198 |
... "s3://bucket-name/my/parquet/", |
| 199 |
... storage_options={"anon": True} # passed to `s3fs.S3FileSystem` |
| 200 |
... ) |
| 201 |
|
| 202 |
For more information on connecting to remote data, see |
| 203 |
:doc:`how-to/connect-to-remote-data`. |
| 204 |
|
| 205 |
Dask will write one file per Dask dataframe partition to this directory. To |
| 206 |
optimize access for downstream consumers, we recommend aiming for an in-memory |
| 207 |
size of 100-300 MiB per partition. This helps balance worker memory usage |
| 208 |
against Dask overhead. You may find the |
| 209 |
:meth:`DataFrame.memory_usage_per_partition` method useful for determining if |
| 210 |
your data is partitioned optimally. |
| 211 |
|
| 212 |
:func:`to_parquet` has many configuration options affecting both behavior and |
| 213 |
performance. Here we highlight a few common options. |
| 214 |
|
| 215 |
Metadata |
| 216 |
~~~~~~~~ |
| 217 |
|
| 218 |
In order to improve *read* performance, Dask can optionally write out |
| 219 |
a global ``_metadata`` file at write time by aggregating the row-group |
| 220 |
metadata from every file in the dataset. While potentially useful at |
| 221 |
read time, the generation of this file may result in excessive memory |
| 222 |
usage at scale (and potentially killed Dask workers). As such, |
| 223 |
enabling the writing of this file is only recommended for small to |
| 224 |
moderate dataset sizes. |
| 225 |
|
| 226 |
.. code-block:: python |
| 227 |
|
| 228 |
>>> df.to_parquet( |
| 229 |
... "s3://bucket-name/my/parquet/", |
| 230 |
... write_metadata_file=True # enable writing the _metadata file |
| 231 |
... ) |
| 232 |
|
| 233 |
File Names |
| 234 |
~~~~~~~~~~ |
| 235 |
|
| 236 |
Unless the `partition_on` option is used (see :doc:`dataframe-hive`), |
| 237 |
:func:`to_parquet` will write one file per Dask dataframe partition to the |
| 238 |
output directory. By default these files will have names like |
| 239 |
``part.0.parquet``, ``part.1.parquet``, etc. If you wish to alter this naming |
| 240 |
scheme, you can use the ``name_function`` keyword argument. This takes a |
| 241 |
function with the signature ``name_function(partition: int) -> str``, taking |
| 242 |
the partition index for each Dask dataframe partition and returning a string to |
| 243 |
use as the filename. Note that names returned must sort in the same order as |
| 244 |
their partition indices. |
| 245 |
|
| 246 |
.. code-block:: python |
| 247 |
|
| 248 |
>>> df.npartitions # 3 partitions (0, 1, and 2) |
| 249 |
3 |
| 250 |
|
| 251 |
>>> df.to_parquet("/path/to/output", name_function=lambda i: f"data-{i}.parquet") |
| 252 |
|
| 253 |
>>> os.listdir("/path/to/parquet") |
| 254 |
["data-0.parquet", "data-1.parquet", "data-2.parquet"] |
| 255 |
|
| 256 |
Hive Partitioning |
| 257 |
~~~~~~~~~~~~~~~~~ |
| 258 |
|
| 259 |
It is sometimes useful to write a parquet dataset with a hive-like directory scheme |
| 260 |
(e.g. ``'/year=2022/month=12/day=25'``). :func:`to_parquet` will automatically |
| 261 |
produce a dataset with this kind of directory structure when the ``partition_on`` |
| 262 |
option is used. In most cases, :func:`to_parquet` will handle hive partitioning |
| 263 |
automatically. See :doc:`dataframe-hive` for more information. |
| 264 |
|
| 265 |
.. _parquet: https://parquet.apache.org/ |
| 266 |
.. _glob string: https://docs.python.org/3/library/glob.html |
| 267 |
.. _fsspec: https://filesystem-spec.readthedocs.io |
| 268 |
.. _s3: https://aws.amazon.com/s3/ |
| 269 |
.. _gcs: https://cloud.google.com/storage |
| 270 |
.. _AWS credentials file: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html#shared-credentials-file>`_. |