pysal / libpysal

Core components of Python Spatial Analysis Library
http://pysal.org/libpysal
Other
259 stars 78 forks source link

try_weightcol is incorrectly defined using getattr() #669

Open Ryclock opened 9 months ago

Ryclock commented 9 months ago

Question Description

In line 372 of the libpysal/weights/weights.py file, the getattr function is called to assign a value to try_weightcol. However, since the third parameter of getattr (the default value to be returned when the attribute is not found) is not passed, it will result in an AttributeError: 'DataFrame' object has no attribute 'weight' if the 'weight_col' column is not present in the 'adjlist', and this will terminate the program.

So, when will the code at lines 373-375 be executed?

if try_weightcol is None:
    adjlist = adjlist.copy(deep=True)
    adjlist["weight"] = 1

Essential Information

ljwolf commented 9 months ago

Thank you for the report!

Yes, I think this should instead use the data frame's .get() method rather than getattr()

try_weightcol = adjlist.get(weight_col)

This should be a fairly straightforward fix.

martinfleis commented 6 months ago

I don't think it has been fixed yet.