Esempio n. 1
0
 def test_from_object_array_unicode(self):
     A = np.array([['abc', sixu('Sigma \u03a3')],
                   ['long   ', '0123456789']], dtype='O')
     self.assertRaises(ValueError, np.char.array, (A,))
     B = np.char.array(A, **kw_unicode_true)
     assert_equal(B.dtype.itemsize, 10 * np.array('a', 'U').dtype.itemsize)
     assert_array_equal(B, [['abc', sixu('Sigma \u03a3')],
                            ['long', '0123456789']])
 def test_from_object_array_unicode(self):
     A = np.array([['abc', sixu('Sigma \u03a3')],
                   ['long   ', '0123456789']], dtype='O')
     self.assertRaises(ValueError, np.char.array, (A,))
     B = np.char.array(A, **kw_unicode_true)
     assert_equal(B.dtype.itemsize, 10 * np.array('a', 'U').dtype.itemsize)
     assert_array_equal(B, [['abc', sixu('Sigma \u03a3')],
                            ['long', '0123456789']])
Esempio n. 3
0
    def test_replace(self):
        R = self.A.replace(asbytes_nested(['3', 'a']),
                           asbytes_nested(['##########', '@']))
        assert_(issubclass(R.dtype.type, np.string_))
        assert_array_equal(
            R,
            asbytes_nested([[' abc ', ''], ['12##########45', 'MixedC@se'],
                            ['12########## \t ##########45 \x00', 'UPPER']]))

        if sys.version_info[0] < 3:
            # NOTE: b'abc'.replace(b'a', 'b') is not allowed on Py3
            R = self.A.replace(asbytes('a'), sixu('\u03a3'))
            assert_(issubclass(R.dtype.type, np.unicode_))
            assert_array_equal(R, [[sixu(' \u03a3bc '), ''],
                                   ['12345', sixu('MixedC\u03a3se')],
                                   ['123 \t 345 \x00', 'UPPER']])
Esempio n. 4
0
def test_pickle_python2_python3():
    # Test that loading object arrays saved on Python 2 works both on
    # Python 2 and Python 3 and vice versa
    data_dir = os.path.join(os.path.dirname(__file__), 'data')

    if sys.version_info[0] >= 3:
        xrange = range
    else:
        import __builtin__
        xrange = __builtin__.xrange

    expected = np.array([
        None, xrange,
        sixu('\u512a\u826f'),
        asbytes('\xe4\xb8\x8d\xe8\x89\xaf')
    ],
                        dtype=object)

    for fname in [
            'py2-objarr.npy', 'py2-objarr.npz', 'py3-objarr.npy',
            'py3-objarr.npz'
    ]:
        path = os.path.join(data_dir, fname)

        for encoding in ['bytes', 'latin1']:
            data_f = np.load(path, encoding=encoding)
            if fname.endswith('.npz'):
                data = data_f['x']
                data_f.close()
            else:
                data = data_f

            if sys.version_info[0] >= 3:
                if encoding == 'latin1' and fname.startswith('py2'):
                    assert_(isinstance(data[3], str))
                    assert_array_equal(data[:-1], expected[:-1])
                    # mojibake occurs
                    assert_array_equal(data[-1].encode(encoding), expected[-1])
                else:
                    assert_(isinstance(data[3], bytes))
                    assert_array_equal(data, expected)
            else:
                assert_array_equal(data, expected)

        if sys.version_info[0] >= 3:
            if fname.startswith('py2'):
                if fname.endswith('.npz'):
                    data = np.load(path)
                    assert_raises(UnicodeError, data.__getitem__, 'x')
                    data.close()
                    data = np.load(path, fix_imports=False, encoding='latin1')
                    assert_raises(ImportError, data.__getitem__, 'x')
                    data.close()
                else:
                    assert_raises(UnicodeError, np.load, path)
                    assert_raises(ImportError,
                                  np.load,
                                  path,
                                  encoding='latin1',
                                  fix_imports=False)
Esempio n. 5
0
def test_unicode_object_array():
    import sys
    if sys.version_info[0] >= 3:
        expected = "array(['é'], dtype=object)"
    else:
        expected = "array([u'\\xe9'], dtype=object)"
    x = np.array([sixu('\xe9')], dtype=object)
    assert_equal(repr(x), expected)
    def test_replace(self):
        R = self.A.replace(asbytes_nested(['3', 'a']),
                           asbytes_nested(['##########', '@']))
        assert_(issubclass(R.dtype.type, np.string_))
        assert_array_equal(R, asbytes_nested([
                [' abc ', ''],
                ['12##########45', 'MixedC@se'],
                ['12########## \t ##########45 \x00', 'UPPER']]))

        if sys.version_info[0] < 3:
            # NOTE: b'abc'.replace(b'a', 'b') is not allowed on Py3
            R = self.A.replace(asbytes('a'), sixu('\u03a3'))
            assert_(issubclass(R.dtype.type, np.unicode_))
            assert_array_equal(R, [
                    [sixu(' \u03a3bc '), ''],
                    ['12345', sixu('MixedC\u03a3se')],
                    ['123 \t 345 \x00', 'UPPER']])
