Skip to content

Instantly share code, notes, and snippets.

@drorata
Created July 10, 2019 16:25
Show Gist options
  • Save drorata/4d2c26602fa9dd8f0f0f37a68ec4d24c to your computer and use it in GitHub Desktop.
Save drorata/4d2c26602fa9dd8f0f0f37a68ec4d24c to your computer and use it in GitHub Desktop.
Casting values carefully using dask
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import dask.dataframe as dd"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Assume you have a dataframe with strings and missing values represented as `None`s."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>a</th>\n",
" <th>b</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>None</td>\n",
" <td>3.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>6</td>\n",
" <td>3.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>6</td>\n",
" <td>3.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>6.5</td>\n",
" <td>3.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>6.5</td>\n",
" <td>3</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" a b\n",
"0 None 3.5\n",
"1 6 3.5\n",
"2 6 3.5\n",
"3 6.5 3.5\n",
"4 6.5 3"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df = pd.DataFrame(\n",
" {\n",
" \"a\": np.random.choice([\"6\", \"6.5\", None], size=50),\n",
" \"b\": np.random.choice([\"3\", \"3.5\", None], size=50)\n",
" }\n",
")\n",
"df.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Before starting, build a `dask` dataframe:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"ddf = dd.from_pandas(df, npartitions=3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Naive approach for casting would be:\n",
"\n",
"```python\n",
"ddf_mapped = ddf.map_partitions(\n",
" lambda df: df.apply(\n",
" lambda col: col.apply(\n",
" lambda x: float(x)\n",
" )\n",
" ), meta=pd.DataFrame({'a': [1., None], 'b': [2., None]})\n",
")\n",
"```\n",
"\n",
"But it will fail once you try to do something like `ddf_mapped.head()` and actual computation kicks in.\n",
"\n",
"So, next, try to define the following function:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def map_str_or_none(x):\n",
" if x is None:\n",
" return None\n",
" else:\n",
" try:\n",
" return float(x)\n",
" except ValueError:\n",
" print(x)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"ddf_mapped = ddf.map_partitions(\n",
" lambda df: df.apply(\n",
" lambda col: col.apply(\n",
" lambda x: map_str_or_none(x)\n",
" )\n",
" ), meta=pd.DataFrame({'a': [1., None], 'b': [2., None]})\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"a float64\n",
"b float64\n",
"dtype: object"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ddf_mapped.dtypes"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>a</th>\n",
" <th>b</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>NaN</td>\n",
" <td>3.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>6.0</td>\n",
" <td>3.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>6.0</td>\n",
" <td>3.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>6.5</td>\n",
" <td>3.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>6.5</td>\n",
" <td>3.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" a b\n",
"0 NaN 3.5\n",
"1 6.0 3.5\n",
"2 6.0 3.5\n",
"3 6.5 3.5\n",
"4 6.5 3.0"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ddf_mapped.head()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"50"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ddf_mapped.shape[0].compute()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This looks like its doing the trick.\n",
"\n",
"What if you have some values that cannot be casted to `float`s?"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>a</th>\n",
" <th>b</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>6</td>\n",
" <td>3.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>None</td>\n",
" <td>str_cannot_be_casted_to_float</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>6.5</td>\n",
" <td>3.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>6.5</td>\n",
" <td>None</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>6.5</td>\n",
" <td>None</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" a b\n",
"0 6 3.5\n",
"1 None str_cannot_be_casted_to_float\n",
"2 6.5 3.5\n",
"3 6.5 None\n",
"4 6.5 None"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df2 = pd.DataFrame(\n",
" {\n",
" \"a\": np.random.choice([\"6\", \"6.5\", None, \"str_cannot_be_casted_to_float\"], size=50),\n",
" \"b\": np.random.choice([\"3\", \"3.5\", None, \"str_cannot_be_casted_to_float\"], size=50)\n",
" }\n",
")\n",
"df2.head()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"ddf2 = dd.from_pandas(df2, npartitions=3)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"ddf2_mapped = ddf2.map_partitions(\n",
" lambda df: df.apply(\n",
" lambda col: col.apply(\n",
" lambda x: map_str_or_none(x)\n",
" )\n",
" ), meta=pd.DataFrame({'a': [1., None], 'b': [2., None]})\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"a float64\n",
"b float64\n",
"dtype: object"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ddf2_mapped.dtypes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So far... so good. But remember, no casting happened yet!"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>a</th>\n",
" <th>b</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>6.0</td>\n",
" <td>3.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>6.5</td>\n",
" <td>3.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>6.5</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>6.5</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" a b\n",
"0 6.0 3.5\n",
"1 NaN NaN\n",
"2 6.5 3.5\n",
"3 6.5 NaN\n",
"4 6.5 NaN"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ddf2_mapped.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Ahhhh... So here we see that we handled the `ValueError`."
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_floatstr_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n"
]
},
{
"data": {
"text/plain": [
"50"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ddf2_mapped.shape[0].compute()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So the shape still has 50 rows... What happened to the strings that couldn't be casted?"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>a</th>\n",
" <th>b</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>6</td>\n",
" <td>3.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>None</td>\n",
" <td>str_cannot_be_casted_to_float</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>6.5</td>\n",
" <td>3.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>6.5</td>\n",
" <td>None</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>6.5</td>\n",
" <td>None</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" a b\n",
"0 6 3.5\n",
"1 None str_cannot_be_casted_to_float\n",
"2 6.5 3.5\n",
"3 6.5 None\n",
"4 6.5 None"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df2.head()"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n",
"str_cannot_be_casted_to_float\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>a</th>\n",
" <th>b</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>6.0</td>\n",
" <td>3.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>NaN</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>6.5</td>\n",
" <td>3.5</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>6.5</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>6.5</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" a b\n",
"0 6.0 3.5\n",
"1 NaN NaN\n",
"2 6.5 3.5\n",
"3 6.5 NaN\n",
"4 6.5 NaN"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ddf2_mapped.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can see that they were mapped to `NaN`. This is neither bad nor good --- you just have to be aware of what you're doing.\n",
"\n",
"Lastly, you might want to check what happens if you remove `meta=pd.DataFrame({'a': [1., None], 'b': [2., None]})` part in the `map_partitions` above ;) foo will show up."
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"foo\n",
"foo\n",
"foo\n",
"foo\n",
"foo\n",
"foo\n"
]
}
],
"source": [
"ddf2_mapped = ddf2.map_partitions(\n",
" lambda df: df.apply(\n",
" lambda col: col.apply(\n",
" lambda x: map_str_or_none(x)\n",
" )\n",
" ) #, meta=pd.DataFrame({'a': [1., None], 'b': [2., None]})\n",
")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:rat-data]",
"language": "python",
"name": "conda-env-rat-data-py"
},
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment