Exemplo n.º 1
0
 def test_construction_from_string(self):
     result = DatetimeTZDtype('datetime64[ns, US/Eastern]')
     self.assertTrue(is_dtype_equal(self.dtype, result))
     result = DatetimeTZDtype.construct_from_string(
         'datetime64[ns, US/Eastern]')
     self.assertTrue(is_dtype_equal(self.dtype, result))
     self.assertRaises(TypeError,
                       lambda: DatetimeTZDtype.construct_from_string('foo'))
Exemplo n.º 2
0
 def test_construction_from_string(self):
     result = DatetimeTZDtype('datetime64[ns, US/Eastern]')
     self.assertTrue(is_dtype_equal(self.dtype, result))
     result = DatetimeTZDtype.construct_from_string(
         'datetime64[ns, US/Eastern]')
     self.assertTrue(is_dtype_equal(self.dtype, result))
     self.assertRaises(TypeError,
                       lambda: DatetimeTZDtype.construct_from_string('foo'))
Exemplo n.º 3
0
 def test_construction_from_string(self):
     result = IntervalDtype('interval[int64]')
     self.assertTrue(is_dtype_equal(self.dtype, result))
     result = IntervalDtype.construct_from_string('interval[int64]')
     self.assertTrue(is_dtype_equal(self.dtype, result))
     with tm.assertRaises(TypeError):
         IntervalDtype.construct_from_string('foo')
     with tm.assertRaises(TypeError):
         IntervalDtype.construct_from_string('interval[foo]')
     with tm.assertRaises(TypeError):
         IntervalDtype.construct_from_string('foo[int64]')
Exemplo n.º 4
0
def test_dtype_equal():
    assert is_dtype_equal(np.int64, np.int64)
    assert not is_dtype_equal(np.int64, np.float64)

    p1 = PeriodDtype('D')
    p2 = PeriodDtype('D')
    assert is_dtype_equal(p1, p2)
    assert not is_dtype_equal(np.int64, p1)

    p3 = PeriodDtype('2D')
    assert not is_dtype_equal(p1, p3)

    assert not DatetimeTZDtype.is_dtype(np.int64)
    assert not PeriodDtype.is_dtype(np.int64)
Exemplo n.º 5
0
    def equals(self, other):
        """
        Determines if two Index objects contain the same elements.
        """
        if self.is_(other):
            return True

        if not isinstance(other, ABCIndexClass):
            return False
        elif not isinstance(other, type(self)):
            try:
                other = type(self)(other)
            except:
                return False

        if not is_dtype_equal(self.dtype, other.dtype):
            # have different timezone
            return False

        # ToDo: Remove this when PeriodDtype is added
        elif isinstance(self, ABCPeriodIndex):
            if not isinstance(other, ABCPeriodIndex):
                return False
            if self.freq != other.freq:
                return False

        return np.array_equal(self.asi8, other.asi8)
Exemplo n.º 6
0
    def test_construction_from_string(self):
        result = PeriodDtype('period[D]')
        self.assertTrue(is_dtype_equal(self.dtype, result))
        result = PeriodDtype.construct_from_string('period[D]')
        self.assertTrue(is_dtype_equal(self.dtype, result))
        with tm.assertRaises(TypeError):
            PeriodDtype.construct_from_string('foo')
        with tm.assertRaises(TypeError):
            PeriodDtype.construct_from_string('period[foo]')
        with tm.assertRaises(TypeError):
            PeriodDtype.construct_from_string('foo[D]')

        with tm.assertRaises(TypeError):
            PeriodDtype.construct_from_string('datetime64[ns]')
        with tm.assertRaises(TypeError):
            PeriodDtype.construct_from_string('datetime64[ns, US/Eastern]')
Exemplo n.º 7
0
    def __new__(cls,
                data=None,
                dtype=None,
                copy=False,
                name=None,
                fastpath=False):

        if fastpath:
            return cls._simple_new(data, name=name)

        # isscalar, generators handled in coerce_to_ndarray
        data = cls._coerce_to_ndarray(data)

        if issubclass(data.dtype.type, compat.string_types):
            cls._string_data_error(data)

        if copy or not is_dtype_equal(data.dtype, cls._default_dtype):
            subarr = np.array(data, dtype=cls._default_dtype, copy=copy)
            cls._assert_safe_casting(data, subarr)
        else:
            subarr = data

        if name is None and hasattr(data, 'name'):
            name = data.name
        return cls._simple_new(subarr, name=name)
Exemplo n.º 8
0
    def test_construction_from_string(self):
        result = PeriodDtype('period[D]')
        self.assertTrue(is_dtype_equal(self.dtype, result))
        result = PeriodDtype.construct_from_string('period[D]')
        self.assertTrue(is_dtype_equal(self.dtype, result))
        with tm.assertRaises(TypeError):
            PeriodDtype.construct_from_string('foo')
        with tm.assertRaises(TypeError):
            PeriodDtype.construct_from_string('period[foo]')
        with tm.assertRaises(TypeError):
            PeriodDtype.construct_from_string('foo[D]')

        with tm.assertRaises(TypeError):
            PeriodDtype.construct_from_string('datetime64[ns]')
        with tm.assertRaises(TypeError):
            PeriodDtype.construct_from_string('datetime64[ns, US/Eastern]')
Exemplo n.º 9
0
    def equals(self, other):
        """
        Determines if two Index objects contain the same elements.
        """
        if self.is_(other):
            return True

        if not isinstance(other, ABCIndexClass):
            return False
        elif not isinstance(other, type(self)):
            try:
                other = type(self)(other)
            except:
                return False

        if not is_dtype_equal(self.dtype, other.dtype):
            # have different timezone
            return False

        # ToDo: Remove this when PeriodDtype is added
        elif isinstance(self, ABCPeriodIndex):
            if not isinstance(other, ABCPeriodIndex):
                return False
            if self.freq != other.freq:
                return False

        return np.array_equal(self.asi8, other.asi8)
Exemplo n.º 10
0
def test_dtype_equal_strict():

    # we are strict on kind equality
    for dtype in [np.int8, np.int16, np.int32]:
        assert not is_dtype_equal(np.int64, dtype)

    for dtype in [np.float32]:
        assert not is_dtype_equal(np.float64, dtype)

    # strict w.r.t. PeriodDtype
    assert not is_dtype_equal(PeriodDtype('D'),
                              PeriodDtype('2D'))

    # strict w.r.t. datetime64
    assert not is_dtype_equal(
        pandas_dtype('datetime64[ns, US/Eastern]'),
        pandas_dtype('datetime64[ns, CET]'))
Exemplo n.º 11
0
 def fill_value(self, value):
     if not is_scalar(value):
         raise ValueError('fill_value must be a scalar')
     # if the specified value triggers type promotion, raise ValueError
     new_dtype, fill_value = _maybe_promote(self.dtype, value)
     if is_dtype_equal(self.dtype, new_dtype):
         self._fill_value = fill_value
     else:
         msg = 'unable to set fill_value {0} to {1} dtype'
         raise ValueError(msg.format(value, self.dtype))
Exemplo n.º 12
0
    def test_equality(self):
        self.assertTrue(is_dtype_equal(self.dtype, 'period[D]'))
        self.assertTrue(is_dtype_equal(self.dtype, PeriodDtype('D')))
        self.assertTrue(is_dtype_equal(self.dtype, PeriodDtype('D')))
        self.assertTrue(is_dtype_equal(PeriodDtype('D'), PeriodDtype('D')))

        self.assertFalse(is_dtype_equal(self.dtype, 'D'))
        self.assertFalse(is_dtype_equal(PeriodDtype('D'), PeriodDtype('2D')))
Exemplo n.º 13
0
    def test_equality(self):
        self.assertTrue(is_dtype_equal(self.dtype, 'period[D]'))
        self.assertTrue(is_dtype_equal(self.dtype, PeriodDtype('D')))
        self.assertTrue(is_dtype_equal(self.dtype, PeriodDtype('D')))
        self.assertTrue(is_dtype_equal(PeriodDtype('D'), PeriodDtype('D')))

        self.assertFalse(is_dtype_equal(self.dtype, 'D'))
        self.assertFalse(is_dtype_equal(PeriodDtype('D'), PeriodDtype('2D')))
Exemplo n.º 14
0
    def test_equality(self):
        self.assertTrue(is_dtype_equal(self.dtype, 'interval[int64]'))
        self.assertTrue(is_dtype_equal(self.dtype, IntervalDtype('int64')))
        self.assertTrue(is_dtype_equal(self.dtype, IntervalDtype('int64')))
        self.assertTrue(
            is_dtype_equal(IntervalDtype('int64'), IntervalDtype('int64')))

        self.assertFalse(is_dtype_equal(self.dtype, 'int64'))
        self.assertFalse(
            is_dtype_equal(IntervalDtype('int64'), IntervalDtype('float64')))
Exemplo n.º 15
0
    def equals(self, other):
        """
        Determines if two Index objects contain the same elements.
        """
        if self is other:
            return True

        # need to compare nans locations and make sure that they are the same
        # since nans don't compare equal this is a bit tricky
        try:
            if not isinstance(other, Float64Index):
                other = self._constructor(other)
            if (not is_dtype_equal(self.dtype, other.dtype) or
                    self.shape != other.shape):
                return False
            left, right = self._values, other._values
            return ((left == right) | (self._isnan & other._isnan)).all()
        except (TypeError, ValueError):
            return False
Exemplo n.º 16
0
    def equals(self, other):
        """
        Determines if two Index objects contain the same elements.
        """
        if self is other:
            return True

        # need to compare nans locations and make sure that they are the same
        # since nans don't compare equal this is a bit tricky
        try:
            if not isinstance(other, Float64Index):
                other = self._constructor(other)
            if (not is_dtype_equal(self.dtype, other.dtype)
                    or self.shape != other.shape):
                return False
            left, right = self._values, other._values
            return ((left == right) | (self._isnan & other._isnan)).all()
        except (TypeError, ValueError):
            return False
Exemplo n.º 17
0
    def test_equality(self):
        self.assertTrue(
            is_dtype_equal(self.dtype, 'datetime64[ns, US/Eastern]'))
        self.assertTrue(
            is_dtype_equal(self.dtype, DatetimeTZDtype('ns', 'US/Eastern')))
        self.assertFalse(is_dtype_equal(self.dtype, 'foo'))
        self.assertFalse(
            is_dtype_equal(self.dtype, DatetimeTZDtype('ns', 'CET')))
        self.assertFalse(
            is_dtype_equal(DatetimeTZDtype('ns', 'US/Eastern'),
                           DatetimeTZDtype('ns', 'US/Pacific')))

        # numpy compat
        self.assertTrue(is_dtype_equal(np.dtype("M8[ns]"), "datetime64[ns]"))
Exemplo n.º 18
0
    def test_equality(self):
        self.assertTrue(is_dtype_equal(self.dtype,
                                       'datetime64[ns, US/Eastern]'))
        self.assertTrue(is_dtype_equal(self.dtype, DatetimeTZDtype(
            'ns', 'US/Eastern')))
        self.assertFalse(is_dtype_equal(self.dtype, 'foo'))
        self.assertFalse(is_dtype_equal(self.dtype, DatetimeTZDtype('ns',
                                                                    'CET')))
        self.assertFalse(is_dtype_equal(
            DatetimeTZDtype('ns', 'US/Eastern'), DatetimeTZDtype(
                'ns', 'US/Pacific')))

        # numpy compat
        self.assertTrue(is_dtype_equal(np.dtype("M8[ns]"), "datetime64[ns]"))
Exemplo n.º 19
0
    def __new__(cls, data=None, dtype=None, copy=False, name=None,
                fastpath=False):

        if fastpath:
            return cls._simple_new(data, name=name)

        # isscalar, generators handled in coerce_to_ndarray
        data = cls._coerce_to_ndarray(data)

        if issubclass(data.dtype.type, compat.string_types):
            cls._string_data_error(data)

        if copy or not is_dtype_equal(data.dtype, cls._default_dtype):
            subarr = np.array(data, dtype=cls._default_dtype, copy=copy)
            cls._assert_safe_casting(data, subarr)
        else:
            subarr = data

        if name is None and hasattr(data, 'name'):
            name = data.name
        return cls._simple_new(subarr, name=name)
Exemplo n.º 20
0
 def test_construction_from_string(self):
     result = CategoricalDtype.construct_from_string('category')
     self.assertTrue(is_dtype_equal(self.dtype, result))
     self.assertRaises(
         TypeError, lambda: CategoricalDtype.construct_from_string('foo'))
Exemplo n.º 21
0
def test_dtype_equal(name1, dtype1, name2, dtype2):

    # match equal to self, but not equal to other
    assert is_dtype_equal(dtype1, dtype1)
    if name1 != name2:
        assert not is_dtype_equal(dtype1, dtype2)
Exemplo n.º 22
0
 def test_equality_invalid(self):
     self.assertRaises(self.dtype == 'foo')
     self.assertFalse(is_dtype_equal(self.dtype, np.int64))
Exemplo n.º 23
0
 def test_equality_invalid(self):
     self.assertRaises(self.dtype == 'foo')
     self.assertFalse(is_dtype_equal(self.dtype, np.int64))
