In [130]:
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 [85]:
#from http://opendata.dc.gov/datasets/f6c703ebe2534fc3800609a07bad8f5b_17
neighborhood_clusters = gpd.read_file("../core/neighborhood_clusters/Neighborhood_Clusters.shp")
# neighborhood_clusters.plot()


Out[85]:
<matplotlib.axes._subplots.AxesSubplot at 0x1b4aef128>

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 [ ]: