Python numpy.object_() Examples

The following are 30 code examples for showing how to use numpy.object_(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module numpy , or try the search function .

Example 1
Project: recruit   Author: Frank-qlu   File: test_regression.py    License: Apache License 2.0 6 votes vote down vote up
def test_object_array_refcount_self_assign(self):
        # Ticket #711
        class VictimObject(object):
            deleted = False

            def __del__(self):
                self.deleted = True

        d = VictimObject()
        arr = np.zeros(5, dtype=np.object_)
        arr[:] = d
        del d
        arr[:] = arr  # refcount of 'd' might hit zero here
        assert_(not arr[0].deleted)
        arr[:] = arr  # trying to induce a segfault by doing it again...
        assert_(not arr[0].deleted) 
Example 2
Project: recruit   Author: Frank-qlu   File: test_base.py    License: Apache License 2.0 6 votes vote down vote up
def test_constructor_from_index_dtlike(self, cast_as_obj, index):
        if cast_as_obj:
            result = pd.Index(index.astype(object))
        else:
            result = pd.Index(index)

        tm.assert_index_equal(result, index)

        if isinstance(index, pd.DatetimeIndex):
            assert result.tz == index.tz
            if cast_as_obj:
                # GH#23524 check that Index(dti, dtype=object) does not
                #  incorrectly raise ValueError, and that nanoseconds are not
                #  dropped
                index += pd.Timedelta(nanoseconds=50)
                result = pd.Index(index, dtype=object)
                assert result.dtype == np.object_
                assert list(result) == list(index) 
Example 3
Project: recruit   Author: Frank-qlu   File: test_dtypes.py    License: Apache License 2.0 6 votes vote down vote up
def test_astype_datetime(self):
        s = Series(iNaT, dtype='M8[ns]', index=lrange(5))

        s = s.astype('O')
        assert s.dtype == np.object_

        s = Series([datetime(2001, 1, 2, 0, 0)])

        s = s.astype('O')
        assert s.dtype == np.object_

        s = Series([datetime(2001, 1, 2, 0, 0) for i in range(3)])

        s[1] = np.nan
        assert s.dtype == 'M8[ns]'

        s = s.astype('O')
        assert s.dtype == np.object_ 
Example 4
Project: recruit   Author: Frank-qlu   File: test_constructors.py    License: Apache License 2.0 6 votes vote down vote up
def test_fromDict(self):
        data = {'a': 0, 'b': 1, 'c': 2, 'd': 3}

        series = Series(data)
        assert tm.is_sorted(series.index)

        data = {'a': 0, 'b': '1', 'c': '2', 'd': datetime.now()}
        series = Series(data)
        assert series.dtype == np.object_

        data = {'a': 0, 'b': '1', 'c': '2', 'd': '3'}
        series = Series(data)
        assert series.dtype == np.object_

        data = {'a': '0', 'b': '1'}
        series = Series(data, dtype=float)
        assert series.dtype == np.float64 
Example 5
Project: recruit   Author: Frank-qlu   File: test_constructors.py    License: Apache License 2.0 6 votes vote down vote up
def test_fromValue(self, datetime_series):

        nans = Series(np.NaN, index=datetime_series.index)
        assert nans.dtype == np.float_
        assert len(nans) == len(datetime_series)

        strings = Series('foo', index=datetime_series.index)
        assert strings.dtype == np.object_
        assert len(strings) == len(datetime_series)

        d = datetime.now()
        dates = Series(d, index=datetime_series.index)
        assert dates.dtype == 'M8[ns]'
        assert len(dates) == len(datetime_series)

        # GH12336
        # Test construction of categorical series from value
        categorical = Series(0, index=datetime_series.index, dtype="category")
        expected = Series(0, index=datetime_series.index).astype("category")
        assert categorical.dtype == 'category'
        assert len(categorical) == len(datetime_series)
        tm.assert_series_equal(categorical, expected) 
Example 6
Project: recruit   Author: Frank-qlu   File: test_strings.py    License: Apache License 2.0 6 votes vote down vote up
def test_zfill(self):
        values = Series(['1', '22', 'aaa', '333', '45678'])

        result = values.str.zfill(5)
        expected = Series(['00001', '00022', '00aaa', '00333', '45678'])
        tm.assert_series_equal(result, expected)
        expected = np.array([v.zfill(5) for v in values.values],
                            dtype=np.object_)
        tm.assert_numpy_array_equal(result.values, expected)

        result = values.str.zfill(3)
        expected = Series(['001', '022', 'aaa', '333', '45678'])
        tm.assert_series_equal(result, expected)
        expected = np.array([v.zfill(3) for v in values.values],
                            dtype=np.object_)
        tm.assert_numpy_array_equal(result.values, expected)

        values = Series(['1', np.nan, 'aaa', np.nan, '45678'])
        result = values.str.zfill(5)
        expected = Series(['00001', np.nan, '00aaa', np.nan, '45678'])
        tm.assert_series_equal(result, expected) 
Example 7
Project: recruit   Author: Frank-qlu   File: test_groupby.py    License: Apache License 2.0 6 votes vote down vote up
def test_convert_objects_leave_decimal_alone():

    s = Series(lrange(5))
    labels = np.array(['a', 'b', 'c', 'd', 'e'], dtype='O')

    def convert_fast(x):
        return Decimal(str(x.mean()))

    def convert_force_pure(x):
        # base will be length 0
        assert (len(x.values.base) > 0)
        return Decimal(str(x.mean()))

    grouped = s.groupby(labels)

    result = grouped.agg(convert_fast)
    assert result.dtype == np.object_
    assert isinstance(result[0], Decimal)

    result = grouped.agg(convert_force_pure)
    assert result.dtype == np.object_
    assert isinstance(result[0], Decimal) 
Example 8
Project: recruit   Author: Frank-qlu   File: test_analytics.py    License: Apache License 2.0 6 votes vote down vote up
def test_stat_operators_attempt_obj_array(self, method):
        # GH 676
        data = {
            'a': [-0.00049987540199591344, -0.0016467257772919831,
                  0.00067695870775883013],
            'b': [-0, -0, 0.0],
            'c': [0.00031111847529610595, 0.0014902627951905339,
                  -0.00094099200035979691]
        }
        df1 = DataFrame(data, index=['foo', 'bar', 'baz'], dtype='O')

        df2 = DataFrame({0: [np.nan, 2], 1: [np.nan, 3],
                         2: [np.nan, 4]}, dtype=object)

        for df in [df1, df2]:
            assert df.values.dtype == np.object_
            result = getattr(df, method)(1)
            expected = getattr(df.astype('f8'), method)(1)

            if method in ['sum', 'prod']:
                tm.assert_series_equal(result, expected) 
Example 9
Project: recruit   Author: Frank-qlu   File: test_constructors.py    License: Apache License 2.0 6 votes vote down vote up
def test_constructor_dict_cast(self):
        # cast float tests
        test_data = {
            'A': {'1': 1, '2': 2},
            'B': {'1': '1', '2': '2', '3': '3'},
        }
        frame = DataFrame(test_data, dtype=float)
        assert len(frame) == 3
        assert frame['B'].dtype == np.float64
        assert frame['A'].dtype == np.float64

        frame = DataFrame(test_data)
        assert len(frame) == 3
        assert frame['B'].dtype == np.object_
        assert frame['A'].dtype == np.float64

        # can't cast to float
        test_data = {
            'A': dict(zip(range(20), tm.makeStringIndex(20))),
            'B': dict(zip(range(15), np.random.randn(15)))
        }
        frame = DataFrame(test_data, dtype=float)
        assert len(frame) == 20
        assert frame['A'].dtype == np.object_
        assert frame['B'].dtype == np.float64 
Example 10
Project: recruit   Author: Frank-qlu   File: test_dtypes.py    License: Apache License 2.0 6 votes vote down vote up
def test_is_dtype(self):
        assert PeriodDtype.is_dtype(self.dtype)
        assert PeriodDtype.is_dtype('period[D]')
        assert PeriodDtype.is_dtype('period[3D]')
        assert PeriodDtype.is_dtype(PeriodDtype('3D'))
        assert PeriodDtype.is_dtype('period[U]')
        assert PeriodDtype.is_dtype('period[S]')
        assert PeriodDtype.is_dtype(PeriodDtype('U'))
        assert PeriodDtype.is_dtype(PeriodDtype('S'))

        assert not PeriodDtype.is_dtype('D')
        assert not PeriodDtype.is_dtype('3D')
        assert not PeriodDtype.is_dtype('U')
        assert not PeriodDtype.is_dtype('S')
        assert not PeriodDtype.is_dtype('foo')
        assert not PeriodDtype.is_dtype(np.object_)
        assert not PeriodDtype.is_dtype(np.int64)
        assert not PeriodDtype.is_dtype(np.float64) 
Example 11
Project: recruit   Author: Frank-qlu   File: test_dtypes.py    License: Apache License 2.0 6 votes vote down vote up
def test_is_dtype(self):
        assert IntervalDtype.is_dtype(self.dtype)
        assert IntervalDtype.is_dtype('interval')
        assert IntervalDtype.is_dtype(IntervalDtype('float64'))
        assert IntervalDtype.is_dtype(IntervalDtype('int64'))
        assert IntervalDtype.is_dtype(IntervalDtype(np.int64))

        assert not IntervalDtype.is_dtype('D')
        assert not IntervalDtype.is_dtype('3D')
        assert not IntervalDtype.is_dtype('U')
        assert not IntervalDtype.is_dtype('S')
        assert not IntervalDtype.is_dtype('foo')
        assert not IntervalDtype.is_dtype('IntervalA')
        assert not IntervalDtype.is_dtype(np.object_)
        assert not IntervalDtype.is_dtype(np.int64)
        assert not IntervalDtype.is_dtype(np.float64) 
Example 12
Project: esmlab   Author: NCAR   File: core.py    License: Apache License 2.0 5 votes vote down vote up
def isdecoded(self, obj):
        return obj.dtype.type in {np.str_, np.object_, np.datetime64} 
Example 13
Project: arctic   Author: man-group   File: tickstore.py    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
def _empty(self, length, dtype):
        if dtype is not None and dtype == np.float64:
            rtn = np.empty(length, dtype)
            rtn[:] = np.nan
            return rtn
        else:
            return np.empty(length, dtype=np.object_) 
Example 14
Project: recruit   Author: Frank-qlu   File: test_io.py    License: Apache License 2.0 5 votes vote down vote up
def test_converters_cornercases(self):
        # Test the conversion to datetime.
        converter = {
            'date': lambda s: strptime(s, '%Y-%m-%d %H:%M:%SZ')}
        data = TextIO('2009-02-03 12:00:00Z, 72214.0')
        test = np.ndfromtxt(data, delimiter=',', dtype=None,
                            names=['date', 'stid'], converters=converter)
        control = np.array((datetime(2009, 2, 3), 72214.),
                           dtype=[('date', np.object_), ('stid', float)])
        assert_equal(test, control) 
Example 15
Project: recruit   Author: Frank-qlu   File: test_nanfunctions.py    License: Apache License 2.0 5 votes vote down vote up
def test_dtype_error(self):
        for f in self.nanfuncs:
            for dtype in [np.bool_, np.int_, np.object_]:
                assert_raises(TypeError, f, _ndat, axis=1, dtype=dtype) 
Example 16
Project: recruit   Author: Frank-qlu   File: test_nanfunctions.py    License: Apache License 2.0 5 votes vote down vote up
def test_out_dtype_error(self):
        for f in self.nanfuncs:
            for dtype in [np.bool_, np.int_, np.object_]:
                out = np.empty(_ndat.shape[0], dtype=dtype)
                assert_raises(TypeError, f, _ndat, axis=1, out=out) 
Example 17
Project: recruit   Author: Frank-qlu   File: test_polynomial.py    License: Apache License 2.0 5 votes vote down vote up
def test_objects(self):
        from decimal import Decimal
        p = np.poly1d([Decimal('4.0'), Decimal('3.0'), Decimal('2.0')])
        p2 = p * Decimal('1.333333333333333')
        assert_(p2[1] == Decimal("3.9999999999999990"))
        p2 = p.deriv()
        assert_(p2[1] == Decimal('8.0'))
        p2 = p.integ()
        assert_(p2[3] == Decimal("1.333333333333333333333333333"))
        assert_(p2[2] == Decimal('1.5'))
        assert_(np.issubdtype(p2.coeffs.dtype, np.object_))
        p = np.poly([Decimal(1), Decimal(2)])
        assert_equal(np.poly([Decimal(1), Decimal(2)]),
                     [1, Decimal(-3), Decimal(2)]) 
Example 18
Project: recruit   Author: Frank-qlu   File: test_defchararray.py    License: Apache License 2.0 5 votes vote down vote up
def test_rsplit(self):
        A = self.A.rsplit(b'3')
        tgt = [[[b' abc '], [b'']],
               [[b'12', b'45'], [b'MixedCase']],
               [[b'12', b' \t ', b'45 \x00 '], [b'UPPER']]]
        assert_(issubclass(A.dtype.type, np.object_))
        assert_equal(A.tolist(), tgt) 
Example 19
Project: recruit   Author: Frank-qlu   File: test_defchararray.py    License: Apache License 2.0 5 votes vote down vote up
def test_split(self):
        A = self.A.split(b'3')
        tgt = [
               [[b' abc '], [b'']],
               [[b'12', b'45'], [b'MixedCase']],
               [[b'12', b' \t ', b'45 \x00 '], [b'UPPER']]]
        assert_(issubclass(A.dtype.type, np.object_))
        assert_equal(A.tolist(), tgt) 
Example 20
Project: recruit   Author: Frank-qlu   File: test_defchararray.py    License: Apache License 2.0 5 votes vote down vote up
def test_splitlines(self):
        A = np.char.array(['abc\nfds\nwer']).splitlines()
        assert_(issubclass(A.dtype.type, np.object_))
        assert_(A.shape == (1,))
        assert_(len(A[0]) == 3) 
Example 21
Project: recruit   Author: Frank-qlu   File: test_umath.py    License: Apache License 2.0 5 votes vote down vote up
def test_lcm_object(self):
        self._test_lcm_inner(np.object_) 
Example 22
Project: recruit   Author: Frank-qlu   File: test_regression.py    License: Apache License 2.0 5 votes vote down vote up
def test_unpickle_dtype_with_object(self):
        # Implemented in r2840
        dt = np.dtype([('x', int), ('y', np.object_), ('z', 'O')])
        for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
            f = BytesIO()
            pickle.dump(dt, f, protocol=proto)
            f.seek(0)
            dt_ = pickle.load(f)
            f.close()
            assert_equal(dt, dt_) 
Example 23
Project: recruit   Author: Frank-qlu   File: test_regression.py    License: Apache License 2.0 5 votes vote down vote up
def test_mem_array_creation_invalid_specification(self):
        # Ticket #196
        dt = np.dtype([('x', int), ('y', np.object_)])
        # Wrong way
        assert_raises(ValueError, np.array, [1, 'object'], dt)
        # Correct way
        np.array([(1, 'object')], dt) 
Example 24
Project: recruit   Author: Frank-qlu   File: test_regression.py    License: Apache License 2.0 5 votes vote down vote up
def test_for_object_scalar_creation(self):
        # Ticket #816
        a = np.object_()
        b = np.object_(3)
        b2 = np.object_(3.0)
        c = np.object_([4, 5])
        d = np.object_([None, {}, []])
        assert_(a is None)
        assert_(type(b) is int)
        assert_(type(b2) is float)
        assert_(type(c) is np.ndarray)
        assert_(c.dtype == object)
        assert_(d.dtype == object) 
Example 25
Project: recruit   Author: Frank-qlu   File: test_regression.py    License: Apache License 2.0 5 votes vote down vote up
def test_object_array_to_fixed_string(self):
        # Ticket #1235.
        a = np.array(['abcdefgh', 'ijklmnop'], dtype=np.object_)
        b = np.array(a, dtype=(np.str_, 8))
        assert_equal(a, b)
        c = np.array(a, dtype=(np.str_, 5))
        assert_equal(c, np.array(['abcde', 'ijklm']))
        d = np.array(a, dtype=(np.str_, 12))
        assert_equal(a, d)
        e = np.empty((2, ), dtype=(np.str_, 8))
        e[:] = a[:]
        assert_equal(a, e) 
Example 26
Project: recruit   Author: Frank-qlu   File: test_panel.py    License: Apache License 2.0 5 votes vote down vote up
def test_setitem(self):
        lp = self.panel.filter(['ItemA', 'ItemB']).to_frame()

        with pytest.raises(TypeError):
            self.panel['ItemE'] = lp

        # DataFrame
        df = self.panel['ItemA'][2:].filter(items=['A', 'B'])
        self.panel['ItemF'] = df
        self.panel['ItemE'] = df

        df2 = self.panel['ItemF']

        assert_frame_equal(df, df2.reindex(
            index=df.index, columns=df.columns))

        # scalar
        self.panel['ItemG'] = 1
        self.panel['ItemE'] = True
        assert self.panel['ItemG'].values.dtype == np.int64
        assert self.panel['ItemE'].values.dtype == np.bool_

        # object dtype
        self.panel['ItemQ'] = 'foo'
        assert self.panel['ItemQ'].values.dtype == np.object_

        # boolean dtype
        self.panel['ItemP'] = self.panel['ItemA'] > 0
        assert self.panel['ItemP'].values.dtype == np.bool_

        pytest.raises(TypeError, self.panel.__setitem__, 'foo',
                      self.panel.loc[['ItemP']])

        # bad shape
        p = Panel(np.random.randn(4, 3, 2))
        msg = (r"shape of value must be \(3, 2\), "
               r"shape of given object was \(4, 2\)")
        with pytest.raises(ValueError, match=msg):
            p[0] = np.random.randn(4, 2) 
Example 27
Project: recruit   Author: Frank-qlu   File: test_panel.py    License: Apache License 2.0 5 votes vote down vote up
def test_from_dict_mixed_orient(self):
        df = tm.makeDataFrame()
        df['foo'] = 'bar'

        data = {'k1': df, 'k2': df}

        panel = Panel.from_dict(data, orient='minor')

        assert panel['foo'].values.dtype == np.object_
        assert panel['A'].values.dtype == np.float64 
Example 28
Project: recruit   Author: Frank-qlu   File: test_internals.py    License: Apache License 2.0 5 votes vote down vote up
def test_set(self):
        mgr = create_mgr('a,b,c: int', item_shape=(3, ))

        mgr.set('d', np.array(['foo'] * 3))
        mgr.set('b', np.array(['bar'] * 3))
        tm.assert_numpy_array_equal(mgr.get('a').internal_values(),
                                    np.array([0] * 3))
        tm.assert_numpy_array_equal(mgr.get('b').internal_values(),
                                    np.array(['bar'] * 3, dtype=np.object_))
        tm.assert_numpy_array_equal(mgr.get('c').internal_values(),
                                    np.array([2] * 3))
        tm.assert_numpy_array_equal(mgr.get('d').internal_values(),
                                    np.array(['foo'] * 3, dtype=np.object_)) 
Example 29
Project: recruit   Author: Frank-qlu   File: test_internals.py    License: Apache License 2.0 5 votes vote down vote up
def test_set_change_dtype(self, mgr):
        mgr.set('baz', np.zeros(N, dtype=bool))

        mgr.set('baz', np.repeat('foo', N))
        assert mgr.get('baz').dtype == np.object_

        mgr2 = mgr.consolidate()
        mgr2.set('baz', np.repeat('foo', N))
        assert mgr2.get('baz').dtype == np.object_

        mgr2.set('quux', randn(N).astype(int))
        assert mgr2.get('quux').dtype == np.int_

        mgr2.set('quux', randn(N))
        assert mgr2.get('quux').dtype == np.float_ 
Example 30
Project: recruit   Author: Frank-qlu   File: test_internals.py    License: Apache License 2.0 5 votes vote down vote up
def test_get_numeric_data(self):
        mgr = create_mgr('int: int; float: float; complex: complex;'
                         'str: object; bool: bool; obj: object; dt: datetime',
                         item_shape=(3, ))
        mgr.set('obj', np.array([1, 2, 3], dtype=np.object_))

        numeric = mgr.get_numeric_data()
        tm.assert_index_equal(numeric.items,
                              pd.Index(['int', 'float', 'complex', 'bool']))
        assert_almost_equal(
            mgr.get('float', fastpath=False), numeric.get('float',
                                                          fastpath=False))
        assert_almost_equal(
            mgr.get('float').internal_values(),
            numeric.get('float').internal_values())

        # Check sharing
        numeric.set('float', np.array([100., 200., 300.]))
        assert_almost_equal(
            mgr.get('float', fastpath=False), np.array([100., 200., 300.]))
        assert_almost_equal(
            mgr.get('float').internal_values(), np.array([100., 200., 300.]))

        numeric2 = mgr.get_numeric_data(copy=True)
        tm.assert_index_equal(numeric.items,
                              pd.Index(['int', 'float', 'complex', 'bool']))
        numeric2.set('float', np.array([1000., 2000., 3000.]))
        assert_almost_equal(
            mgr.get('float', fastpath=False), np.array([100., 200., 300.]))
        assert_almost_equal(
            mgr.get('float').internal_values(), np.array([100., 200., 300.]))