From 6cef7af5abc8b7eef4867bb85eb03eb2cf28f83d Mon Sep 17 00:00:00 2001 From: Dave Hirschfeld Date: Fri, 21 Apr 2017 06:57:31 +1000 Subject: [PATCH] Allow for handling Map types in conversion to numpy dtypes (#224) * Added IDE section to .gitignore Ignore PyCharm folder * Handle Map types in conversion to numpy In the case of Map types only keep the key Fixes blaze/odo#534 * Fix `test_map` to account for new behaviour --- .gitignore | 3 +++ datashape/coretypes.py | 7 ++++++- datashape/tests/test_coretypes.py | 3 +-- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 1d8dd28..bdb4810 100644 --- a/.gitignore +++ b/.gitignore @@ -48,3 +48,6 @@ coverage.xml # Sphinx documentation docs/_build/ + +# IDE +.idea/ diff --git a/datashape/coretypes.py b/datashape/coretypes.py index e37b594..327a23d 100644 --- a/datashape/coretypes.py +++ b/datashape/coretypes.py @@ -364,7 +364,7 @@ class Bytes(Unit): u'U32': u'U32', u'utf-32': u'U32', u'utf_32': u'U32', - u'utf32': u'U32' + u'utf32': u'U32', } @@ -907,6 +907,9 @@ def __str__(self): self.key, self.value) + def to_numpy_dtype(self): + return to_numpy_dtype(self) + def _launder(x): """ Clean up types prior to insertion into DataShape @@ -1276,6 +1279,8 @@ def __str__(self): def to_numpy_dtype(ds): """ Throw away the shape information and just return the measure as NumPy dtype instance.""" + if isinstance(ds.measure, datashape.coretypes.Map): + ds = ds.measure.key return to_numpy(ds.measure)[1] diff --git a/datashape/tests/test_coretypes.py b/datashape/tests/test_coretypes.py index 4f15a80..000b6c9 100644 --- a/datashape/tests/test_coretypes.py +++ b/datashape/tests/test_coretypes.py @@ -590,8 +590,7 @@ def test_map(): assert fk.value == Record([('a', int32)]) assert fk.value.dict == {'a': int32} assert fk.value.fields == (('a', int32),) - with pytest.raises(TypeError): - fk.to_numpy_dtype() + assert fk.to_numpy_dtype() == np.dtype('int32') def test_map_parse():