In [1]:
import pandas as pd
import geopandas as gpd
import shapely
import numpy as np
import matplotlib.pyplot as plt
# %matplotlib inline
# pd.set_option("max_columns", None)

In [4]:
fine_df_file = '../data/interim/fine_enriched_parking_violations.tsv'

In [5]:
df = pd.read_csv(fine_df_file, sep='\t', parse_dates=['ticket_issue_datetime'])
# df['counter'] = 1

In [6]:
# Load Up Shape File

In [2]:
#from http://opendata.dc.gov/datasets/f6c703ebe2534fc3800609a07bad8f5b_17
parking_beat = gpd.read_file("../core/parking_beat/DPW_Parking_Beats.shp")
parking_beat


Out[2]:
BEAT_ID OBJECTID Shape_Area Shape_Leng WARD_ID geometry
0 2E08 1 1.087943e+05 2065.511432 2 POLYGON ((-77.07284008806842 38.90922603213077...
1 4B01 2 5.888287e+05 3737.972340 4 POLYGON ((-77.02648789373752 38.98467250046636...
2 4C09 3 2.656067e+05 3616.797624 4 POLYGON ((-77.01743753247668 38.94861915211851...
3 5C02 4 1.144147e+06 6127.223768 5 POLYGON ((-76.96122795732586 38.93369267797588...
4 5D01 5 1.161886e+06 5323.264957 5 POLYGON ((-76.99303008676054 38.90409195607624...
5 5D06 6 3.105442e+05 2489.843898 5 POLYGON ((-76.98344470229944 38.90013853850555...
6 5E10 7 2.785257e+05 2839.967596 5 POLYGON ((-77.00244622311548 38.92560197262328...
7 5E08 8 1.756905e+05 1699.113500 5 POLYGON ((-77.00904433327054 38.92024250692058...
8 5E06 9 2.036891e+05 2402.508033 5 POLYGON ((-77.01417625257722 38.91415803960639...
9 6B09 10 6.442668e+05 3694.015341 6 POLYGON ((-76.97940687430157 38.88540338881592...
10 6E02 11 2.829294e+05 2539.419648 6 POLYGON ((-77.01789607473616 38.9140954615865,...
11 6E04 12 2.283540e+05 2401.972598 6 POLYGON ((-77.01591034621457 38.90858156260074...
12 6E05 13 1.500037e+05 1698.773494 6 POLYGON ((-77.01619806779782 38.90020951983058...
13 7C07 14 5.440855e+05 3256.134303 7 POLYGON ((-76.93125759001666 38.91024355200825...
14 7F06 15 4.505299e+05 3419.584789 7 POLYGON ((-76.95302810343209 38.89298654499904...
15 7F05 16 3.089489e+05 2987.535381 7 POLYGON ((-76.9482666390456 38.88979584658379,...
16 8B06 17 2.548869e+05 2983.268251 8 POLYGON ((-76.96692200352373 38.84901094951221...
17 8E02 18 3.886736e+05 2772.937458 8 POLYGON ((-76.9741988409196 38.8502533365274, ...
18 2F04 19 1.660846e+05 2136.770984 2 POLYGON ((-77.02704024046562 38.91115653383562...
19 2F06 20 2.211521e+05 2289.866177 2 POLYGON ((-77.02450447329691 38.90965354659368...
20 2F03 21 9.054206e+04 1254.519165 2 POLYGON ((-77.03117230282027 38.90698621666759...
21 2F05 22 1.931242e+05 2209.488286 2 POLYGON ((-77.02962448408563 38.90619248830735...
22 2B08 23 1.101193e+05 1539.642286 2 POLYGON ((-77.04122594142832 38.9170156195919,...
23 2B09 24 1.651924e+05 1698.227165 2 POLYGON ((-77.03502871090478 38.91700758847743...
24 2B01 25 1.388810e+05 1953.322456 2 POLYGON ((-77.04342798365944 38.91491440244491...
25 2B04 26 1.122980e+05 1340.540792 2 POLYGON ((-77.03716127422202 38.91410055236736...
26 2B03 27 1.496694e+05 1984.630348 2 POLYGON ((-77.03848107753841 38.91344189485536...
27 2B07 28 1.450345e+05 2025.965699 2 POLYGON ((-77.04188932241668 38.91113596557457...
28 3E02 29 7.311047e+05 4077.306636 3 POLYGON ((-77.09330953220501 38.95470947253554...
29 3E01 30 6.572733e+05 4381.550833 3 POLYGON ((-77.07460312840134 38.95073854614341...
... ... ... ... ... ... ...
266 4C05 267 3.748639e+05 3035.463663 4 POLYGON ((-77.02958688515207 38.94506906670293...
267 4C06 268 2.277587e+05 2072.367524 4 POLYGON ((-77.02906636339239 38.940810519694, ...
268 4C07 269 3.721868e+05 3371.393139 4 POLYGON ((-77.02117565241169 38.94620746831188...
269 4C08 270 2.435531e+05 2119.888033 4 POLYGON ((-77.02403226732682 38.93639607558973...
270 4C10 271 2.635688e+05 2570.935890 4 POLYGON ((-77.01331715352769 38.94214165674279...
271 4D01 272 2.009479e+05 2234.151254 4 POLYGON ((-77.02758284424668 38.95409071594234...
272 4D04 273 2.593568e+05 2131.014258 4 POLYGON ((-77.02661408114218 38.94974928612336...
273 4D03 274 2.679143e+05 2896.256230 4 POLYGON ((-77.0176142344388 38.95223045323473,...
274 4D06 275 2.442639e+05 2342.828154 4 POLYGON ((-77.01957618537229 38.94981772520373...
275 4D02 276 3.174412e+05 2749.119044 4 POLYGON ((-77.01923035096921 38.95759741455306...
276 4D05 277 5.697866e+05 3638.138783 4 POLYGON ((-77.00872803920072 38.9486167938885,...
277 4A08 278 2.169164e+06 8378.639079 4 POLYGON ((-77.03973065309513 38.96006339146098...
278 4A07 279 1.418014e+06 6264.630904 4 POLYGON ((-77.03753248818326 38.97284267754259...
279 3G02 280 2.436793e+06 8225.956836 3 POLYGON ((-77.05439433660025 38.9777985951978,...
280 1A08 281 2.156405e+05 2399.237019 1 POLYGON ((-77.01913669130195 38.93401491442542...
281 1A09 282 1.832899e+05 1926.852041 1 POLYGON ((-77.0207628929662 38.93384546087452,...
282 1A10 283 2.089986e+05 2322.995275 1 POLYGON ((-77.01877194737649 38.93256049261549...
283 1A11 284 1.096376e+05 1820.389490 1 POLYGON ((-77.02622444698154 38.92874737175382...
284 1A03 285 7.677013e+04 1277.305063 1 POLYGON ((-77.03263412630392 38.92872594834615...
285 1B02 286 2.622841e+05 2285.666509 1 POLYGON ((-77.0259941686966 38.91918851342313,...
286 1C04 287 2.747101e+05 2977.521592 1 POLYGON ((-77.0464219248981 38.92597950466724,...
287 1C02 288 1.790123e+05 2045.346343 1 POLYGON ((-77.04714122821154 38.92324789380741...
288 1D05 289 2.800708e+05 3483.223889 1 POLYGON ((-77.0485577459299 38.93263211371966,...
289 1D03 290 3.549126e+05 3203.536839 1 POLYGON ((-77.03913291044294 38.93615381559751...
290 4A01 291 1.879073e+06 8591.680527 4 POLYGON ((-77.03634141659005 38.98640488066252...
291 4A02 292 7.980851e+05 3602.872282 4 POLYGON ((-77.02646372615456 38.97947684447482...
292 4A03 293 7.878888e+05 3636.368199 4 POLYGON ((-77.02968446734641 38.97939923116115...
293 4A04 294 3.075091e+05 2473.009397 4 POLYGON ((-77.0323431503396 38.96993330572591,...
294 4A05 295 1.404765e+05 1852.922605 4 POLYGON ((-77.03608280497868 38.96685643664709...
295 2A07 296 1.789538e+05 2943.976849 2 POLYGON ((-77.05084254486169 38.90278404094224...

296 rows × 6 columns


In [100]:
# neighborhood_clusters.head(10).plot()

In [92]:
# import mplleaflet

# neighborhood_clusters.plot()
# mplleaflet.display()


Out[92]:

In [127]:
sample_df = df

In [128]:
dc_df = gpd.GeoDataFrame(df, geometry=df.apply(
        lambda srs: shapely.geometry.Point(srs['x'], srs['y']), axis='columns'
    ))

In [ ]:


In [ ]:


In [ ]:


In [111]:
def assign_geo_designation_tract(row):
    """ Takes in a geopandas frame and creates a new column categorizing which geoshape a row contains"""
    bools = [geom.contains(row['geometry']) for geom in neighborhood_clusters['geometry']]
    if True in bools:
        return neighborhood_clusters.iloc[bools.index(True)]['NBH_NAMES']
    else:
        return np.nan

In [129]:
dc_df['neighborhood'] = dc_df.apply(assign_geo_designation_tract, axis='columns')


---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-129-2f6abe6f2573> in <module>()
----> 1 dc_df['neighborhood'] = dc_df.apply(assign_geo_designation_tract, axis='columns')

/usr/local/var/pyenv/versions/3.5.2/envs/parking_data/lib/python3.5/site-packages/pandas/core/frame.py in apply(self, func, axis, broadcast, raw, reduce, args, **kwds)
   4161                     if reduce is None:
   4162                         reduce = True
-> 4163                     return self._apply_standard(f, axis, reduce=reduce)
   4164             else:
   4165                 return self._apply_broadcast(f, axis)

/usr/local/var/pyenv/versions/3.5.2/envs/parking_data/lib/python3.5/site-packages/pandas/core/frame.py in _apply_standard(self, func, axis, ignore_failures, reduce)
   4257             try:
   4258                 for i, v in enumerate(series_gen):
-> 4259                     results[i] = func(v)
   4260                     keys.append(v.name)
   4261             except Exception as e:

<ipython-input-111-460e910427e1> in assign_geo_designation_tract(row)
      1 def assign_geo_designation_tract(row):
      2     """ Takes in a geopandas frame and creates a new column categorizing which geoshape a row contains"""
----> 3     bools = [geom.contains(row['geometry']) for geom in neighborhood_clusters['geometry']]
      4     if True in bools:
      5         return neighborhood_clusters.iloc[bools.index(True)]['NBH_NAMES']

<ipython-input-111-460e910427e1> in <listcomp>(.0)
      1 def assign_geo_designation_tract(row):
      2     """ Takes in a geopandas frame and creates a new column categorizing which geoshape a row contains"""
----> 3     bools = [geom.contains(row['geometry']) for geom in neighborhood_clusters['geometry']]
      4     if True in bools:
      5         return neighborhood_clusters.iloc[bools.index(True)]['NBH_NAMES']

/usr/local/var/pyenv/versions/3.5.2/envs/parking_data/lib/python3.5/site-packages/pandas/core/series.py in __getitem__(self, key)
    602 
    603             if not is_scalar(result):
--> 604                 if is_list_like(result) and not isinstance(result, Series):
    605 
    606                     # we need to box if we have a non-unique index here

/usr/local/var/pyenv/versions/3.5.2/envs/parking_data/lib/python3.5/site-packages/pandas/types/inference.py in is_list_like(arg)
     52 
     53 def is_list_like(arg):
---> 54     return (hasattr(arg, '__iter__') and
     55             not isinstance(arg, string_and_binary_types))
     56 

KeyboardInterrupt: 

In [ ]:
neighborhood_pickle = '../data/interim/neighborhood_enriched_parking_violations.pickle'
dc_df.to_pickle(neighborhood_pickle)

In [ ]: