Repository: pydata/sparse

Issue:
Setting DOK elements to zero doesn't work.
Trivial example:

```
>>> import sparse
>>> import numpy as np
>>> s = sparse.DOK((1,))
>>> s[0] = 1
>>> s[0]
1.0
>>> s[0] = 0
>>> s[0]
1.0
```

Discussion:

Patch:
diff --git a/sparse/dok.py b/sparse/dok.py
index 65ede6f..9beaada 100644
--- a/sparse/dok.py
+++ b/sparse/dok.py
@@ -339,8 +339,11 @@ class DOK(object):
                 raise IndexError('All indices must be slices or integers'
                                  ' when setting an item.')
 
+        key = tuple(key_list)
         if value != _zero_of_dtype(self.dtype):
-            self.data[tuple(key_list)] = value[()]
+            self.data[key] = value[()]
+        elif key in self.data:
+            del self.data[key]
 
     def __str__(self):
         return "<DOK: shape=%s, dtype=%s, nnz=%d>" % (self.shape, self.dtype, self.nnz)


Test patch:
diff --git a/sparse/tests/test_dok.py b/sparse/tests/test_dok.py
index 568c1d0..45c9899 100644
--- a/sparse/tests/test_dok.py
+++ b/sparse/tests/test_dok.py
@@ -149,3 +149,12 @@ def test_float_dtype():
     s = DOK((5,), data)
 
     assert s.dtype == np.float32
+
+
+def test_set_zero():
+    s = DOK((1,), dtype=np.uint8)
+    s[0] = 1
+    s[0] = 0
+
+    assert s[0] == 0
+    assert s.nnz == 0