Esempio n. 7
0
def test_unicode_object_array():
    import sys
    if sys.version_info[0] >= 3:
        expected = "array(['é'], dtype=object)"
    else:
        expected = "array([u'\\xe9'], dtype=object)"
    x = np.array([sixu('\xe9')], dtype=object)
    assert_equal(repr(x), expected)
Esempio n. 8
0
 def test_capitalize(self):
     assert_(issubclass(self.A.capitalize().dtype.type, np.string_))
     assert_array_equal(
         self.A.capitalize(),
         asbytes_nested([[' abc ', ''], ['12345', 'Mixedcase'],
                         ['123 \t 345 \0 ', 'Upper']]))
     assert_(issubclass(self.B.capitalize().dtype.type, np.unicode_))
     assert_array_equal(self.B.capitalize(),
                        [[sixu(' \u03c3 '), ''], ['12345', 'Mixedcase'],
                         ['123 \t 345 \0 ', 'Upper']])
 def test_capitalize(self):
     assert_(issubclass(self.A.capitalize().dtype.type, np.string_))
     assert_array_equal(self.A.capitalize(), asbytes_nested([
             [' abc ', ''],
             ['12345', 'Mixedcase'],
             ['123 \t 345 \0 ', 'Upper']]))
     assert_(issubclass(self.B.capitalize().dtype.type, np.unicode_))
     assert_array_equal(self.B.capitalize(), [
             [sixu(' \u03c3 '), ''],
             ['12345', 'Mixedcase'],
             ['123 \t 345 \0 ', 'Upper']])
 def setUp(self):
     self.A = np.array([[' abc ', ''],
                        ['12345', 'MixedCase'],
                        ['123 \t 345 \0 ', 'UPPER']]).view(np.chararray)
     self.B = np.array([[sixu(' \u03a3 '), sixu('')],
                        [sixu('12345'), sixu('MixedCase')],
                        [sixu('123 \t 345 \0 '), sixu('UPPER')]]).view(np.chararray)
Esempio n. 11
0
 def setUp(self):
     self.A = np.array([[' abc ', ''], ['12345', 'MixedCase'],
                        ['123 \t 345 \0 ', 'UPPER']]).view(np.chararray)
     self.B = np.array([[sixu(' \u03a3 '), sixu('')],
                        [sixu('12345'), sixu('MixedCase')],
                        [sixu('123 \t 345 \0 '),
                         sixu('UPPER')]]).view(np.chararray)
Esempio n. 12
0
def test_pickle_python2_python3():
    # Test that loading object arrays saved on Python 2 works both on
    # Python 2 and Python 3 and vice versa
    data_dir = os.path.join(os.path.dirname(__file__), 'data')

    if sys.version_info[0] >= 3:
        xrange = range
    else:
        import __builtin__
        xrange = __builtin__.xrange

    expected = np.array([None, xrange, sixu('\u512a\u826f'),
                         asbytes('\xe4\xb8\x8d\xe8\x89\xaf')],
                        dtype=object)

    for fname in ['py2-objarr.npy', 'py2-objarr.npz',
                  'py3-objarr.npy', 'py3-objarr.npz']:
        path = os.path.join(data_dir, fname)

        for encoding in ['bytes', 'latin1']:
            data_f = np.load(path, encoding=encoding)
            if fname.endswith('.npz'):
                data = data_f['x']
                data_f.close()
            else:
                data = data_f

            if sys.version_info[0] >= 3:
                if encoding == 'latin1' and fname.startswith('py2'):
                    assert_(isinstance(data[3], str))
                    assert_array_equal(data[:-1], expected[:-1])
                    # mojibake occurs
                    assert_array_equal(data[-1].encode(encoding), expected[-1])
                else:
                    assert_(isinstance(data[3], bytes))
                    assert_array_equal(data, expected)
            else:
                assert_array_equal(data, expected)

        if sys.version_info[0] >= 3:
            if fname.startswith('py2'):
                if fname.endswith('.npz'):
                    data = np.load(path)
                    assert_raises(UnicodeError, data.__getitem__, 'x')
                    data.close()
                    data = np.load(path, fix_imports=False, encoding='latin1')
                    assert_raises(ImportError, data.__getitem__, 'x')
                    data.close()
                else:
                    assert_raises(UnicodeError, np.load, path)
                    assert_raises(ImportError, np.load, path,
                                  encoding='latin1', fix_imports=False)
Esempio n. 13
0
    def test_lstrip(self):
        tgt = asbytes_nested([['abc ', ''], ['12345', 'MixedCase'],
                              ['123 \t 345 \0 ', 'UPPER']])
        assert_(issubclass(self.A.lstrip().dtype.type, np.string_))
        assert_array_equal(self.A.lstrip(), tgt)

        tgt = asbytes_nested([[' abc', ''], ['2345', 'ixedCase'],
                              ['23 \t 345 \x00', 'UPPER']])
        assert_array_equal(self.A.lstrip(asbytes_nested(['1', 'M'])), tgt)

        tgt = [[sixu('\u03a3 '), ''], ['12345', 'MixedCase'],
               ['123 \t 345 \0 ', 'UPPER']]
        assert_(issubclass(self.B.lstrip().dtype.type, np.unicode_))
        assert_array_equal(self.B.lstrip(), tgt)
Esempio n. 14
0
 def test_strip(self):
     assert_(issubclass(self.A.strip().dtype.type, np.string_))
     assert_array_equal(
         self.A.strip(),
         asbytes_nested([['abc', ''], ['12345', 'MixedCase'],
                         ['123 \t 345', 'UPPER']]))
     assert_array_equal(
         self.A.strip(asbytes_nested(['15', 'EReM'])),
         asbytes_nested([[' abc ', ''], ['234', 'ixedCas'],
                         ['23 \t 345 \x00', 'UPP']]))
     assert_(issubclass(self.B.strip().dtype.type, np.unicode_))
     assert_array_equal(self.B.strip(),
                        [[sixu('\u03a3'), ''], ['12345', 'MixedCase'],
                         ['123 \t 345', 'UPPER']])
Esempio n. 15
0
    def content_check(self, ua, ua_scalar, nbytes):

        # Check the length of the unicode base type
        self.assertTrue(int(ua.dtype.str[2:]) == self.ulen)
        # Check the length of the data buffer
        self.assertTrue(buffer_length(ua) == nbytes)
        # Small check that data in array element is ok
        self.assertTrue(ua_scalar == sixu(''))
        # Encode to ascii and double check
        self.assertTrue(ua_scalar.encode('ascii') == asbytes(''))
        # Check buffer lengths for scalars
        if ucs4:
            self.assertTrue(buffer_length(ua_scalar) == 0)
        else:
            self.assertTrue(buffer_length(ua_scalar) == 0)
Esempio n. 16
0
 def test_from_unicode_array(self):
     A = np.array([['abc', sixu('Sigma \u03a3')],
                   ['long   ', '0123456789']])
     assert_equal(A.dtype.type, np.unicode_)
     B = np.char.array(A)
     assert_array_equal(B, A)
     assert_equal(B.dtype, A.dtype)
     assert_equal(B.shape, A.shape)
     B = np.char.array(A, **kw_unicode_true)
     assert_array_equal(B, A)
     assert_equal(B.dtype, A.dtype)
     assert_equal(B.shape, A.shape)
     def fail():
         B = np.char.array(A, **kw_unicode_false)
     self.assertRaises(UnicodeEncodeError, fail)
Esempio n. 17
0
    def content_check(self, ua, ua_scalar, nbytes):

        # Check the length of the unicode base type
        self.assertTrue(int(ua.dtype.str[2:]) == self.ulen)
        # Check the length of the data buffer
        self.assertTrue(buffer_length(ua) == nbytes)
        # Small check that data in array element is ok
        self.assertTrue(ua_scalar == sixu(''))
        # Encode to ascii and double check
        self.assertTrue(ua_scalar.encode('ascii') == asbytes(''))
        # Check buffer lengths for scalars
        if ucs4:
            self.assertTrue(buffer_length(ua_scalar) == 0)
        else:
            self.assertTrue(buffer_length(ua_scalar) == 0)
 def test_from_unicode_array(self):
     A = np.array([['abc', sixu('Sigma \u03a3')],
                   ['long   ', '0123456789']])
     assert_equal(A.dtype.type, np.unicode_)
     B = np.char.array(A)
     assert_array_equal(B, A)
     assert_equal(B.dtype, A.dtype)
     assert_equal(B.shape, A.shape)
     B = np.char.array(A, **kw_unicode_true)
     assert_array_equal(B, A)
     assert_equal(B.dtype, A.dtype)
     assert_equal(B.shape, A.shape)
     def fail():
         B = np.char.array(A, **kw_unicode_false)
     self.assertRaises(UnicodeEncodeError, fail)
 def test_strip(self):
     assert_(issubclass(self.A.strip().dtype.type, np.string_))
     assert_array_equal(self.A.strip(), asbytes_nested([
             ['abc', ''],
             ['12345', 'MixedCase'],
             ['123 \t 345', 'UPPER']]))
     assert_array_equal(self.A.strip(asbytes_nested(['15', 'EReM'])),
                        asbytes_nested([
             [' abc ', ''],
             ['234', 'ixedCas'],
             ['23 \t 345 \x00', 'UPP']]))
     assert_(issubclass(self.B.strip().dtype.type, np.unicode_))
     assert_array_equal(self.B.strip(), [
             [sixu('\u03a3'), ''],
             ['12345', 'MixedCase'],
             ['123 \t 345', 'UPPER']])
Esempio n. 20
0
    def test_title(self):
        tgt = asbytes_nested([[' Abc ', ''], ['12345', 'Mixedcase'],
                              ['123 \t 345 \0 ', 'Upper']])
        assert_(issubclass(self.A.title().dtype.type, np.string_))
        assert_array_equal(self.A.title(), tgt)

        tgt = [[sixu(' \u03a3 '), sixu('')],
               [sixu('12345'), sixu('Mixedcase')],
               [sixu('123 \t 345 \0 '), sixu('Upper')]]
        assert_(issubclass(self.B.title().dtype.type, np.unicode_))
        assert_array_equal(self.B.title(), tgt)
Esempio n. 21
0
    def test_lower(self):
        tgt = asbytes_nested([[' abc ', ''], ['12345', 'mixedcase'],
                              ['123 \t 345 \0 ', 'upper']])
        assert_(issubclass(self.A.lower().dtype.type, np.string_))
        assert_array_equal(self.A.lower(), tgt)

        tgt = [[sixu(' \u03c3 '), sixu('')],
               [sixu('12345'), sixu('mixedcase')],
               [sixu('123 \t 345 \0 '), sixu('upper')]]
        assert_(issubclass(self.B.lower().dtype.type, np.unicode_))
        assert_array_equal(self.B.lower(), tgt)
Esempio n. 22
0
    def test_upper(self):
        tgt = asbytes_nested([[' ABC ', ''], ['12345', 'MIXEDCASE'],
                              ['123 \t 345 \0 ', 'UPPER']])
        assert_(issubclass(self.A.upper().dtype.type, np.string_))
        assert_array_equal(self.A.upper(), tgt)

        tgt = [[sixu(' \u03a3 '), sixu('')],
               [sixu('12345'), sixu('MIXEDCASE')],
               [sixu('123 \t 345 \0 '), sixu('UPPER')]]
        assert_(issubclass(self.B.upper().dtype.type, np.unicode_))
        assert_array_equal(self.B.upper(), tgt)
 def test_swapcase(self):
     assert_(issubclass(self.A.swapcase().dtype.type, np.string_))
     assert_array_equal(self.A.swapcase(), asbytes_nested([
             [' ABC ', ''],
             ['12345', 'mIXEDcASE'],
             ['123 \t 345 \0 ', 'upper']]))
     assert_(issubclass(self.B.swapcase().dtype.type, np.unicode_))
     assert_array_equal(self.B.swapcase(), [
             [sixu(' \u03c3 '), sixu('')],
             [sixu('12345'), sixu('mIXEDcASE')],
             [sixu('123 \t 345 \0 '), sixu('upper')]])
 def test_title(self):
     assert_(issubclass(self.A.title().dtype.type, np.string_))
     assert_array_equal(self.A.title(), asbytes_nested([
             [' Abc ', ''],
             ['12345', 'Mixedcase'],
             ['123 \t 345 \0 ', 'Upper']]))
     assert_(issubclass(self.B.title().dtype.type, np.unicode_))
     assert_array_equal(self.B.title(), [
             [sixu(' \u03a3 '), sixu('')],
             [sixu('12345'), sixu('Mixedcase')],
             [sixu('123 \t 345 \0 '), sixu('Upper')]])
 def test_upper(self):
     assert_(issubclass(self.A.upper().dtype.type, np.string_))
     assert_array_equal(self.A.upper(), asbytes_nested([
             [' ABC ', ''],
             ['12345', 'MIXEDCASE'],
             ['123 \t 345 \0 ', 'UPPER']]))
     assert_(issubclass(self.B.upper().dtype.type, np.unicode_))
     assert_array_equal(self.B.upper(), [
             [sixu(' \u03a3 '), sixu('')],
             [sixu('12345'), sixu('MIXEDCASE')],
             [sixu('123 \t 345 \0 '), sixu('UPPER')]])
 def test_lower(self):
     assert_(issubclass(self.A.lower().dtype.type, np.string_))
     assert_array_equal(self.A.lower(), asbytes_nested([
             [' abc ', ''],
             ['12345', 'mixedcase'],
             ['123 \t 345 \0 ', 'upper']]))
     assert_(issubclass(self.B.lower().dtype.type, np.unicode_))
     assert_array_equal(self.B.lower(), [
             [sixu(' \u03c3 '), sixu('')],
             [sixu('12345'), sixu('mixedcase')],
             [sixu('123 \t 345 \0 '), sixu('upper')]])
Esempio n. 27
0
    def test_swapcase(self):
        tgt = asbytes_nested([[' ABC ', ''], ['12345', 'mIXEDcASE'],
                              ['123 \t 345 \0 ', 'upper']])
        assert_(issubclass(self.A.swapcase().dtype.type, np.string_))
        assert_array_equal(self.A.swapcase(), tgt)

        tgt = [[sixu(' \u03c3 '), sixu('')],
               [sixu('12345'), sixu('mIXEDcASE')],
               [sixu('123 \t 345 \0 '), sixu('upper')]]
        assert_(issubclass(self.B.swapcase().dtype.type, np.unicode_))
        assert_array_equal(self.B.swapcase(), tgt)
Esempio n. 28
0
    def test_lstrip(self):
        tgt = asbytes_nested([['abc ', ''],
                              ['12345', 'MixedCase'],
                              ['123 \t 345 \0 ', 'UPPER']])
        assert_(issubclass(self.A.lstrip().dtype.type, np.string_))
        assert_array_equal(self.A.lstrip(), tgt)

        tgt = asbytes_nested([[' abc', ''],
                              ['2345', 'ixedCase'],
                              ['23 \t 345 \x00', 'UPPER']])
        assert_array_equal(self.A.lstrip(asbytes_nested(['1', 'M'])), tgt)

        tgt = [[sixu('\u03a3 '), ''],
               ['12345', 'MixedCase'],
               ['123 \t 345 \0 ', 'UPPER']]
        assert_(issubclass(self.B.lstrip().dtype.type, np.unicode_))
        assert_array_equal(self.B.lstrip(), tgt)
 def test_from_unicode(self):
     A = np.char.array(sixu('\u03a3'))
     assert_equal(len(A), 1)
     assert_equal(len(A[0]), 1)
     assert_equal(A.itemsize, 4)
     assert_(issubclass(A.dtype.type, np.unicode_))
Esempio n. 30
0
def test_array_astype():
    a = np.arange(6, dtype='f4').reshape(2, 3)
    # Default behavior: allows unsafe casts, keeps memory layout,
    #                   always copies.
    b = a.astype('i4')
    assert_equal(a, b)
    assert_equal(b.dtype, np.dtype('i4'))
    assert_equal(a.strides, b.strides)
    b = a.T.astype('i4')
    assert_equal(a.T, b)
    assert_equal(b.dtype, np.dtype('i4'))
    assert_equal(a.T.strides, b.strides)
    b = a.astype('f4')
    assert_equal(a, b)
    assert_(not (a is b))

    # copy=False parameter can sometimes skip a copy
    b = a.astype('f4', copy=False)
    assert_(a is b)

    # order parameter allows overriding of the memory layout,
    # forcing a copy if the layout is wrong
    b = a.astype('f4', order='F', copy=False)
    assert_equal(a, b)
    assert_(not (a is b))
    assert_(b.flags.f_contiguous)

    b = a.astype('f4', order='C', copy=False)
    assert_equal(a, b)
    assert_(a is b)
    assert_(b.flags.c_contiguous)

    # casting parameter allows catching bad casts
    b = a.astype('c8', casting='safe')
    assert_equal(a, b)
    assert_equal(b.dtype, np.dtype('c8'))

    assert_raises(TypeError, a.astype, 'i4', casting='safe')

    # subok=False passes through a non-subclassed array
    b = a.astype('f4', subok=0, copy=False)
    assert_(a is b)

    a = np.matrix([[0, 1, 2], [3, 4, 5]], dtype='f4')

    # subok=True passes through a matrix
    b = a.astype('f4', subok=True, copy=False)
    assert_(a is b)

    # subok=True is default, and creates a subtype on a cast
    b = a.astype('i4', copy=False)
    assert_equal(a, b)
    assert_equal(type(b), np.matrix)

    # subok=False never returns a matrix
    b = a.astype('f4', subok=False, copy=False)
    assert_equal(a, b)
    assert_(not (a is b))
    assert_(type(b) is not np.matrix)

    # Make sure converting from string object to fixed length string
    # does not truncate.
    a = np.array([b'a' * 100], dtype='O')
    b = a.astype('S')
    assert_equal(a, b)
    assert_equal(b.dtype, np.dtype('S100'))
    a = np.array([sixu('a') * 100], dtype='O')
    b = a.astype('U')
    assert_equal(a, b)
    assert_equal(b.dtype, np.dtype('U100'))

    # Same test as above but for strings shorter than 64 characters
    a = np.array([b'a' * 10], dtype='O')
    b = a.astype('S')
    assert_equal(a, b)
    assert_equal(b.dtype, np.dtype('S10'))
    a = np.array([sixu('a') * 10], dtype='O')
    b = a.astype('U')
    assert_equal(a, b)
    assert_equal(b.dtype, np.dtype('U10'))
Esempio n. 31
0
def test_array_astype():
    a = np.arange(6, dtype="f4").reshape(2, 3)
    # Default behavior: allows unsafe casts, keeps memory layout,
    #                   always copies.
    b = a.astype("i4")
    assert_equal(a, b)
    assert_equal(b.dtype, np.dtype("i4"))
    assert_equal(a.strides, b.strides)
    b = a.T.astype("i4")
    assert_equal(a.T, b)
    assert_equal(b.dtype, np.dtype("i4"))
    assert_equal(a.T.strides, b.strides)
    b = a.astype("f4")
    assert_equal(a, b)
    assert_(not (a is b))

    # copy=False parameter can sometimes skip a copy
    b = a.astype("f4", copy=False)
    assert_(a is b)

    # order parameter allows overriding of the memory layout,
    # forcing a copy if the layout is wrong
    b = a.astype("f4", order="F", copy=False)
    assert_equal(a, b)
    assert_(not (a is b))
    assert_(b.flags.f_contiguous)

    b = a.astype("f4", order="C", copy=False)
    assert_equal(a, b)
    assert_(a is b)
    assert_(b.flags.c_contiguous)

    # casting parameter allows catching bad casts
    b = a.astype("c8", casting="safe")
    assert_equal(a, b)
    assert_equal(b.dtype, np.dtype("c8"))

    assert_raises(TypeError, a.astype, "i4", casting="safe")

    # subok=False passes through a non-subclassed array
    b = a.astype("f4", subok=0, copy=False)
    assert_(a is b)

    a = np.matrix([[0, 1, 2], [3, 4, 5]], dtype="f4")

    # subok=True passes through a matrix
    b = a.astype("f4", subok=True, copy=False)
    assert_(a is b)

    # subok=True is default, and creates a subtype on a cast
    b = a.astype("i4", copy=False)
    assert_equal(a, b)
    assert_equal(type(b), np.matrix)

    # subok=False never returns a matrix
    b = a.astype("f4", subok=False, copy=False)
    assert_equal(a, b)
    assert_(not (a is b))
    assert_(type(b) is not np.matrix)

    # Make sure converting from string object to fixed length string
    # does not truncate.
    a = np.array([b"a" * 100], dtype="O")
    b = a.astype("S")
    assert_equal(a, b)
    assert_equal(b.dtype, np.dtype("S100"))
    a = np.array([sixu("a") * 100], dtype="O")
    b = a.astype("U")
    assert_equal(a, b)
    assert_equal(b.dtype, np.dtype("U100"))

    # Same test as above but for strings shorter than 64 characters
    a = np.array([b"a" * 10], dtype="O")
    b = a.astype("S")
    assert_equal(a, b)
    assert_equal(b.dtype, np.dtype("S10"))
    a = np.array([sixu("a") * 10], dtype="O")
    b = a.astype("U")
    assert_equal(a, b)
    assert_equal(b.dtype, np.dtype("U10"))

    a = np.array(123456789012345678901234567890, dtype="O").astype("S")
    assert_array_equal(a, np.array(b"1234567890" * 3, dtype="S30"))
    a = np.array(123456789012345678901234567890, dtype="O").astype("U")
    assert_array_equal(a, np.array(sixu("1234567890" * 3), dtype="U30"))

    a = np.array([123456789012345678901234567890], dtype="O").astype("S")
    assert_array_equal(a, np.array(b"1234567890" * 3, dtype="S30"))
    a = np.array([123456789012345678901234567890], dtype="O").astype("U")
    assert_array_equal(a, np.array(sixu("1234567890" * 3), dtype="U30"))

    a = np.array(123456789012345678901234567890, dtype="S")
    assert_array_equal(a, np.array(b"1234567890" * 3, dtype="S30"))
    a = np.array(123456789012345678901234567890, dtype="U")
    assert_array_equal(a, np.array(sixu("1234567890" * 3), dtype="U30"))

    a = np.array(sixu("a\u0140"), dtype="U")
    b = np.ndarray(buffer=a, dtype="uint32", shape=2)
    assert_(b.size == 2)

    a = np.array([1000], dtype="i4")
    assert_raises(TypeError, a.astype, "S1", casting="safe")

    a = np.array(1000, dtype="i4")
    assert_raises(TypeError, a.astype, "U1", casting="safe")
 def test_unicode_upconvert(self):
     A = np.char.array(['abc'])
     B = np.char.array([sixu('\u03a3')])
     assert_(issubclass((A + B).dtype.type, np.unicode_))
Esempio n. 33
0
            return np.prod(v.shape) * v.itemsize
elif sys.version_info[0] >= 3:
    import array as _array

    ucs4 = (_array.array('u').itemsize == 4)

    def buffer_length(arr):
        if isinstance(arr, unicode):
            return _array.array('u').itemsize * len(arr)
        v = memoryview(arr)
        if v.shape is None:
            return len(v) * v.itemsize
        else:
            return np.prod(v.shape) * v.itemsize
else:
    if len(buffer(sixu('u'))) == 4:
        ucs4 = True
    else:
        ucs4 = False

    def buffer_length(arr):
        if isinstance(arr, np.ndarray):
            return len(arr.data)
        return len(buffer(arr))

# In both cases below we need to make sure that the byte swapped value (as
# UCS4) is still a valid unicode:
# Value that can be represented in UCS2 interpreters
ucs2_value = sixu('\u0900')
# Value that cannot be represented in UCS2 interpreters (but can in UCS4)
ucs4_value = sixu('\U00100900')
Esempio n. 34
0
def test_array_astype():
    a = np.arange(6, dtype='f4').reshape(2, 3)
    # Default behavior: allows unsafe casts, keeps memory layout,
    #                   always copies.
    b = a.astype('i4')
    assert_equal(a, b)
    assert_equal(b.dtype, np.dtype('i4'))
    assert_equal(a.strides, b.strides)
    b = a.T.astype('i4')
    assert_equal(a.T, b)
    assert_equal(b.dtype, np.dtype('i4'))
    assert_equal(a.T.strides, b.strides)
    b = a.astype('f4')
    assert_equal(a, b)
    assert_(not (a is b))

    # copy=False parameter can sometimes skip a copy
    b = a.astype('f4', copy=False)
    assert_(a is b)

    # order parameter allows overriding of the memory layout,
    # forcing a copy if the layout is wrong
    b = a.astype('f4', order='F', copy=False)
    assert_equal(a, b)
    assert_(not (a is b))
    assert_(b.flags.f_contiguous)

    b = a.astype('f4', order='C', copy=False)
    assert_equal(a, b)
    assert_(a is b)
    assert_(b.flags.c_contiguous)

    # casting parameter allows catching bad casts
    b = a.astype('c8', casting='safe')
    assert_equal(a, b)
    assert_equal(b.dtype, np.dtype('c8'))

    assert_raises(TypeError, a.astype, 'i4', casting='safe')

    # subok=False passes through a non-subclassed array
    b = a.astype('f4', subok=0, copy=False)
    assert_(a is b)

    a = np.matrix([[0, 1, 2], [3, 4, 5]], dtype='f4')

    # subok=True passes through a matrix
    b = a.astype('f4', subok=True, copy=False)
    assert_(a is b)

    # subok=True is default, and creates a subtype on a cast
    b = a.astype('i4', copy=False)
    assert_equal(a, b)
    assert_equal(type(b), np.matrix)

    # subok=False never returns a matrix
    b = a.astype('f4', subok=False, copy=False)
    assert_equal(a, b)
    assert_(not (a is b))
    assert_(type(b) is not np.matrix)

    # Make sure converting from string object to fixed length string
    # does not truncate.
    a = np.array([b'a'*100], dtype='O')
    b = a.astype('S')
    assert_equal(a, b)
    assert_equal(b.dtype, np.dtype('S100'))
    a = np.array([sixu('a')*100], dtype='O')
    b = a.astype('U')
    assert_equal(a, b)
    assert_equal(b.dtype, np.dtype('U100'))

    # Same test as above but for strings shorter than 64 characters
    a = np.array([b'a'*10], dtype='O')
    b = a.astype('S')
    assert_equal(a, b)
    assert_equal(b.dtype, np.dtype('S10'))
    a = np.array([sixu('a')*10], dtype='O')
    b = a.astype('U')
    assert_equal(a, b)
    assert_equal(b.dtype, np.dtype('U10'))

    a = np.array(123456789012345678901234567890, dtype='O').astype('S')
    assert_array_equal(a, np.array(b'1234567890' * 3, dtype='S30'))
    a = np.array(123456789012345678901234567890, dtype='O').astype('U')
    assert_array_equal(a, np.array(sixu('1234567890' * 3), dtype='U30'))

    a = np.array([123456789012345678901234567890], dtype='O').astype('S')
    assert_array_equal(a, np.array(b'1234567890' * 3, dtype='S30'))
    a = np.array([123456789012345678901234567890], dtype='O').astype('U')
    assert_array_equal(a, np.array(sixu('1234567890' * 3), dtype='U30'))

    a = np.array(123456789012345678901234567890, dtype='S')
    assert_array_equal(a, np.array(b'1234567890' * 3, dtype='S30'))
    a = np.array(123456789012345678901234567890, dtype='U')
    assert_array_equal(a, np.array(sixu('1234567890' * 3), dtype='U30'))

    a = np.array(sixu('a\u0140'), dtype='U')
    b = np.ndarray(buffer=a, dtype='uint32', shape=2)
    assert_(b.size == 2)
Esempio n. 35
0
            return np.prod(v.shape) * v.itemsize
elif sys.version_info[0] >= 3:
    import array as _array

    ucs4 = (_array.array('u').itemsize == 4)

    def buffer_length(arr):
        if isinstance(arr, unicode):
            return _array.array('u').itemsize * len(arr)
        v = memoryview(arr)
        if v.shape is None:
            return len(v) * v.itemsize
        else:
            return np.prod(v.shape) * v.itemsize
else:
    if len(buffer(sixu('u'))) == 4:
        ucs4 = True
    else:
        ucs4 = False

    def buffer_length(arr):
        if isinstance(arr, np.ndarray):
            return len(arr.data)
        return len(buffer(arr))


# In both cases below we need to make sure that the byte swapped value (as
# UCS4) is still a valid unicode:
# Value that can be represented in UCS2 interpreters
ucs2_value = sixu('\u0900')
# Value that cannot be represented in UCS2 interpreters (but can in UCS4)
Esempio n. 36
0
def test_unicode():
    np.longdouble(sixu("1.2"))
Esempio n. 37
0
def test_unicode():
    np.longdouble(sixu("1.2"))
Esempio n. 38
0
from __future__ import division, absolute_import, print_function

import sys

import numpy as np
from numpy.compat import asbytes, unicode, sixu
from numpy.testing import TestCase, run_module_suite, assert_equal

array(sixu(''), dtype='unicode')

# Guess the UCS length for this python interpreter
if sys.version_info[:2] >= (3, 3):
    # Python 3.3 uses a flexible string representation
    ucs4 = False

    def buffer_length(arr):
        if isinstance(arr, unicode):
            arr = str(arr)
            return (sys.getsizeof(arr + "a") - sys.getsizeof(arr)) * len(arr)
        v = memoryview(arr)
        if v.shape is None:
            return len(v) * v.itemsize
        else:
            return np.prod(v.shape) * v.itemsize
elif sys.version_info[0] >= 3:
    import array as _array

    ucs4 = (_array.array('u').itemsize == 4)

    def buffer_length(arr):
        if isinstance(arr, unicode):
Esempio n. 39
0
 def test_unicode_upconvert(self):
     A = np.char.array(['abc'])
     B = np.char.array([sixu('\u03a3')])
     assert_(issubclass((A + B).dtype.type, np.unicode_))
Esempio n. 40
0
from __future__ import division, absolute_import, print_function

import sys

import numpy as np
from numpy.compat import asbytes, unicode, sixu
from numpy.testing import TestCase, run_module_suite, assert_equal

array(sixu(''), dtype='unicode')

# Guess the UCS length for this python interpreter
if sys.version_info[:2] >= (3, 3):
    # Python 3.3 uses a flexible string representation
    ucs4 = False

    def buffer_length(arr):
        if isinstance(arr, unicode):
            arr = str(arr)
            return (sys.getsizeof(arr+"a") - sys.getsizeof(arr)) * len(arr)
        v = memoryview(arr)
        if v.shape is None:
            return len(v) * v.itemsize
        else:
            return np.prod(v.shape) * v.itemsize
elif sys.version_info[0] >= 3:
    import array as _array

    ucs4 = (_array.array('u').itemsize == 4)

    def buffer_length(arr):
        if isinstance(arr, unicode):
Esempio n. 41
0
 def test_masked_array_repr_unicode(self):
     """Ticket #1256"""
     repr(np.ma.array(sixu("Unicode")))
Esempio n. 42
0
 def test_from_unicode(self):
     A = np.char.array(sixu('\u03a3'))
     assert_equal(len(A), 1)
     assert_equal(len(A[0]), 1)
     assert_equal(A.itemsize, 4)
     assert_(issubclass(A.dtype.type, np.unicode_))
Esempio n. 43
0
 def test_masked_array_repr_unicode(self):
     """Ticket #1256"""
     repr(np.ma.array(sixu("Unicode")))