Skip to content

Commit 0cbad82

Browse files
Merge pull request #653 from dimitri-yatsenko/dev
Fix fetches of nullable integer attributes
2 parents 272531b + c90f1f4 commit 0cbad82

File tree

4 files changed

+33
-17
lines changed

4 files changed

+33
-17
lines changed

datajoint/fetch.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,14 @@ def __call__(self, *attrs, offset=None, limit=None, order_by=None, format=None,
174174
else:
175175
ret = list(cur.fetchall())
176176
record_type = (heading.as_dtype if not ret else np.dtype(
177-
[(name, type(value))
178-
if heading.as_dtype[name] == 'O' and isinstance(
179-
value, numbers.Number) # value of blob is packed here
177+
[(name, type(value)) # use the first element to determine the type for blobs
178+
if heading[name].is_blob and isinstance(value, numbers.Number)
180179
else (name, heading.as_dtype[name])
181-
for value, name
182-
in zip(ret[0], heading.as_dtype.names)]))
183-
ret = np.array(ret, dtype=record_type)
180+
for value, name in zip(ret[0], heading.as_dtype.names)]))
181+
try:
182+
ret = np.array(ret, dtype=record_type)
183+
except Exception as e:
184+
raise
184185
for name in heading:
185186
ret[name] = list(map(partial(get, heading[name]), ret[name]))
186187
if format == "frame":

tests/schema.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ class TTest3(dj.Manual):
3939
"""
4040

4141

42+
@schema
43+
class NullableNumbers(dj.Manual):
44+
definition = """
45+
key : int
46+
---
47+
fvalue = null : float
48+
dvalue = null : double
49+
ivalue = null : int
50+
"""
51+
52+
4253
@schema
4354
class TTestExtra(dj.Manual):
4455
"""

tests/test_fetch.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,15 @@ def test_decimal(self):
203203
assert_true(len(rel & keys[0]) == 1)
204204
keys = rel.fetch(dj.key)
205205
assert_true(len(rel & keys[1]) == 1)
206+
207+
def test_nullable_numbers(self):
208+
""" test mixture of values and nulls in numeric attributes """
209+
table = schema.NullableNumbers()
210+
table.insert((
211+
(k, np.random.randn(), np.random.randint(-1000, 1000), np.random.randn())
212+
for k in range(10)))
213+
table.insert1((100, None, None, None))
214+
f, d, i = table.fetch('fvalue', 'dvalue', 'ivalue')
215+
assert_true(None in i)
216+
assert_true(any(np.isnan(d)))
217+
assert_true(any(np.isnan(f)))

tests/test_fetch_same.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,31 +35,23 @@ class TestFetchSame:
3535

3636
@staticmethod
3737
def test_object_conversion_one():
38-
39-
new = ProjData.proj(sub='resp-sim').fetch('sub')
40-
38+
new = ProjData.proj(sub='resp').fetch('sub')
4139
assert_equal(new.dtype, np.float64)
4240

4341
@staticmethod
4442
def test_object_conversion_two():
45-
46-
[sub, add] = ProjData.proj(sub='resp-sim', add='resp+sim').fetch(
47-
'sub', 'add')
48-
43+
[sub, add] = ProjData.proj(sub='resp', add='sim').fetch('sub', 'add')
4944
assert_equal(sub.dtype, np.float64)
5045
assert_equal(add.dtype, np.float64)
5146

5247
@staticmethod
5348
def test_object_conversion_all():
54-
55-
new = ProjData.proj(sub='resp-sim', add='resp+sim').fetch()
56-
49+
new = ProjData.proj(sub='resp', add='sim').fetch()
5750
assert_equal(new['sub'].dtype, np.float64)
5851
assert_equal(new['add'].dtype, np.float64)
5952

6053
@staticmethod
6154
def test_object_no_convert():
62-
6355
new = ProjData.fetch()
6456
assert_equal(new['big'].dtype, 'object')
6557
assert_equal(new['blah'].dtype, 'object')

0 commit comments

Comments
 (0)