I'm trying to plot a folium heatmap in Jupyter, but I get the error ValueError: Location values cannot contain NaNs, got: [nan, nan] So I need to delete the masked elements from a list of arrays. My arrays look like this:
Xpos
Out[68]:
masked_array(
data=[[--, --, --, ..., --, --, --],
[--, --, --, ..., --, --, --],
[--, --, --, ..., --, --, --],
...,
[-3.6872851848602295, -3.732004165649414, -3.7555367946624756,
..., --, --, --],
[-3.819749116897583, -3.824702739715576, -3.804812431335449, ...,
--, --, --],
[-3.6894078254699707, -3.7181897163391113, -3.7022457122802734,
..., --, --, --]],
mask=[[ True, True, True, ..., True, True, True],
[ True, True, True, ..., True, True, True],
[ True, True, True, ..., True, True, True],
...,
[False, False, False, ..., True, True, True],
[False, False, False, ..., True, True, True],
[False, False, False, ..., True, True, True]],
fill_value=9.96921e+36,
dtype=float32)
Each array in the list has a different amount of masked elements, some are only masked at the end, others are completely masked. How do I delete every masked element? So completely removing the completely masked ones and only deleting a few elements in the others. This would result in a list where the arrays have different lengths.
My entire code is:
#read in data:
matplotlib.use('Qt4Agg', warn=False)
def ncdump(nc_fid, verb=True):
def print_ncattr(key):
try:
#print("\t\ttype:", repr(nc_fid.variables[key].dtype))
for ncattr in nc_fid.variables[key].ncattrs():
print('\t\t%s:' % ncattr,\
repr(nc_fid.variables[key].getncattr(ncattr)))
except KeyError:
print("\t\tWARNING: %s does not contain variable attributes" % key)
# NetCDF global attributes
nc_attrs = nc_fid.ncattrs()
if verb:
print("NetCDF Global Attributes:")
for nc_attr in nc_attrs:
print('\t%s:' % nc_attr, repr(nc_fid.getncattr(nc_attr)))
nc_dims = [dim for dim in nc_fid.dimensions] # list of nc dimensions
# Dimension shape information.
if verb:
#print("NetCDF dimension information:")
for dim in nc_dims:
#print("\tName:", dim)
#print("\t\tsize:", len(nc_fid.dimensions[dim]))
print_ncattr(dim)
# Variable information.
nc_vars = [var for var in nc_fid.variables] # list of nc variables
if verb:
print("NetCDF variable information:")
for var in nc_vars:
if var not in nc_dims:
print('\tName:', var)
print("\t\tdimensions:", nc_fid.variables[var].dimensions)
print("\t\tsize:", nc_fid.variables[var].size)
print_ncattr(var)
return nc_attrs, nc_dims, nc_vars
###
results = nc.Dataset('H:/EMBC/Thesis/Python/NoS2, Aa/201302cohNoS10001_1.tspar1.nc')#K
Longitude = results.variables['lon'][:]
Latitude = results.variables['lat'][:]
Time=results.variables['time'][:]
Xpos=results.variables['part_xpos'][:]
Ypos=results.variables['part_ypos'][:]
Trajectory=results.variables['trajectory'][:]
#%% Density
Density = np.tile(1,len(Ypos[0]))
print(len(Density),len(Ypos[0]),len(Xpos[0]))
#Create map
m = folium.Map([52.5, 2], zoom_start=5.5)
divvyStations =df = pd.DataFrame({'Xpos': Xpos[0], 'Ypos':
Ypos[0],'Density': Density})
divvyStations.head()
OUT:
Density Xpos Ypos
0 1 NaN NaN
1 1 NaN NaN
2 1 NaN NaN
3 1 NaN NaN
4 1 NaN NaN
#The first values are all NaN
#Now the part that gives me the error:
for index, row in divvyStations.iterrows():
folium.CircleMarker([row['Ypos'],row['Xpos'] ],
radius=1,
#popup=row['Density'],
fill_color="#3db7e4", # divvy color
).add_to(m)#, row['Density']
Which gives me the error:
ValueError Traceback (most recent call last)
<ipython-input-3-2a47eaba6e43> in <module>()
4 radius=1,
5 #popup=row['Density'],
----> 6 fill_color="#3db7e4", # divvy color
7 ).add_to(m)#, row['Density']
8 m
D:\ProgramData\Anaconda3\lib\site-packages\folium\features.py in __init__(self, location, radius, popup, **kw)
868 """
869 def __init__(self, location, radius=10, popup=None, **kw):
--> 870 super(CircleMarker, self).__init__(location=location, popup=popup)
871 self._name = 'CircleMarker'
872 options = _parse_path(**kw)
D:\ProgramData\Anaconda3\lib\site-packages\folium\map.py in __init__(self, location, popup, icon)
645 # Must be _validate_coordinates b/c some markers are defined with
646 # multiple coordinates values, like Polygons.
--> 647 self.location = _validate_coordinates(location)
648 if icon is not None:
649 self.add_child(icon)
D:\ProgramData\Anaconda3\lib\site-packages\folium\utilities.py in _validate_coordinates(coordinates)
46 if _isnan(coordinates):
47 raise ValueError('Location values cannot contain NaNs, ' ---> 48 'got:\n{!r}'.format(coordinates))
49 coordinates = _locations_tolist(coordinates)
50 return coordinates
ValueError: Location values cannot contain NaNs, got:
[nan, nan]
So I need to delete my NaN's from Xpos and Ypos (Ypos is similar to Xpos)
I tried:
- ma.compress_cols(a) from https://docs.scipy.org/doc/numpy-1.13.0/reference/routines.ma.html. which deletes too much and when I try to go through every array seperately in a loop it tells me it only works on 2D arrays.
- Deleting masked elements in numpy array which gives me AttributeError: 'list' object has no attribute 'mask'
- Deleting masked elements in arrays by index which also deletes too much
Any help is appreciated!