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

Creating Weights from dataframe: IndexError: index 0 is out of bounds for axis 0 with size 0 #479

Closed
raholler opened this issue Oct 16, 2022 · 4 comments
Assignees

Comments

@raholler
Copy link

I get the following error when running

W = libpysal.weights.KNN.from_dataframe(df,  k=4)
File Anaconda3\envs\geo_new\lib\site-packages\libpysal\weights\distance.py:905, in DistanceBand.from_dataframe(cls, df, threshold, geom_col, ids, **kwargs)
    903 if geom_col is None:
    904     geom_col = df.geometry.name
--> 905 pts = get_points_array(df[geom_col])
    906 if ids is None:
    907     ids = df.index.tolist()

File Anaconda3\envs\geo_new\lib\site-packages\libpysal\weights\util.py:1081, in get_points_array(iterable)
   1078 try:
   1079     if HAS_SHAPELY:
   1080         data = np.vstack(
-> 1081             [
   1082                 np.array(shape.centroid.coords)[0]
   1083                 if isinstance(shape, BaseGeometry)
   1084                 else np.array(shape.centroid)
   1085                 for shape in first_choice
   1086             ]
...
   1092                 for shape in first_choice
   1093             ]
   1094         )

IndexError: index 0 is out of bounds for axis 0 with size

The error disappears, when I restrict the geodataframe to areas without islands.

  • Platform information: nt win32
  • Python version: 3.10.6
  • SciPy version: 1.9.1
  • NumPy version: 1.23.2
  • Geopandas: 0.11.1
  • libpysal: 4.6.2

test.zip

@raholler raholler changed the title Creating Weighting Matrix from dataframe: IndexError: index 0 is out of bounds for axis 0 with size 0 Creating Weights from dataframe: IndexError: index 0 is out of bounds for axis 0 with size 0 Oct 16, 2022
@sjsrey sjsrey self-assigned this Oct 27, 2022
@sjsrey
Copy link
Member

sjsrey commented Oct 27, 2022

The issue seems to be that there are observations that do not have geometries:

image

Without coordinates, it isn't possible to calculate the distances needed for the knn determination.
If you drop those observations, all is well.

@sjsrey
Copy link
Member

sjsrey commented Oct 27, 2022

There are 11 such observations:
image

@raholler
Copy link
Author

Thanks for the swift response. I am sorry, I actually checked in the originial data

data.query("geometry.isnull()")

and it seemed that there were no missing geometries. I will check again, what is going on.

@sjsrey
Copy link
Member

sjsrey commented Oct 29, 2022

Closing since the problem is not due to a bug.

@sjsrey sjsrey closed this as completed Oct 29, 2022
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

2 participants