Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save aavanian/f4340a2656d69a3a767cf613712ae072 to your computer and use it in GitHub Desktop.
Save aavanian/f4340a2656d69a3a767cf613712ae072 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import dask\n",
"import dask.dataframe as dd\n",
"from dask.diagnostics import ProgressBar, Profiler, ResourceProfiler, CacheProfiler\n",
"import numpy as np\n",
"import pandas as pd\n",
"from pandas.tseries.offsets import DateOffset"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Versions\n",
"print('pandas:', pd.__version__)\n",
"print('numpy:', np.__version__)\n",
"print('dask:', dask.__version__)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df = pd.DataFrame(index=pd.DatetimeIndex(end=pd.Timestamp('2017-05-18'), periods=25000, freq='1B', name='date'), columns=['value'], data=np.random.rand(25000)).reset_index()\n",
"ddf = dd.from_pandas(df, npartitions=10)\n",
"def my_func(d):\n",
" return d + DateOffset(months=3)\n",
"\n",
"f = lambda x: my_func(x.date)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# MAP\n",
"\n",
"z = ddf.map_partitions(f, meta=(pd.Timestamp))\n",
"ddf = ddf.assign(date2=z) # Assignement\n",
"with ProgressBar():\n",
" ddf.compute()\n",
"ddf.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# APPLY\n",
"\n",
"z = ddf.apply(f, axis=1, meta=pd.Timestamp)\n",
"ddf['date2'] = z # Assignement\n",
"with ProgressBar():\n",
" ddf.compute()\n",
"ddf.head()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:vol_tools3.5]",
"language": "python",
"name": "conda-env-vol_tools3.5-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.5.3"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment