Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error when adding info to gdf #163

Closed
OnnoEbbens opened this issue Apr 12, 2023 · 1 comment
Closed

error when adding info to gdf #163

OnnoEbbens opened this issue Apr 12, 2023 · 1 comment

Comments

@OnnoEbbens
Copy link
Collaborator

When I try to run the function bgt = nlmod.gwf.surface_water.add_stages_from_waterboards(bgt, la) on linux using pandas version 1.5.3 I get this error:


---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[16], line 5
      2 bgt = nlmod.gwf.surface_water.add_min_ahn_to_gdf(bgt, ahn, buffer=5.0)
      4 # add information from level areas (la) to bgt
----> 5 bgt = nlmod.gwf.surface_water.add_stages_from_waterboards(bgt, la)
      7 # add information from watercources (wc) to bgt
      8 bgt = nlmod.gwf.surface_water.add_bottom_height_from_waterboards(bgt, wc)

File ~/.local/lib/python3.9/site-packages/nlmod/gwf/surface_water.py:711, in add_stages_from_waterboards(gdf, la, extent, columns, config, min_total_overlap)
    709         continue
    710     mask = gdf["bronhouder"] == config[wb]["bgt_code"]
--> 711     gdf[mask] = add_info_to_gdf(
    712         la[wb],
    713         gdf[mask],
    714         columns=columns,
    715         min_total_overlap=min_total_overlap,
    716         desc=f"Adding {columns} from {wb}",
    717     )
    718 return gdf

File /opt/tljh/user/lib/python3.9/site-packages/geopandas/geodataframe.py:1443, in GeoDataFrame.__setitem__(self, key, value)
   1441     except TypeError:
   1442         warnings.warn("Geometry column does not contain geometry.")
-> 1443 super().__setitem__(key, value)

File /opt/tljh/user/lib/python3.9/site-packages/pandas/core/frame.py:3968, in DataFrame.__setitem__(self, key, value)
   3966     self._setitem_frame(key, value)
   3967 elif isinstance(key, (Series, np.ndarray, list, Index)):
-> 3968     self._setitem_array(key, value)
   3969 elif isinstance(value, DataFrame):
   3970     self._set_item_frame_value(key, value)

File /opt/tljh/user/lib/python3.9/site-packages/pandas/core/frame.py:4003, in DataFrame._setitem_array(self, key, value)
   4000     if isinstance(value, DataFrame):
   4001         # GH#39931 reindex since iloc does not align
   4002         value = value.reindex(self.index.take(indexer))
-> 4003     self.iloc[indexer] = value
   4005 else:
   4006     # Note: unlike self.iloc[:, indexer] = value, this will
   4007     #  never try to overwrite values inplace
   4009     if isinstance(value, DataFrame):

File /opt/tljh/user/lib/python3.9/site-packages/pandas/core/indexing.py:818, in _LocationIndexer.__setitem__(self, key, value)
    815 self._has_valid_setitem_indexer(key)
    817 iloc = self if self.name == "iloc" else self.obj.iloc
--> 818 iloc._setitem_with_indexer(indexer, value, self.name)

File /opt/tljh/user/lib/python3.9/site-packages/pandas/core/indexing.py:1795, in _iLocIndexer._setitem_with_indexer(self, indexer, value, name)
   1792 # align and set the values
   1793 if take_split_path:
   1794     # We have to operate column-wise
-> 1795     self._setitem_with_indexer_split_path(indexer, value, name)
   1796 else:
   1797     self._setitem_single_block(indexer, value, name)

File /opt/tljh/user/lib/python3.9/site-packages/pandas/core/indexing.py:1831, in _iLocIndexer._setitem_with_indexer_split_path(self, indexer, value, name)
   1828 if is_list_like_indexer(value) and getattr(value, "ndim", 1) > 0:
   1830     if isinstance(value, ABCDataFrame):
-> 1831         self._setitem_with_indexer_frame_value(indexer, value, name)
   1833     elif np.ndim(value) == 2:
   1834         self._setitem_with_indexer_2d_value(indexer, value)

File /opt/tljh/user/lib/python3.9/site-packages/pandas/core/indexing.py:1922, in _iLocIndexer._setitem_with_indexer_frame_value(self, indexer, value, name)
   1920     for i, loc in enumerate(ilocs):
   1921         val = value.iloc[:, i]
-> 1922         self._setitem_single_column(loc, val, pi)
   1924 elif not unique_cols and value.columns.equals(self.obj.columns):
   1925     # We assume we are already aligned, see
   1926     # test_iloc_setitem_frame_duplicate_columns_multiple_blocks
   1927     for loc in ilocs:

File /opt/tljh/user/lib/python3.9/site-packages/pandas/core/indexing.py:1985, in _iLocIndexer._setitem_single_column(self, loc, value, plane_indexer)
   1978 elif (
   1979     is_array_like(value)
   1980     and len(value.shape) > 0
   1981     and self.obj.shape[0] == value.shape[0]
   1982     and not is_empty_indexer(pi)
   1983 ):
   1984     if is_list_like(pi) and not is_bool_dtype(pi):
-> 1985         value = value[np.argsort(pi)]
   1986     else:
   1987         # in case of slice
   1988         value = value[pi]

File /opt/tljh/user/lib/python3.9/site-packages/pandas/core/series.py:1007, in Series.__getitem__(self, key)
   1004     key = np.asarray(key, dtype=bool)
   1005     return self._get_values(key)
-> 1007 return self._get_with(key)

File /opt/tljh/user/lib/python3.9/site-packages/pandas/core/series.py:1042, in Series._get_with(self, key)
   1038 if key_type == "integer":
   1039     # We need to decide whether to treat this as a positional indexer
   1040     #  (i.e. self.iloc) or label-based (i.e. self.loc)
   1041     if not self.index._should_fallback_to_positional:
-> 1042         return self.loc[key]
   1043     else:
   1044         return self.iloc[key]

File /opt/tljh/user/lib/python3.9/site-packages/pandas/core/indexing.py:1073, in _LocationIndexer.__getitem__(self, key)
   1070 axis = self.axis or 0
   1072 maybe_callable = com.apply_if_callable(key, self.obj)
-> 1073 return self._getitem_axis(maybe_callable, axis=axis)

File /opt/tljh/user/lib/python3.9/site-packages/pandas/core/indexing.py:1301, in _LocIndexer._getitem_axis(self, key, axis)
   1298     if hasattr(key, "ndim") and key.ndim > 1:
   1299         raise ValueError("Cannot index with multidimensional key")
-> 1301     return self._getitem_iterable(key, axis=axis)
   1303 # nested tuple slicing
   1304 if is_nested_tuple(key, labels):

File /opt/tljh/user/lib/python3.9/site-packages/pandas/core/indexing.py:1239, in _LocIndexer._getitem_iterable(self, key, axis)
   1236 self._validate_key(key, axis)
   1238 # A collection of keys
-> 1239 keyarr, indexer = self._get_listlike_indexer(key, axis)
   1240 return self.obj._reindex_with_indexers(
   1241     {axis: [keyarr, indexer]}, copy=True, allow_dups=True
   1242 )

File /opt/tljh/user/lib/python3.9/site-packages/pandas/core/indexing.py:1432, in _LocIndexer._get_listlike_indexer(self, key, axis)
   1429 ax = self.obj._get_axis(axis)
   1430 axis_name = self.obj._get_axis_name(axis)
-> 1432 keyarr, indexer = ax._get_indexer_strict(key, axis_name)
   1434 return keyarr, indexer

File /opt/tljh/user/lib/python3.9/site-packages/pandas/core/indexes/base.py:6070, in Index._get_indexer_strict(self, key, axis_name)
   6067 else:
   6068     keyarr, indexer, new_indexer = self._reindex_non_unique(keyarr)
-> 6070 self._raise_if_missing(keyarr, indexer, axis_name)
   6072 keyarr = self.take(indexer)
   6073 if isinstance(key, Index):
   6074     # GH 42790 - Preserve name from an Index

File /opt/tljh/user/lib/python3.9/site-packages/pandas/core/indexes/base.py:6133, in Index._raise_if_missing(self, key, indexer, axis_name)
   6130     raise KeyError(f"None of [{key}] are in the [{axis_name}]")
   6132 not_found = list(ensure_index(key)[missing_mask.nonzero()[0]].unique())
-> 6133 raise KeyError(f"{not_found} not in index")

KeyError: '[0, 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, 28, 29, 30, 31, 33, 34, 36, 40, 43, 44, 45, 46, 47, 48, 49, 50, 51, 53, 54, 55, 57, 58, 60, 61, 62, 63, 64, 65, 66, 67, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 83, 85, 86, 87, 88, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 127, 128, 132, 133, 134, 135, 138, 139] not in index'

When I use gdf.loc[mask] instead of gdf[mask] in the function add_stages_from_waterboards it works fine (see below). I think there is some pandas version thing involved.

gdf.loc[mask] = add_info_to_gdf(
    712         la[wb],
    713         gdf[mask],
    714         columns=columns,
    715         min_total_overlap=min_total_overlap,
    716         desc=f"Adding {columns} from {wb}",
    717     )
OnnoEbbens added a commit that referenced this issue Apr 12, 2023
@OnnoEbbens
Copy link
Collaborator Author

closed by adding .loc as suggested

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant