diff options
author | Stephan Hoyer <shoyer@gmail.com> | 2016-08-17 08:49:49 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-08-17 08:49:49 -0700 |
commit | 912e24a4ac2d0470bb18d659d325197bbfc18114 (patch) | |
tree | 32f1acc7854dfb0b3ed0764a26f8349a5ef39c2a /numpy/lib | |
parent | 630917f4e9dd432da1733fceff0c7c455973fa70 (diff) | |
parent | 60b3727e6937891a9b91bac4ffb097bb3b5bd25d (diff) | |
download | numpy-912e24a4ac2d0470bb18d659d325197bbfc18114.tar.gz |
Merge pull request #7936 from goerz/sparse-type-check
ENH: improve duck typing inside iscomplexobj
Diffstat (limited to 'numpy/lib')
-rw-r--r-- | numpy/lib/tests/test_type_check.py | 35 | ||||
-rw-r--r-- | numpy/lib/type_check.py | 12 |
2 files changed, 45 insertions, 2 deletions
diff --git a/numpy/lib/tests/test_type_check.py b/numpy/lib/tests/test_type_check.py index f7430c27d..93a4da97a 100644 --- a/numpy/lib/tests/test_type_check.py +++ b/numpy/lib/tests/test_type_check.py @@ -148,6 +148,41 @@ class TestIscomplexobj(TestCase): z = np.array([-1j, 0, -1]) assert_(iscomplexobj(z)) + def test_scalar(self): + assert_(not iscomplexobj(1.0)) + assert_(iscomplexobj(1+0j)) + + def test_list(self): + assert_(iscomplexobj([3, 1+0j, True])) + assert_(not iscomplexobj([3, 1, True])) + + def test_duck(self): + class DummyComplexArray: + @property + def dtype(self): + return np.dtype(complex) + dummy = DummyComplexArray() + assert_(iscomplexobj(dummy)) + + def test_pandas_duck(self): + # This tests a custom np.dtype duck-typed class, such as used by pandas + # (pandas.core.dtypes) + class PdComplex(np.complex128): + pass + class PdDtype(object): + name = 'category' + names = None + type = PdComplex + kind = 'c' + str = '<c16' + base = np.dtype('complex128') + class DummyPd: + @property + def dtype(self): + return PdDtype + dummy = DummyPd() + assert_(iscomplexobj(dummy)) + class TestIsrealobj(TestCase): def test_basic(self): diff --git a/numpy/lib/type_check.py b/numpy/lib/type_check.py index d6e0704ad..f620d49d5 100644 --- a/numpy/lib/type_check.py +++ b/numpy/lib/type_check.py @@ -266,7 +266,15 @@ def iscomplexobj(x): True """ - return issubclass(asarray(x).dtype.type, _nx.complexfloating) + try: + dtype = x.dtype + except AttributeError: + dtype = asarray(x).dtype + try: + return issubclass(dtype.type, _nx.complexfloating) + except AttributeError: + return False + def isrealobj(x): """ @@ -300,7 +308,7 @@ def isrealobj(x): False """ - return not issubclass(asarray(x).dtype.type, _nx.complexfloating) + return not iscomplexobj(x) #----------------------------------------------------------------------------- |