Exemplo n.º 24
0
def _sparse_array_op(left, right, op, name, series=False):

    if series and is_integer_dtype(left) and is_integer_dtype(right):
        # series coerces to float64 if result should have NaN/inf
        if name in ('floordiv', 'mod') and (right.values == 0).any():
            left = left.astype(np.float64)
            right = right.astype(np.float64)
        elif name in ('rfloordiv', 'rmod') and (left.values == 0).any():
            left = left.astype(np.float64)
            right = right.astype(np.float64)

    # dtype used to find corresponding sparse method
    if not is_dtype_equal(left.dtype, right.dtype):
        dtype = _find_common_type([left.dtype, right.dtype])
        left = left.astype(dtype)
        right = right.astype(dtype)
    else:
        dtype = left.dtype

    # dtype the result must have
    result_dtype = None

    if left.sp_index.ngaps == 0 or right.sp_index.ngaps == 0:
        with np.errstate(all='ignore'):
            result = op(left.get_values(), right.get_values())
            fill = op(_get_fill(left), _get_fill(right))

        if left.sp_index.ngaps == 0:
            index = left.sp_index
        else:
            index = right.sp_index
    elif left.sp_index.equals(right.sp_index):
        with np.errstate(all='ignore'):
            result = op(left.sp_values, right.sp_values)
            fill = op(_get_fill(left), _get_fill(right))
        index = left.sp_index
    else:
        if name[0] == 'r':
            left, right = right, left
            name = name[1:]

        if name in ('and', 'or') and dtype == 'bool':
            opname = 'sparse_{name}_uint8'.format(name=name, dtype=dtype)
            # to make template simple, cast here
            left_sp_values = left.sp_values.view(np.uint8)
            right_sp_values = right.sp_values.view(np.uint8)
            result_dtype = np.bool
        else:
            opname = 'sparse_{name}_{dtype}'.format(name=name, dtype=dtype)
            left_sp_values = left.sp_values
            right_sp_values = right.sp_values

        sparse_op = getattr(splib, opname)
        with np.errstate(all='ignore'):
            result, index, fill = sparse_op(left_sp_values, left.sp_index,
                                            left.fill_value, right_sp_values,
                                            right.sp_index, right.fill_value)

    if result_dtype is None:
        result_dtype = result.dtype

    return _wrap_result(name, result, index, fill, dtype=result_dtype)
Exemplo n.º 25
0
 def test_equality(self):
     self.assertTrue(is_dtype_equal(self.dtype, 'category'))
     self.assertTrue(is_dtype_equal(self.dtype, CategoricalDtype()))
     self.assertFalse(is_dtype_equal(self.dtype, 'foo'))
Exemplo n.º 26
0
 def test_equality(self):
     self.assertTrue(is_dtype_equal(self.dtype, 'category'))
     self.assertTrue(is_dtype_equal(self.dtype, CategoricalDtype()))
     self.assertFalse(is_dtype_equal(self.dtype, 'foo'))
Exemplo n.º 27
0
 def test_construction_from_string(self):
     result = CategoricalDtype.construct_from_string('category')
     self.assertTrue(is_dtype_equal(self.dtype, result))
     self.assertRaises(
         TypeError, lambda: CategoricalDtype.construct_from_string('foo'))
Exemplo n.º 28
0
def _sparse_array_op(left, right, op, name, series=False):

    if series and is_integer_dtype(left) and is_integer_dtype(right):
        # series coerces to float64 if result should have NaN/inf
        if name in ('floordiv', 'mod') and (right.values == 0).any():
            left = left.astype(np.float64)
            right = right.astype(np.float64)
        elif name in ('rfloordiv', 'rmod') and (left.values == 0).any():
            left = left.astype(np.float64)
            right = right.astype(np.float64)

    # dtype used to find corresponding sparse method
    if not is_dtype_equal(left.dtype, right.dtype):
        dtype = _find_common_type([left.dtype, right.dtype])
        left = left.astype(dtype)
        right = right.astype(dtype)
    else:
        dtype = left.dtype

    # dtype the result must have
    result_dtype = None

    if left.sp_index.ngaps == 0 or right.sp_index.ngaps == 0:
        with np.errstate(all='ignore'):
            result = op(left.get_values(), right.get_values())
            fill = op(_get_fill(left), _get_fill(right))

        if left.sp_index.ngaps == 0:
            index = left.sp_index
        else:
            index = right.sp_index
    elif left.sp_index.equals(right.sp_index):
        with np.errstate(all='ignore'):
            result = op(left.sp_values, right.sp_values)
            fill = op(_get_fill(left), _get_fill(right))
        index = left.sp_index
    else:
        if name[0] == 'r':
            left, right = right, left
            name = name[1:]

        if name in ('and', 'or') and dtype == 'bool':
            opname = 'sparse_{name}_uint8'.format(name=name, dtype=dtype)
            # to make template simple, cast here
            left_sp_values = left.sp_values.view(np.uint8)
            right_sp_values = right.sp_values.view(np.uint8)
            result_dtype = np.bool
        else:
            opname = 'sparse_{name}_{dtype}'.format(name=name, dtype=dtype)
            left_sp_values = left.sp_values
            right_sp_values = right.sp_values

        sparse_op = getattr(splib, opname)
        with np.errstate(all='ignore'):
            result, index, fill = sparse_op(left_sp_values, left.sp_index,
                                            left.fill_value, right_sp_values,
                                            right.sp_index, right.fill_value)

    if result_dtype is None:
        result_dtype = result.dtype

    return _wrap_result(name, result, index, fill, dtype=result_dtype)