문제 설명
Geopandas Dataframe이 인덱스 오류를 반환하는 이유 (Why is the Geopandas Dataframe returning an Index Error)
저는 VS Code 노트북에서 geopandas 0.6.1과 pandas 1.0.0을 사용하고 있습니다. 오늘 작업하는 동안 겉보기에 유효한 geopandas 데이터 프레임에서 geopandas 함수 head()를 호출하여 아래 오류가 발생했다는 사실에 놀랐습니다.
gpd.columns 및 gpd.shape와 같은 메소드는 정상적으로 작동하고 예상 결과를 반환합니다. .iloc은 예상 레코드를 반환합니다. 예:
for item in new_west_df.columns.values.tolist():
print(item)
TYPE
NWID
LICENCE
TRADE_NAME
LICENCEE_NAME
CIVIC_ADDRESS
LICENCE_DESCRIPTION
YEAR_OPENED
APPROVED_DATE
SIC
SIC_GROUP
CIVIC_POSTAL_CODE
geometry
그러나 columns() 및 .geometry는 아래 오류를 발생시킵니다. 문제에 대한 내 연구에서 이러한 조건과 유사한 시나리오는 없었으며 오류를 읽으면 근본 원인을 이해할 수 없습니다.
IndexError Traceback (most recent call last)
/Library/Frameworks/anaconda3/envs/vm/lib/python3.7/site‑packages/IPython/core/formatters.py in __call__(self, obj)
700 type_pprinters=self.type_printers,
701 deferred_pprinters=self.deferred_printers)
‑‑> 702 printer.pretty(obj)
703 printer.flush()
704 return stream.getvalue()
/Library/Frameworks/anaconda3/envs/vm/lib/python3.7/site‑packages/IPython/lib/pretty.py in pretty(self, obj)
397 if cls is not object \
398 and callable(cls.__dict__.get('__repr__')):
‑‑> 399 return _repr_pprint(obj, self, cycle)
400
401 return _default_pprint(obj, self, cycle)
/Library/Frameworks/anaconda3/envs/vm/lib/python3.7/site‑packages/IPython/lib/pretty.py in _repr_pprint(obj, p, cycle)
687 """A pprint that just redirects to the normal repr function."""
688 # Find newlines and replace them with p.break_()
‑‑> 689 output = repr(obj)
690 for idx,output_line in enumerate(output.splitlines()):
691 if idx:
/Library/Frameworks/anaconda3/envs/vm/lib/python3.7/site‑packages/pandas/core/series.py in __repr__(self)
1370 min_rows=min_rows,
1371 max_rows=max_rows,
‑> 1372 length=show_dimensions,
1373 )
1374 result = buf.getvalue()
/Library/Frameworks/anaconda3/envs/vm/lib/python3.7/site‑packages/pandas/core/series.py in to_string(self, buf, na_rep, float_format, header, index, length, dtype, name, max_rows, min_rows)
1436 max_rows=max_rows,
1437 )
‑> 1438 result = formatter.to_string()
1439
1440 # catch contract violations
/Library/Frameworks/anaconda3/envs/vm/lib/python3.7/site‑packages/pandas/io/formats/format.py in to_string(self)
359
360 fmt_index, have_header = self._get_formatted_index()
‑‑> 361 fmt_values = self._get_formatted_values()
362
363 if self.truncate_v:
/Library/Frameworks/anaconda3/envs/vm/lib/python3.7/site‑packages/pandas/io/formats/format.py in _get_formatted_values(self)
346 None,
347 float_format=self.float_format,
‑‑> 348 na_rep=self.na_rep,
349 )
350
/Library/Frameworks/anaconda3/envs/vm/lib/python3.7/site‑packages/pandas/io/formats/format.py in format_array(values, formatter, float_format, na_rep, digits, space, justify, decimal, leading_space)
1172 )
1173
‑> 1174 return fmt_obj.get_result()
1175
1176
/Library/Frameworks/anaconda3/envs/vm/lib/python3.7/site‑packages/pandas/io/formats/format.py in get_result(self)
1203
1204 def get_result(self) ‑> List[str]:
‑> 1205 fmt_values = self._format_strings()
1206 return _make_fixed_width(fmt_values, self.justify)
1207
/Library/Frameworks/anaconda3/envs/vm/lib/python3.7/site‑packages/pandas/io/formats/format.py in _format_strings(self)
1483 values = values._values
1484
‑> 1485 formatter = values._formatter(boxed=True)
1486
1487 if is_categorical_dtype(values.dtype):
/Library/Frameworks/anaconda3/envs/vm/lib/python3.7/site‑packages/geopandas/array.py in _formatter(self, boxed)
1009 # dummy heuristic based on 10 first geometries that should
1010 # work in most cases
‑> 1011 xmin, ymin, xmax, ymax = self[~self.isna()][:10].total_bounds
1012 if (
1013 (‑180 <= xmin <= 180)
/Library/Frameworks/anaconda3/envs/vm/lib/python3.7/site‑packages/geopandas/array.py in total_bounds(self)
750 return np.array(
751 (
‑‑> 752 b[:, 0].min(), # minx
753 b[:, 1].min(), # miny
754 b[:, 2].max(), # maxx
IndexError: too many indices for array
참조 솔루션
방법 1:
This is a bug that was fixed in GeoPandas 0.6.2 (https://github.com/geopandas/geopandas/releases/tag/v0.6.2), so if you upgrade geopandas, it should be solved.