You learn something new every day in GIS.
If I open a shapefile attribute table, select an attribute, and use the field calculator to populate the field with an expression such as...
attributeX / 0
I get an error all calculated values were invalid, no rows were updated.
But
attributeX / attributeY returns no error even if values in attributeY have zero values, In fact, it populates those values with zero values.
This is disturbing.
If I do this same test in a geodatabase I get null values for the zero division rows.
I understand that there are data structure limitations. Shapefile fields are not nullable... but the shapefile example ought to give some warning!
Having trouble with Python when dividing by zero?
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
5/0
ZeroDivisionError: integer division or modulo by zero
Better get Chuck Norris to help:
def chuck_norris(a, b): try: value = a/b except ZeroDivisionError: if (a == 0): value = float('nan') elif (a > 0): value = float("inf") else: value = float("-inf") return value
I was always an Arnold fan
def arnold(a,b): import numpy as np return (max(np.inf,a*b/1.)) print("I'll be back in... {!r:}".format(arnold(5,0)))
That is one hell of a spinning back kick ...
Still goes to my point (my laziness) that this should be embedded within the language and not have to be manually accounted for (and thought of).
I believe that I have been spoiled by other 4th generation languages and have the expection that python honors certain expected rules. When it does not intrinsically, I am left with no/lesser confidence. Again I am beating a dead horse... either jump on the train or get left behind.
def Live_Long_and_Prosper(vulcan)
return "Needs of the Many outweigh the Needs of the few"
print Live_Long_and_Prosper
FYI for the 5th generation... in python 3.x that would be
print("always a lesson...{}".format(Live_Long_and_Prosper() )
Dan I agree with your statements -- It is more of Laziness on my part. I would rather use a function isNull rather than a def for trapping all possibilities of nullness. In the same hand, knowing a division by zero will halt my program/script in its tracks. Pure Laziness!
However, I use python on databases outside of GIS/No shapefiles and still I have problems with Nulls! Yes they are Pesky! One day we will avoid Null in database design and this issue will go away (and replaced by with some other pesky issue) --- Keeping us in business.
Yes.. it would be useful to implement the concept of a mask in database design, effectively excluding any operation from including records that are masked, unless you want to deal with the masked records explicitly
Gerry, maybe you can change this to a Discussion since there is obviously no answer that suits all situations.