{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# DataFrames: Reading in messy data\n", " \n", "In the [01-data-access](./01-data-access.ipynb) example we show how Dask Dataframes can read and store data in many of the same formats as Pandas dataframes. One key difference, when using Dask Dataframes is that instead of opening a single file with a function like [pandas.read_csv](https://docs.dask.org/en/latest/generated/dask.dataframe.read_csv.html), we typically open many files at once with [dask.dataframe.read_csv](https://docs.dask.org/en/latest/dataframe-api.html#dask.dataframe.read_csv). This enables us to treat a collection of files as a single dataset. Most of the time this works really well. But real data is messy and in this notebook we will explore a more advanced technique to bring messy datasets into a dask dataframe." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Start Dask Client for Dashboard\n", "\n", "Starting the Dask Client is optional. It will provide a dashboard which \n", "is useful to gain insight on the computation. \n", "\n", "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." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "execution": { "iopub.execute_input": "2022-05-16T13:57:40.481377Z", "iopub.status.busy": "2022-05-16T13:57:40.481023Z", "iopub.status.idle": "2022-05-16T13:57:42.496125Z", "shell.execute_reply": "2022-05-16T13:57:42.495490Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "
\n", "
\n", "

Client

\n", "

Client-26d54db6-d520-11ec-a232-000d3aeabb7a

\n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", "
Connection method: Cluster objectCluster type: distributed.LocalCluster
\n", " Dashboard: http://127.0.0.1:8787/status\n", "
\n", "\n", " \n", "
\n", "

Cluster Info

\n", "
\n", "
\n", "
\n", "
\n", "

LocalCluster

\n", "

177239e4

\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", "\n", " \n", "
\n", " Dashboard: http://127.0.0.1:8787/status\n", " \n", " Workers: 1\n", "
\n", " Total threads: 4\n", " \n", " Total memory: 1.86 GiB\n", "
Status: runningUsing processes: True
\n", "\n", "
\n", " \n", "

Scheduler Info

\n", "
\n", "\n", "
\n", "
\n", "
\n", "
\n", "

Scheduler

\n", "

Scheduler-5c5a39b2-0eee-4be6-89c2-0c9f027e5450

\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", " Comm: tcp://127.0.0.1:39299\n", " \n", " Workers: 1\n", "
\n", " Dashboard: http://127.0.0.1:8787/status\n", " \n", " Total threads: 4\n", "
\n", " Started: Just now\n", " \n", " Total memory: 1.86 GiB\n", "
\n", "
\n", "
\n", "\n", "
\n", " \n", "

Workers

\n", "
\n", "\n", " \n", "
\n", "
\n", "
\n", "
\n", " \n", "

Worker: 0

\n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", " \n", "\n", " \n", "\n", "
\n", " Comm: tcp://127.0.0.1:34319\n", " \n", " Total threads: 4\n", "
\n", " Dashboard: http://127.0.0.1:34619/status\n", " \n", " Memory: 1.86 GiB\n", "
\n", " Nanny: tcp://127.0.0.1:38497\n", "
\n", " Local directory: /home/runner/work/dask-examples/dask-examples/dataframes/dask-worker-space/worker-rnv_x7jo\n", "
\n", "
\n", "
\n", "
\n", " \n", "\n", "
\n", "
\n", "\n", "
\n", "
\n", "
\n", "
\n", " \n", "\n", "
\n", "
" ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from dask.distributed import Client\n", "client = Client(n_workers=1, threads_per_worker=4, processes=True, memory_limit='2GB')\n", "client" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create artificial dataset\n", "\n", "First we create an artificial dataset and write it to many CSV files.\n", "\n", "You don't need to understand this section, we're just creating a dataset for the rest of the notebook." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "execution": { "iopub.execute_input": "2022-05-16T13:57:42.499613Z", "iopub.status.busy": "2022-05-16T13:57:42.499163Z", "iopub.status.idle": "2022-05-16T13:57:42.837861Z", "shell.execute_reply": "2022-05-16T13:57:42.837264Z" } }, "outputs": [ { "data": { "text/html": [ "
Dask DataFrame Structure:
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idnamexy
npartitions=30
2000-01-01int64objectfloat64float64
2000-01-02............
...............
2000-01-30............
2000-01-31............
\n", "
\n", "
Dask Name: make-timeseries, 30 tasks
" ], "text/plain": [ "Dask DataFrame Structure:\n", " id name x y\n", "npartitions=30 \n", "2000-01-01 int64 object float64 float64\n", "2000-01-02 ... ... ... ...\n", "... ... ... ... ...\n", "2000-01-30 ... ... ... ...\n", "2000-01-31 ... ... ... ...\n", "Dask Name: make-timeseries, 30 tasks" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import dask\n", "df = dask.datasets.timeseries()\n", "df" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "execution": { "iopub.execute_input": "2022-05-16T13:57:42.841303Z", "iopub.status.busy": "2022-05-16T13:57:42.840837Z", "iopub.status.idle": "2022-05-16T13:57:58.948028Z", "shell.execute_reply": "2022-05-16T13:57:58.947454Z" } }, "outputs": [], "source": [ "import os\n", "import datetime\n", "\n", "if not os.path.exists('data'):\n", " os.mkdir('data')\n", "\n", "def name(i):\n", " \"\"\" Provide date for filename given index\n", " \n", " Examples\n", " --------\n", " >>> name(0)\n", " '2000-01-01'\n", " >>> name(10)\n", " '2000-01-11'\n", " \"\"\"\n", " return str(datetime.date(2000, 1, 1) + i * datetime.timedelta(days=1))\n", " \n", "df.to_csv('data/*.csv', name_function=name, index=False);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Read CSV files\n", "\n", "We now have many CSV files in our data directory, one for each day in the month of January 2000. Each CSV file holds timeseries data for that day. We can read all of them as one logical dataframe using the `dd.read_csv` function with a glob string." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "execution": { "iopub.execute_input": "2022-05-16T13:57:58.951633Z", "iopub.status.busy": "2022-05-16T13:57:58.951198Z", "iopub.status.idle": "2022-05-16T13:57:59.113064Z", "shell.execute_reply": "2022-05-16T13:57:59.112211Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "data/2000-01-01.csv\r\n", "data/2000-01-02.csv\r\n", "data/2000-01-03.csv\r\n", "data/2000-01-04.csv\r\n", "data/2000-01-05.csv\r\n", "data/2000-01-06.csv\r\n", "data/2000-01-07.csv\r\n", "data/2000-01-08.csv\r\n", "data/2000-01-09.csv\r\n", "data/2000-01-10.csv\r\n" ] } ], "source": [ "!ls data/*.csv | head" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "execution": { "iopub.execute_input": "2022-05-16T13:57:59.117325Z", "iopub.status.busy": "2022-05-16T13:57:59.116734Z", "iopub.status.idle": "2022-05-16T13:57:59.147762Z", "shell.execute_reply": "2022-05-16T13:57:59.147176Z" } }, "outputs": [ { "data": { "text/html": [ "
Dask DataFrame Structure:
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idnamexy
npartitions=30
int64objectfloat64float64
............
...............
............
............
\n", "
\n", "
Dask Name: read-csv, 30 tasks
" ], "text/plain": [ "Dask DataFrame Structure:\n", " id name x y\n", "npartitions=30 \n", " int64 object float64 float64\n", " ... ... ... ...\n", "... ... ... ... ...\n", " ... ... ... ...\n", " ... ... ... ...\n", "Dask Name: read-csv, 30 tasks" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import dask.dataframe as dd\n", "\n", "df = dd.read_csv('data/2000-*-*.csv')\n", "df" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "execution": { "iopub.execute_input": "2022-05-16T13:57:59.150541Z", "iopub.status.busy": "2022-05-16T13:57:59.150242Z", "iopub.status.idle": "2022-05-16T13:57:59.250244Z", "shell.execute_reply": "2022-05-16T13:57:59.249331Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idnamexy
01035Norbert-0.7572080.410627
11088Hannah0.655573-0.618467
2979Dan0.1121890.363218
3983Edith0.547462-0.595247
41039Wendy0.7105900.695703
\n", "
" ], "text/plain": [ " id name x y\n", "0 1035 Norbert -0.757208 0.410627\n", "1 1088 Hannah 0.655573 -0.618467\n", "2 979 Dan 0.112189 0.363218\n", "3 983 Edith 0.547462 -0.595247\n", "4 1039 Wendy 0.710590 0.695703" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's look at some statistics on the data" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "execution": { "iopub.execute_input": "2022-05-16T13:57:59.253285Z", "iopub.status.busy": "2022-05-16T13:57:59.253094Z", "iopub.status.idle": "2022-05-16T13:58:01.254282Z", "shell.execute_reply": "2022-05-16T13:58:01.253648Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idxy
count2.592000e+062.592000e+062.592000e+06
mean9.999959e+02-2.440908e-042.330697e-06
std3.162768e+015.772636e-015.773129e-01
min8.550000e+02-9.999995e-01-9.999996e-01
25%9.790000e+02-4.962286e-01-4.949900e-01
50%1.000000e+035.109524e-036.483245e-03
75%1.021000e+035.067333e-015.048855e-01
max1.160000e+039.999983e-019.999985e-01
\n", "
" ], "text/plain": [ " id x y\n", "count 2.592000e+06 2.592000e+06 2.592000e+06\n", "mean 9.999959e+02 -2.440908e-04 2.330697e-06\n", "std 3.162768e+01 5.772636e-01 5.773129e-01\n", "min 8.550000e+02 -9.999995e-01 -9.999996e-01\n", "25% 9.790000e+02 -4.962286e-01 -4.949900e-01\n", "50% 1.000000e+03 5.109524e-03 6.483245e-03\n", "75% 1.021000e+03 5.067333e-01 5.048855e-01\n", "max 1.160000e+03 9.999983e-01 9.999985e-01" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.describe().compute()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Make some messy data\n", "\n", "Now this works great, and in most cases dd.read_csv or dd.read_parquet etc are the preferred way to read in large collections of data files into a dask dataframe, but real world data is often very messy and some files may be broken or badly formatted. To simulate this we are going to create some fake messy data by tweaking our example csv files. For the file `data/2000-01-05.csv` we will replace with no data and for the file `data/2000-01-07.csv` we will remove the `y` column " ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "execution": { "iopub.execute_input": "2022-05-16T13:58:01.257752Z", "iopub.status.busy": "2022-05-16T13:58:01.257172Z", "iopub.status.idle": "2022-05-16T13:58:01.261547Z", "shell.execute_reply": "2022-05-16T13:58:01.260997Z" } }, "outputs": [], "source": [ "# corrupt the data in data/2000-01-05.csv\n", "with open('data/2000-01-05.csv', 'w') as f:\n", " f.write('')" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "execution": { "iopub.execute_input": "2022-05-16T13:58:01.264071Z", "iopub.status.busy": "2022-05-16T13:58:01.263892Z", "iopub.status.idle": "2022-05-16T13:58:01.569601Z", "shell.execute_reply": "2022-05-16T13:58:01.568994Z" } }, "outputs": [], "source": [ "# remove y column from data/2000-01-07.csv\n", "import pandas as pd\n", "df = pd.read_csv('data/2000-01-07.csv')\n", "del df['y']\n", "df.to_csv('data/2000-01-07.csv', index=False)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "execution": { "iopub.execute_input": "2022-05-16T13:58:01.573418Z", "iopub.status.busy": "2022-05-16T13:58:01.573211Z", "iopub.status.idle": "2022-05-16T13:58:01.734107Z", "shell.execute_reply": "2022-05-16T13:58:01.732940Z" } }, "outputs": [], "source": [ "!head data/2000-01-05.csv" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "execution": { "iopub.execute_input": "2022-05-16T13:58:01.738253Z", "iopub.status.busy": "2022-05-16T13:58:01.738040Z", "iopub.status.idle": "2022-05-16T13:58:01.901026Z", "shell.execute_reply": "2022-05-16T13:58:01.900020Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "id,name,x\r\n", "1024,Sarah,0.1864121065059618\r\n", "1086,Michael,-0.1573116298587789\r\n", "1031,Kevin,0.7762392026848937\r\n", "1058,Charlie,-0.1641263675170783\r\n", "929,Jerry,0.6898603625500546\r\n", "1002,Yvonne,0.3046855447254017\r\n", "1043,Dan,-0.654128931819649\r\n", "1024,Dan,-0.4308229218905972\r\n", "1026,Patricia,-0.0112543302085683\r\n" ] } ], "source": [ "!head data/2000-01-07.csv" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Reading the messy data\n", "\n", "Let's try reading in the collection of files again" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "execution": { "iopub.execute_input": "2022-05-16T13:58:01.905952Z", "iopub.status.busy": "2022-05-16T13:58:01.905536Z", "iopub.status.idle": "2022-05-16T13:58:01.925587Z", "shell.execute_reply": "2022-05-16T13:58:01.925038Z" } }, "outputs": [], "source": [ "df = dd.read_csv('data/2000-*-*.csv')" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "execution": { "iopub.execute_input": "2022-05-16T13:58:01.928458Z", "iopub.status.busy": "2022-05-16T13:58:01.928262Z", "iopub.status.idle": "2022-05-16T13:58:02.021749Z", "shell.execute_reply": "2022-05-16T13:58:02.021142Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idnamexy
01035Norbert-0.7572080.410627
11088Hannah0.655573-0.618467
2979Dan0.1121890.363218
3983Edith0.547462-0.595247
41039Wendy0.7105900.695703
\n", "
" ], "text/plain": [ " id name x y\n", "0 1035 Norbert -0.757208 0.410627\n", "1 1088 Hannah 0.655573 -0.618467\n", "2 979 Dan 0.112189 0.363218\n", "3 983 Edith 0.547462 -0.595247\n", "4 1039 Wendy 0.710590 0.695703" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Ok this looks like it worked, let us calculate the dataset statistics again" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "execution": { "iopub.execute_input": "2022-05-16T13:58:02.025405Z", "iopub.status.busy": "2022-05-16T13:58:02.025116Z", "iopub.status.idle": "2022-05-16T13:58:06.265308Z", "shell.execute_reply": "2022-05-16T13:58:06.264389Z" }, "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "ValueError", "evalue": "The columns in the computed data do not match the columns in the provided metadata\n Extra: []\n Missing: ['y']", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "Input \u001b[0;32mIn [14]\u001b[0m, in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mdf\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdescribe\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcompute\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/usr/share/miniconda3/envs/dask-examples/lib/python3.9/site-packages/dask/base.py:292\u001b[0m, in \u001b[0;36mDaskMethodsMixin.compute\u001b[0;34m(self, **kwargs)\u001b[0m\n\u001b[1;32m 268\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcompute\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs):\n\u001b[1;32m 269\u001b[0m \u001b[38;5;124;03m\"\"\"Compute this dask collection\u001b[39;00m\n\u001b[1;32m 270\u001b[0m \n\u001b[1;32m 271\u001b[0m \u001b[38;5;124;03m This turns a lazy Dask collection into its in-memory equivalent.\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 290\u001b[0m \u001b[38;5;124;03m dask.base.compute\u001b[39;00m\n\u001b[1;32m 291\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m--> 292\u001b[0m (result,) \u001b[38;5;241m=\u001b[39m \u001b[43mcompute\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mtraverse\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 293\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m result\n", "File \u001b[0;32m/usr/share/miniconda3/envs/dask-examples/lib/python3.9/site-packages/dask/base.py:575\u001b[0m, in \u001b[0;36mcompute\u001b[0;34m(traverse, optimize_graph, scheduler, get, *args, **kwargs)\u001b[0m\n\u001b[1;32m 572\u001b[0m keys\u001b[38;5;241m.\u001b[39mappend(x\u001b[38;5;241m.\u001b[39m__dask_keys__())\n\u001b[1;32m 573\u001b[0m postcomputes\u001b[38;5;241m.\u001b[39mappend(x\u001b[38;5;241m.\u001b[39m__dask_postcompute__())\n\u001b[0;32m--> 575\u001b[0m results \u001b[38;5;241m=\u001b[39m \u001b[43mschedule\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdsk\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkeys\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 576\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m repack([f(r, \u001b[38;5;241m*\u001b[39ma) \u001b[38;5;28;01mfor\u001b[39;00m r, (f, a) \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mzip\u001b[39m(results, postcomputes)])\n", "File \u001b[0;32m/usr/share/miniconda3/envs/dask-examples/lib/python3.9/site-packages/distributed/client.py:3018\u001b[0m, in \u001b[0;36mClient.get\u001b[0;34m(self, dsk, keys, workers, allow_other_workers, resources, sync, asynchronous, direct, retries, priority, fifo_timeout, actors, **kwargs)\u001b[0m\n\u001b[1;32m 3016\u001b[0m should_rejoin \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m\n\u001b[1;32m 3017\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m-> 3018\u001b[0m results \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mgather\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpacked\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43masynchronous\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43masynchronous\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdirect\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdirect\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 3019\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[1;32m 3020\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m f \u001b[38;5;129;01min\u001b[39;00m futures\u001b[38;5;241m.\u001b[39mvalues():\n", "File \u001b[0;32m/usr/share/miniconda3/envs/dask-examples/lib/python3.9/site-packages/distributed/client.py:2171\u001b[0m, in \u001b[0;36mClient.gather\u001b[0;34m(self, futures, errors, direct, asynchronous)\u001b[0m\n\u001b[1;32m 2169\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 2170\u001b[0m local_worker \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m-> 2171\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msync\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 2172\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_gather\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 2173\u001b[0m \u001b[43m \u001b[49m\u001b[43mfutures\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 2174\u001b[0m \u001b[43m \u001b[49m\u001b[43merrors\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43merrors\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 2175\u001b[0m \u001b[43m \u001b[49m\u001b[43mdirect\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdirect\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 2176\u001b[0m \u001b[43m \u001b[49m\u001b[43mlocal_worker\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mlocal_worker\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 2177\u001b[0m \u001b[43m \u001b[49m\u001b[43masynchronous\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43masynchronous\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 2178\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/usr/share/miniconda3/envs/dask-examples/lib/python3.9/site-packages/distributed/utils.py:309\u001b[0m, in \u001b[0;36mSyncMethodMixin.sync\u001b[0;34m(self, func, asynchronous, callback_timeout, *args, **kwargs)\u001b[0m\n\u001b[1;32m 307\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m future\n\u001b[1;32m 308\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 309\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43msync\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 310\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mloop\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mfunc\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcallback_timeout\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcallback_timeout\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\n\u001b[1;32m 311\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/usr/share/miniconda3/envs/dask-examples/lib/python3.9/site-packages/distributed/utils.py:376\u001b[0m, in \u001b[0;36msync\u001b[0;34m(loop, func, callback_timeout, *args, **kwargs)\u001b[0m\n\u001b[1;32m 374\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m error:\n\u001b[1;32m 375\u001b[0m typ, exc, tb \u001b[38;5;241m=\u001b[39m error\n\u001b[0;32m--> 376\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m exc\u001b[38;5;241m.\u001b[39mwith_traceback(tb)\n\u001b[1;32m 377\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 378\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m result\n", "File \u001b[0;32m/usr/share/miniconda3/envs/dask-examples/lib/python3.9/site-packages/distributed/utils.py:349\u001b[0m, in \u001b[0;36msync..f\u001b[0;34m()\u001b[0m\n\u001b[1;32m 347\u001b[0m future \u001b[38;5;241m=\u001b[39m asyncio\u001b[38;5;241m.\u001b[39mwait_for(future, callback_timeout)\n\u001b[1;32m 348\u001b[0m future \u001b[38;5;241m=\u001b[39m asyncio\u001b[38;5;241m.\u001b[39mensure_future(future)\n\u001b[0;32m--> 349\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01myield\u001b[39;00m future\n\u001b[1;32m 350\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m:\n\u001b[1;32m 351\u001b[0m error \u001b[38;5;241m=\u001b[39m sys\u001b[38;5;241m.\u001b[39mexc_info()\n", "File \u001b[0;32m/usr/share/miniconda3/envs/dask-examples/lib/python3.9/site-packages/tornado/gen.py:762\u001b[0m, in \u001b[0;36mRunner.run\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 759\u001b[0m exc_info \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[1;32m 761\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 762\u001b[0m value \u001b[38;5;241m=\u001b[39m \u001b[43mfuture\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mresult\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 763\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m:\n\u001b[1;32m 764\u001b[0m exc_info \u001b[38;5;241m=\u001b[39m sys\u001b[38;5;241m.\u001b[39mexc_info()\n", "File \u001b[0;32m/usr/share/miniconda3/envs/dask-examples/lib/python3.9/site-packages/distributed/client.py:2034\u001b[0m, in \u001b[0;36mClient._gather\u001b[0;34m(self, futures, errors, direct, local_worker)\u001b[0m\n\u001b[1;32m 2032\u001b[0m exc \u001b[38;5;241m=\u001b[39m CancelledError(key)\n\u001b[1;32m 2033\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m-> 2034\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m exception\u001b[38;5;241m.\u001b[39mwith_traceback(traceback)\n\u001b[1;32m 2035\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m exc\n\u001b[1;32m 2036\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m errors \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mskip\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n", "File \u001b[0;32m/usr/share/miniconda3/envs/dask-examples/lib/python3.9/site-packages/dask/optimization.py:990\u001b[0m, in \u001b[0;36m__call__\u001b[0;34m()\u001b[0m\n\u001b[1;32m 988\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(args) \u001b[38;5;241m==\u001b[39m \u001b[38;5;28mlen\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39minkeys):\n\u001b[1;32m 989\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mExpected \u001b[39m\u001b[38;5;132;01m%d\u001b[39;00m\u001b[38;5;124m args, got \u001b[39m\u001b[38;5;132;01m%d\u001b[39;00m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;241m%\u001b[39m (\u001b[38;5;28mlen\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39minkeys), \u001b[38;5;28mlen\u001b[39m(args)))\n\u001b[0;32m--> 990\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m core\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdsk, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39moutkey, \u001b[38;5;28mdict\u001b[39m(\u001b[38;5;28mzip\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39minkeys, args)))\n", "File \u001b[0;32m/usr/share/miniconda3/envs/dask-examples/lib/python3.9/site-packages/dask/core.py:149\u001b[0m, in \u001b[0;36mget\u001b[0;34m()\u001b[0m\n\u001b[1;32m 147\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m key \u001b[38;5;129;01min\u001b[39;00m toposort(dsk):\n\u001b[1;32m 148\u001b[0m task \u001b[38;5;241m=\u001b[39m dsk[key]\n\u001b[0;32m--> 149\u001b[0m result \u001b[38;5;241m=\u001b[39m _execute_task(task, cache)\n\u001b[1;32m 150\u001b[0m cache[key] \u001b[38;5;241m=\u001b[39m result\n\u001b[1;32m 151\u001b[0m result \u001b[38;5;241m=\u001b[39m _execute_task(out, cache)\n", "File \u001b[0;32m/usr/share/miniconda3/envs/dask-examples/lib/python3.9/site-packages/dask/core.py:119\u001b[0m, in \u001b[0;36m_execute_task\u001b[0;34m()\u001b[0m\n\u001b[1;32m 115\u001b[0m func, args \u001b[38;5;241m=\u001b[39m arg[\u001b[38;5;241m0\u001b[39m], arg[\u001b[38;5;241m1\u001b[39m:]\n\u001b[1;32m 116\u001b[0m \u001b[38;5;66;03m# Note: Don't assign the subtask results to a variable. numpy detects\u001b[39;00m\n\u001b[1;32m 117\u001b[0m \u001b[38;5;66;03m# temporaries by their reference count and can execute certain\u001b[39;00m\n\u001b[1;32m 118\u001b[0m \u001b[38;5;66;03m# operations in-place.\u001b[39;00m\n\u001b[0;32m--> 119\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m func(\u001b[38;5;241m*\u001b[39m(_execute_task(a, cache) \u001b[38;5;28;01mfor\u001b[39;00m a \u001b[38;5;129;01min\u001b[39;00m args))\n\u001b[1;32m 120\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m ishashable(arg):\n\u001b[1;32m 121\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m arg\n", "File \u001b[0;32m/usr/share/miniconda3/envs/dask-examples/lib/python3.9/site-packages/dask/utils.py:39\u001b[0m, in \u001b[0;36mapply\u001b[0;34m()\u001b[0m\n\u001b[1;32m 37\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mapply\u001b[39m(func, args, kwargs\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m):\n\u001b[1;32m 38\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m kwargs:\n\u001b[0;32m---> 39\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m func(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[1;32m 40\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 41\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m func(\u001b[38;5;241m*\u001b[39margs)\n", "File \u001b[0;32m/usr/share/miniconda3/envs/dask-examples/lib/python3.9/site-packages/dask/dataframe/core.py:6355\u001b[0m, in \u001b[0;36mapply_and_enforce\u001b[0;34m()\u001b[0m\n\u001b[1;32m 6353\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m meta\n\u001b[1;32m 6354\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m is_dataframe_like(df):\n\u001b[0;32m-> 6355\u001b[0m check_matching_columns(meta, df)\n\u001b[1;32m 6356\u001b[0m c \u001b[38;5;241m=\u001b[39m meta\u001b[38;5;241m.\u001b[39mcolumns\n\u001b[1;32m 6357\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n", "File \u001b[0;32m/usr/share/miniconda3/envs/dask-examples/lib/python3.9/site-packages/dask/dataframe/utils.py:415\u001b[0m, in \u001b[0;36mcheck_matching_columns\u001b[0;34m()\u001b[0m\n\u001b[1;32m 413\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 414\u001b[0m extra_info \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mOrder of columns does not match\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m--> 415\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m 416\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThe columns in the computed data do not match\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 417\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m the columns in the provided metadata\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 418\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mextra_info\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 419\u001b[0m )\n", "\u001b[0;31mValueError\u001b[0m: The columns in the computed data do not match the columns in the provided metadata\n Extra: []\n Missing: ['y']" ] } ], "source": [ "df.describe().compute()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So what happened? \n", "\n", "When creating a dask dataframe from a collection of files, dd.read_csv samples the first few files in the dataset to determine the datatypes and columns available. Since it has not opened all the files it does not now if some of them are corrupt. Hence, `df.head()` works since it is only looking at the first file. `df.describe.compute()` fails because of the corrupt data in `data/2000-01-05.csv`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Building a delayed reader\n", "\n", "To get around this problem we are going to use a more advanced technique to build our dask dataframe. This method can also be used any time some custom logic is required when reading each file. Essentially, we are going to build a function that uses pandas and some error checking and returns a pandas dataframe. If we find a bad data file we will either find a way to fix/clean the data or we will return and empty pandas dataframe with the same structure as the good data." ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "execution": { "iopub.execute_input": "2022-05-16T13:58:06.269063Z", "iopub.status.busy": "2022-05-16T13:58:06.268710Z", "iopub.status.idle": "2022-05-16T13:58:06.277575Z", "shell.execute_reply": "2022-05-16T13:58:06.276551Z" } }, "outputs": [], "source": [ "import numpy as np\n", "import io\n", "\n", "def read_data(filename):\n", " \n", " # for this to work we need to explicitly set the datatypes of our pandas dataframe \n", " dtypes = {'id': int, 'name': str, 'x': float, 'y': float}\n", " try:\n", " # try reading in the data with pandas \n", " df = pd.read_csv(filename, dtype=dtypes)\n", " except:\n", " # if this fails create an empty pandas dataframe with the same dtypes as the good data\n", " df = pd.read_csv(io.StringIO(''), names=dtypes.keys(), dtype=dtypes)\n", " \n", " # for the case with the missing column, add a column of data with NaN's\n", " if 'y' not in df.columns:\n", " df['y'] = np.NaN\n", " \n", " return df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's test this function on a good file and the two bad files" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "execution": { "iopub.execute_input": "2022-05-16T13:58:06.280341Z", "iopub.status.busy": "2022-05-16T13:58:06.280148Z", "iopub.status.idle": "2022-05-16T13:58:06.349376Z", "shell.execute_reply": "2022-05-16T13:58:06.348788Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idnamexy
01035Norbert-0.7572080.410627
11088Hannah0.655573-0.618467
2979Dan0.1121890.363218
3983Edith0.547462-0.595247
41039Wendy0.7105900.695703
\n", "
" ], "text/plain": [ " id name x y\n", "0 1035 Norbert -0.757208 0.410627\n", "1 1088 Hannah 0.655573 -0.618467\n", "2 979 Dan 0.112189 0.363218\n", "3 983 Edith 0.547462 -0.595247\n", "4 1039 Wendy 0.710590 0.695703" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# test function on a normal file\n", "read_data('data/2000-01-01.csv').head()" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "execution": { "iopub.execute_input": "2022-05-16T13:58:06.352286Z", "iopub.status.busy": "2022-05-16T13:58:06.351946Z", "iopub.status.idle": "2022-05-16T13:58:06.363723Z", "shell.execute_reply": "2022-05-16T13:58:06.363189Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idnamexy
\n", "
" ], "text/plain": [ "Empty DataFrame\n", "Columns: [id, name, x, y]\n", "Index: []" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# test function on the empty file\n", "read_data('data/2000-01-05.csv').head()" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "execution": { "iopub.execute_input": "2022-05-16T13:58:06.366195Z", "iopub.status.busy": "2022-05-16T13:58:06.366015Z", "iopub.status.idle": "2022-05-16T13:58:06.416064Z", "shell.execute_reply": "2022-05-16T13:58:06.415502Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idnamexy
01024Sarah0.186412NaN
11086Michael-0.157312NaN
21031Kevin0.776239NaN
31058Charlie-0.164126NaN
4929Jerry0.689860NaN
\n", "
" ], "text/plain": [ " id name x y\n", "0 1024 Sarah 0.186412 NaN\n", "1 1086 Michael -0.157312 NaN\n", "2 1031 Kevin 0.776239 NaN\n", "3 1058 Charlie -0.164126 NaN\n", "4 929 Jerry 0.689860 NaN" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# test function on the file missing the y column\n", "read_data('data/2000-01-07.csv').head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Assembling the dask dataframe\n", "\n", "First we take our `read_data` function and convert it to a dask delayed function" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "execution": { "iopub.execute_input": "2022-05-16T13:58:06.419035Z", "iopub.status.busy": "2022-05-16T13:58:06.418618Z", "iopub.status.idle": "2022-05-16T13:58:06.423186Z", "shell.execute_reply": "2022-05-16T13:58:06.422637Z" } }, "outputs": [], "source": [ "from dask import delayed\n", "read_data = delayed(read_data)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let us look at what the function does now" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "execution": { "iopub.execute_input": "2022-05-16T13:58:06.425927Z", "iopub.status.busy": "2022-05-16T13:58:06.425525Z", "iopub.status.idle": "2022-05-16T13:58:06.430080Z", "shell.execute_reply": "2022-05-16T13:58:06.429499Z" } }, "outputs": [ { "data": { "text/plain": [ "Delayed('read_data-5f0eccbc-3bde-4275-8ba0-2649154b0bb7')" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = read_data('data/2000-01-01.csv')\n", "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It creates a delayed object, to actually run read the file we need to run `.compute()`" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "execution": { "iopub.execute_input": "2022-05-16T13:58:06.433486Z", "iopub.status.busy": "2022-05-16T13:58:06.433016Z", "iopub.status.idle": "2022-05-16T13:58:06.531676Z", "shell.execute_reply": "2022-05-16T13:58:06.531052Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idnamexy
01035Norbert-0.7572080.410627
11088Hannah0.655573-0.618467
2979Dan0.1121890.363218
3983Edith0.547462-0.595247
41039Wendy0.7105900.695703
...............
863951021Kevin0.125742-0.711153
86396965Zelda-0.6241590.855033
863971042Frank0.7380920.464486
863981067Oliver-0.3253760.768031
863991003Charlie-0.755295-0.279816
\n", "

86400 rows × 4 columns

\n", "
" ], "text/plain": [ " id name x y\n", "0 1035 Norbert -0.757208 0.410627\n", "1 1088 Hannah 0.655573 -0.618467\n", "2 979 Dan 0.112189 0.363218\n", "3 983 Edith 0.547462 -0.595247\n", "4 1039 Wendy 0.710590 0.695703\n", "... ... ... ... ...\n", "86395 1021 Kevin 0.125742 -0.711153\n", "86396 965 Zelda -0.624159 0.855033\n", "86397 1042 Frank 0.738092 0.464486\n", "86398 1067 Oliver -0.325376 0.768031\n", "86399 1003 Charlie -0.755295 -0.279816\n", "\n", "[86400 rows x 4 columns]" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.compute()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's build a list of all the available csv files" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "execution": { "iopub.execute_input": "2022-05-16T13:58:06.534975Z", "iopub.status.busy": "2022-05-16T13:58:06.534575Z", "iopub.status.idle": "2022-05-16T13:58:06.541474Z", "shell.execute_reply": "2022-05-16T13:58:06.540900Z" } }, "outputs": [ { "data": { "text/plain": [ "['data/2000-01-26.csv',\n", " 'data/2000-01-09.csv',\n", " 'data/2000-01-01.csv',\n", " 'data/2000-01-11.csv',\n", " 'data/2000-01-02.csv',\n", " 'data/2000-01-22.csv',\n", " 'data/2000-01-08.csv',\n", " 'data/2000-01-07.csv',\n", " 'data/2000-01-03.csv',\n", " 'data/2000-01-30.csv',\n", " 'data/2000-01-29.csv',\n", " 'data/2000-01-12.csv',\n", " 'data/2000-01-19.csv',\n", " 'data/2000-01-20.csv',\n", " 'data/2000-01-23.csv',\n", " 'data/2000-01-04.csv',\n", " 'data/2000-01-13.csv',\n", " 'data/2000-01-06.csv',\n", " 'data/2000-01-21.csv',\n", " 'data/2000-01-10.csv',\n", " 'data/2000-01-17.csv',\n", " 'data/2000-01-14.csv',\n", " 'data/2000-01-05.csv',\n", " 'data/2000-01-16.csv',\n", " 'data/2000-01-28.csv',\n", " 'data/2000-01-25.csv',\n", " 'data/2000-01-27.csv',\n", " 'data/2000-01-18.csv',\n", " 'data/2000-01-15.csv',\n", " 'data/2000-01-24.csv']" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# loop over all the files\n", "from glob import glob\n", "files = glob('data/2000-*-*.csv')\n", "files" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we run the delayed read_data function on each file in the list" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "execution": { "iopub.execute_input": "2022-05-16T13:58:06.544432Z", "iopub.status.busy": "2022-05-16T13:58:06.544249Z", "iopub.status.idle": "2022-05-16T13:58:06.552477Z", "shell.execute_reply": "2022-05-16T13:58:06.551898Z" } }, "outputs": [ { "data": { "text/plain": [ "[Delayed('read_data-0f117b8f-3bf5-4a88-bb2e-7cf633df2d1f'),\n", " Delayed('read_data-046ac206-4de3-4777-8167-d0bc0b4979d0'),\n", " Delayed('read_data-2aa9072d-e97f-442f-8b9c-cdc2266e8ae3'),\n", " Delayed('read_data-ccf646ff-5709-4edc-aa4c-725c70181082'),\n", " Delayed('read_data-411c2f2c-e985-4b50-b52b-a41fcb8419f0'),\n", " Delayed('read_data-dbd0a7a9-fc3b-4848-8467-d7da73a4a585'),\n", " Delayed('read_data-f802b6f6-c99e-41d9-b011-7f7f76141341'),\n", " Delayed('read_data-fd3751d6-9add-4b27-a741-785b26eb90a9'),\n", " Delayed('read_data-0b3ed986-6857-49c2-abeb-223af0f8c1b1'),\n", " Delayed('read_data-3edf4bea-e554-47bd-9345-720c73bd1779'),\n", " Delayed('read_data-b2356f93-2073-40d6-90ae-3adc4643103f'),\n", " Delayed('read_data-25587a27-877a-4095-99ed-49c51cda9e8c'),\n", " Delayed('read_data-a493b21a-05d0-4fb1-bd10-fe9af7701445'),\n", " Delayed('read_data-03f39d68-650e-4bb2-8bcc-ab1d80351429'),\n", " Delayed('read_data-243e92f4-aabb-4598-ae28-dc85c4742202'),\n", " Delayed('read_data-572862e0-e437-4c83-bd01-9feaaabd3c50'),\n", " Delayed('read_data-f59e5f89-aa36-4da6-9c83-63749d7aa21c'),\n", " Delayed('read_data-29fb0fcc-80f4-4f06-8d34-fb8797494d99'),\n", " Delayed('read_data-2f38c117-6072-4b19-a46a-fbfc44c39e73'),\n", " Delayed('read_data-cbab1938-7f71-41fe-adb3-9674cf0edc2a'),\n", " Delayed('read_data-0abf24ff-3f78-44fd-ae07-214523714beb'),\n", " Delayed('read_data-923234e2-d058-4f37-9e5c-d61e93168585'),\n", " Delayed('read_data-e0bbcca9-9268-43b9-af80-64cf333119e4'),\n", " Delayed('read_data-c455609b-c62e-4953-a158-28f9e00ef5e2'),\n", " Delayed('read_data-35304cc6-6f22-4ab4-883b-c21ddb2b6f12'),\n", " Delayed('read_data-29e56dee-a38f-41e9-ad86-f4357b8015c5'),\n", " Delayed('read_data-c2341016-7ed3-41d5-bcbd-1ff397c9bf2d'),\n", " Delayed('read_data-54edd786-0f28-42f5-83f1-e09419608a35'),\n", " Delayed('read_data-67004d43-1987-48b9-b121-815dc4953d43'),\n", " Delayed('read_data-2e368572-51e3-488a-8b5d-54b852123414')]" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = [read_data(file) for file in files]\n", "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then we use [dask.dataframe.from_delayed](https://docs.dask.org/en/latest/generated/dask.dataframe.from_delayed.html). This function creates a Dask DataFrame from a list of delayed objects as long as each delayed object returns a pandas dataframe. The structure of each individual dataframe returned must also be the same." ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "execution": { "iopub.execute_input": "2022-05-16T13:58:06.555612Z", "iopub.status.busy": "2022-05-16T13:58:06.555167Z", "iopub.status.idle": "2022-05-16T13:58:06.572082Z", "shell.execute_reply": "2022-05-16T13:58:06.571557Z" } }, "outputs": [ { "data": { "text/html": [ "
Dask DataFrame Structure:
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idnamexy
npartitions=30
int64objectfloat64float64
............
...............
............
............
\n", "
\n", "
Dask Name: from-delayed, 60 tasks
" ], "text/plain": [ "Dask DataFrame Structure:\n", " id name x y\n", "npartitions=30 \n", " int64 object float64 float64\n", " ... ... ... ...\n", "... ... ... ... ...\n", " ... ... ... ...\n", " ... ... ... ...\n", "Dask Name: from-delayed, 60 tasks" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = dd.from_delayed(df, meta={'id': int, 'name': str, 'x': float, 'y': float})\n", "df" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note: we provided the dtypes in the `meta` keyword to explicitly tell Dask Dataframe what kind of dataframe to expect. If we did not do this Dask would infer this from the first delayed object which could be slow if it was a large csv file" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Now let's see if this works" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "execution": { "iopub.execute_input": "2022-05-16T13:58:06.574502Z", "iopub.status.busy": "2022-05-16T13:58:06.574318Z", "iopub.status.idle": "2022-05-16T13:58:06.662374Z", "shell.execute_reply": "2022-05-16T13:58:06.661769Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idnamexy
0968Wendy0.427086-0.267555
11011Zelda-0.944234-0.925922
21009Ray-0.4994150.092161
3998Dan0.2432370.754739
4982Xavier0.3009570.548789
\n", "
" ], "text/plain": [ " id name x y\n", "0 968 Wendy 0.427086 -0.267555\n", "1 1011 Zelda -0.944234 -0.925922\n", "2 1009 Ray -0.499415 0.092161\n", "3 998 Dan 0.243237 0.754739\n", "4 982 Xavier 0.300957 0.548789" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.head()" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "execution": { "iopub.execute_input": "2022-05-16T13:58:06.665580Z", "iopub.status.busy": "2022-05-16T13:58:06.665186Z", "iopub.status.idle": "2022-05-16T13:58:08.688203Z", "shell.execute_reply": "2022-05-16T13:58:08.687643Z" } }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
idxy
count2.505600e+062.505600e+062.419200e+06
mean1.000001e+03-2.724378e-04-3.678182e-06
std3.162445e+015.772779e-015.772706e-01
min8.550000e+02-9.999995e-01-9.999996e-01
25%9.790000e+02-4.962286e-01-4.949900e-01
50%1.000000e+035.109524e-036.483245e-03
75%1.021000e+035.067333e-015.048855e-01
max1.160000e+039.999983e-019.999985e-01
\n", "
" ], "text/plain": [ " id x y\n", "count 2.505600e+06 2.505600e+06 2.419200e+06\n", "mean 1.000001e+03 -2.724378e-04 -3.678182e-06\n", "std 3.162445e+01 5.772779e-01 5.772706e-01\n", "min 8.550000e+02 -9.999995e-01 -9.999996e-01\n", "25% 9.790000e+02 -4.962286e-01 -4.949900e-01\n", "50% 1.000000e+03 5.109524e-03 6.483245e-03\n", "75% 1.021000e+03 5.067333e-01 5.048855e-01\n", "max 1.160000e+03 9.999983e-01 9.999985e-01" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.describe().compute()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Success!\n", "\n", "To recap, in this example, we looked at an approach to create a Dask Dataframe from a collection of many data files. Typically you would use built-in functions like `dd.read_csv` or `dd.read_parquet` to do this. Sometimes, this is not possible because of messy/corrupted files in your dataset or some custom processing that might need to be done. \n", "\n", "In these cases, you can build a Dask DataFrame with the following steps.\n", "\n", "1. Create a regular python function that reads the data, performs any transformations, error checking etc and always returns a Pandas dataframe with the same structure\n", "2. Convert this read function to a delayed object using the `dask.delayed` function\n", "3. Call each file in your dataset with the delayed data reader and assemble the output as a list of delayed objects\n", "4. Used `dd.from_delayed` to covert the list of delayed objects to a Dask Dataframe \n", "\n", "This same technique can be used in other situations as well. Another example might be data files that require using a specialized reader, or several transformations before they can be converted to a pandas dataframe." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.12" } }, "nbformat": 4, "nbformat_minor": 4 }