def make_crimerate_df(self, crimes): crimes = pd.DataFrame(crimes, columns=["date_reported", "agg_area", "count"]) crimes["year_reported"] = crimes["date_reported"].apply( lambda d: date(d).year) crimes["month_reported"] = crimes["date_reported"].apply( lambda d: date(d).month) crimes = crimes.set_index("agg_area") return crimes[["year_reported", "month_reported", "count"]]
def make_expected_df(self, expected): expected = pd.DataFrame( expected, columns=["parcel_id", "inspection_date", "crime_rate"]) expected["inspection_date"] = expected["inspection_date"].apply( lambda d: date(d)) expected = expected.set_index(["parcel_id", "inspection_date"]) return expected
def test_one_inspection_one_crime_missing_tract(self): crimes = [("16Sep2014", "tract567", 3)] parcels = [("parcelA", "01Dec2014", "tract567")] population = [] window = datetime.timedelta(days=365) expected = [("parcelA", date("01Dec2014"), np.nan)] actual =crime.crimerate_in_aggregation_area(self.make_parcels_df(parcels), self.make_crimerate_df(crimes), self.make_population_df(population), window) actual = actual.reset_index()[["parcel_id", "inspection_date", "crime_rate"]].values self.assert_array_almost_equal(expected, actual)
def test_one_inspection_several_crimes(self): crimes = [("16Sep2014", "tract567", 3), ("18Oct2014", "tract567", 1)] parcels = [("parcelA", "01Dec2014", "tract567")] population = [("tract567", 1234)] window = datetime.timedelta(days=365) expected = [("parcelA", date("01Dec2014"), 4 / float(1234))] actual = crime.crimerate_in_aggregation_area( self.make_parcels_df(parcels), self.make_crimerate_df(crimes), self.make_population_df(population), window) actual = actual.reset_index()[[ "parcel_id", "inspection_date", "crime_rate" ]].values self.assert_array_almost_equal(expected, actual)
def make_parcels_df(self, parcels): parcels = pd.DataFrame( parcels, columns=["parcel_id", "inspection_date", "agg_area"]) parcels["inspection_date"] = parcels["inspection_date"].apply( lambda d: date(d)) return parcels
def make_expected_df(self, expected): expected = pd.DataFrame(expected, columns=["parcel_id", "inspection_date", "crime_rate"]) expected["inspection_date"] = expected["inspection_date"].apply(lambda d: date(d)) expected = expected.set_index(["parcel_id", "inspection_date"]) return expected
def make_parcels_df(self, parcels): parcels = pd.DataFrame(parcels, columns=["parcel_id", "inspection_date", "agg_area"]) parcels["inspection_date"] = parcels["inspection_date"].apply(lambda d: date(d)) return parcels
def make_crimerate_df(self, crimes): crimes = pd.DataFrame(crimes, columns=["date_reported", "agg_area", "count"]) crimes["year_reported"] = crimes["date_reported"].apply(lambda d: date(d).year) crimes["month_reported"] = crimes["date_reported"].apply(lambda d: date(d).month) crimes = crimes.set_index("agg_area") return crimes[["year_reported", "month_reported", "count"]]