Debian Codegraph @

docs/source/dataframe-create.rst

raw
1 Load and Save Data with Dask DataFrames
2 =======================================
3
4 .. meta::
5 :description: Learn how to create DataFrames and store them. Create a Dask DataFrame from various data storage formats like CSV, HDF, Apache Parquet, and others.
6
7 You can create a Dask DataFrame from various data storage formats
8 like CSV, HDF, Apache Parquet, and others. For most formats, this data can live on various
9 storage systems including local disk, network file systems (NFS), the Hadoop
10 Distributed File System (HDFS), Google Cloud Storage, and Amazon S3
11 (excepting HDF, which is only available on POSIX like file systems).
12
13 See the :doc:`DataFrame overview page <dataframe>` for more on
14 ``dask.dataframe`` scope, use, and limitations and
15 :doc:`DataFrame Best Practices <dataframe-best-practices>` for more tips
16 and solutions to common problems.
17
18 API
19 ---
20
21 The following functions provide access to convert between Dask DataFrames,
22 file formats, and other Dask or Python collections.
23
24 .. currentmodule:: dask.dataframe
25
26 File Formats:
27
28 .. autosummary::
29 read_csv
30 read_parquet
31 read_hdf
32 read_orc
33 read_json
34 read_sql_table
35 read_sql_query
36 read_sql
37 read_table
38 read_fwf
39 from_array
40 to_csv
41 to_parquet
42 to_hdf
43 to_sql
44
45 Dask Collections:
46
47 .. autosummary::
48 from_delayed
49 from_dask_array
50 from_map
51 dask.bag.core.Bag.to_dataframe
52 DataFrame.to_delayed
53 to_records
54 to_bag
55
56 Pandas:
57
58 .. autosummary::
59 from_pandas
60 DataFrame.from_dict
61
62 Other File Formats:
63
64 - `Snowflake <https://github.com/coiled/dask-snowflake>`_
65 - `Bigquery <https://github.com/coiled/dask-bigquery>`_
66 - `Delta Lake <https://github.com/dask-contrib/dask-deltatable>`_
67
68 Creating
69 --------
70
71 Read from CSV
72 ~~~~~~~~~~~~~
73
74 You can use :func:`read_csv` to read one or more CSV files into a Dask DataFrame.
75 It supports loading multiple files at once using globstrings:
76
77 .. code-block:: python
78
79 >>> df = dd.read_csv('myfiles.*.csv')
80
81 You can break up a single large file with the ``blocksize`` parameter:
82
83 .. code-block:: python
84
85 >>> df = dd.read_csv('largefile.csv', blocksize=25e6) # 25MB chunks
86
87 Changing the ``blocksize`` parameter will change the number of partitions (see the explanation on
88 :ref:`partitions <dataframe-design-partitions>`). A good rule of thumb when working with
89 Dask DataFrames is to keep your partitions under 100MB in size.
90
91 Read from Parquet
92 ~~~~~~~~~~~~~~~~~
93
94 Similarly, you can use :func:`read_parquet` for reading one or more Parquet files.
95 You can read in a single Parquet file:
96
97 .. code-block:: python
98
99 >>> df = dd.read_parquet("path/to/mydata.parquet")
100
101 Or a directory of local Parquet files:
102
103 .. code-block:: python
104
105 >>> df = dd.read_parquet("path/to/my/parquet/")
106
107 For more details on working with Parquet files, including tips and best practices, see the documentation
108 on :doc:`dataframe-parquet`.
109
110 Read from cloud storage
111 ~~~~~~~~~~~~~~~~~~~~~~~
112
113 Dask can read data from a variety of data stores including cloud object stores.
114 You can do this by prepending a protocol like ``s3://`` to paths
115 used in common data access functions like ``dd.read_csv``:
116
117 .. code-block:: python
118
119 >>> df = dd.read_csv('s3://bucket/path/to/data-*.csv')
120 >>> df = dd.read_parquet('gcs://bucket/path/to/data-*.parq')
121
122 For remote systems like Amazon S3 or Google Cloud Storage, you may need to provide credentials.
123 These are usually stored in a configuration file, but in some cases you may want to pass
124 storage-specific options through to the storage backend.
125 You can do this with the ``storage_options`` parameter:
126
127 .. code-block:: python
128
129 >>> df = dd.read_csv('s3://bucket-name/my-data-*.csv',
130 ... storage_options={'anon': True})
131 >>> df = dd.read_parquet('gs://dask-nyc-taxi/yellowtrip.parquet',
132 ... storage_options={'token': 'anon'})
133
134 See the documentation on connecting to :ref:`Amazon S3 <connect-to-remote-data-s3>` or
135 :ref:`Google Cloud Storage <connect-to-remote-data-gc>`.
136
137 Mapping from a function
138 ~~~~~~~~~~~~~~~~~~~~~~~
139
140 For cases that are not covered by the functions above, but *can* be
141 captured by a simple ``map`` operation, :func:`from_map` is likely to be
142 the most convenient means for DataFrame creation. For example, this
143 API can be used to convert an arbitrary PyArrow ``Dataset`` object into a
144 DataFrame collection by mapping fragments to DataFrame partitions:
145
146 .. code-block:: python
147
148 >>> import pyarrow.dataset as ds
149 >>> dataset = ds.dataset("hive_data_path", format="orc", partitioning="hive")
150 >>> fragments = dataset.get_fragments()
151 >>> func = lambda frag: frag.to_table().to_pandas()
152 >>> df = dd.from_map(func, fragments)
153
154
155 Dask Delayed
156 ~~~~~~~~~~~~
157
158 :doc:`Dask delayed<delayed>` is particularly useful when simple ``map`` operations aren’t sufficient to capture the complexity of your data layout. It lets you construct Dask DataFrames out of arbitrary Python function calls, which can be
159 helpful to handle custom data formats or bake in particular logic around loading data.
160 See the :doc:`documentation on using dask.delayed with collections<delayed-collections>`.
161
162 Storing
163 -------
164
165 Writing files locally
166 ~~~~~~~~~~~~~~~~~~~~~
167
168 You can save files locally, assuming each worker can access the same file system. The workers could be located on the same machine, or a network file system can be mounted and referenced at the same path location for every worker node.
169 See the documentation on :ref:`accessing data locally <connect-to-remote-data-local>`.
170
171 Writing to remote locations
172 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
173
174 Dask can write to a variety of data stores including cloud object stores.
175 For example, you can write a ``dask.dataframe`` to an Azure storage blob as:
176
177 .. code-block:: python
178
179 >>> d = {'col1': [1, 2, 3, 4], 'col2': [5, 6, 7, 8]}
180 >>> df = dd.from_pandas(pd.DataFrame(data=d), npartitions=2)
181 >>> dd.to_parquet(df=df,
182 ... path='abfs://CONTAINER/FILE.parquet'
183 ... storage_options={'account_name': 'ACCOUNT_NAME',
184 ... 'account_key': 'ACCOUNT_KEY'}
185
186 See the :doc:`how-to guide on connecting to remote data <how-to/connect-to-remote-data>`.