-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
37 lines (32 loc) · 885 Bytes
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import folium as fm
import pandas as pd
import numpy as np
from folium.plugins import HeatMap
def create_shipping_heatmap(
db,
conn,
*args,
**kwargs,
) -> fm.Map:
df = db.get_shipping(conn)
df.dropna(subset=['latitude', 'longitude'], inplace=True)
m = fm.Map(
location=(df.latitude.mean(), df.longitude.mean()),
zoom_start=6,
tiles='cartodbpositron'
)
HeatMap(
[(i['latitude'], i['longitude']) for _, i in df.iterrows()]
).add_to(m)
return m
def id_nan_to_none(
df: pd.DataFrame,
*args,
**kwargs,
) -> pd.DataFrame:
''' Replace np.nan with None in any column with "id" in its name
Useful since Postgres does not allow NaN values in the Bigint datatype, but
None values is fine.
'''
df[list(df.columns[df.columns.str.contains('id')])].replace({np.nan: None})
return df