Skip to content

Instantly share code, notes, and snippets.

@EthanRosenthal
Created February 17, 2017 16:27
Show Gist options
  • Save EthanRosenthal/ba4e2455cb7705c8d6b840d35c5316b3 to your computer and use it in GitHub Desktop.
Save EthanRosenthal/ba4e2455cb7705c8d6b840d35c5316b3 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def empty_time(df):\n",
" \n",
" # Get all the times\n",
" bins = df.daytime.unique()\n",
" bins.sort()\n",
" first = np.where(bins == '18:00:00')[0][0]\n",
" last = np.where(bins == '19:00:00')[0][0]\n",
" bins = bins[first:last]\n",
" n_bins = bins.shape[0]\n",
"\n",
" # Ignore weekends, just grab between 9-10\n",
" # Group by calendar date\n",
" groups = (df[(~np.in1d(df.index.dayofweek, [5, 6]))\n",
" & (df.daytime.isin(bins))]\n",
" .groupby('cal_date'))\n",
"\n",
" all_times = 0\n",
" zero_times = 0\n",
" for i, group in groups:\n",
" these_zero_times = group[group.available_bikes == 0]\n",
" all_times += n_bins \n",
" if not these_zero_times.empty:\n",
" min_time = these_zero_times.daytime.min()\n",
" initial_zero = np.where(bins == min_time)[0].min()\n",
" zero_times += n_bins - initial_zero\n",
" return zero_times / all_times"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Old office (Howard) empty probability = 20.36%\n",
"New office (Macdougal) empty probability = 9.04%\n"
]
}
],
"source": [
"mac_prob = empty_time(macdougal) * 100\n",
"how_prob = empty_time(howard) * 100\n",
"\n",
"print('Old office (Howard) empty probability = {:4.2f}%'.format(how_prob))\n",
"print('New office (Macdougal) empty probability = {:4.2f}%'.format(mac_prob))"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [Root]",
"language": "python",
"name": "Python [Root]"
},
"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.2"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment