Example #1
0
 def test_isscalar_numpy_zerodim_arrays(self):
     for zerodim in [np.array(1), np.array('foobar'),
                     np.array(np.datetime64('2014-01-01')),
                     np.array(np.timedelta64(1, 'h')),
                     np.array(np.datetime64('NaT'))]:
         self.assertFalse(is_scalar(zerodim))
         self.assertTrue(is_scalar(lib.item_from_zerodim(zerodim)))
Example #2
0
    def _evaluate_compare(self, other, op):
        """
        We have been called because a comparison between
        8 aware arrays. numpy >= 1.11 will
        now warn about NaT comparisons
        """

        # coerce to a similar object
        if not isinstance(other, type(self)):
            if not com.is_list_like(other):
                # scalar
                other = [other]
            elif lib.isscalar(lib.item_from_zerodim(other)):
                # ndarray scalar
                other = [other.item()]
            other = type(self)(other)

        # compare
        result = getattr(self.asi8, op)(other.asi8)

        # technically we could support bool dtyped Index
        # for now just return the indexing array directly
        mask = (self._isnan) | (other._isnan)
        if is_bool_dtype(result):
            result[mask] = False
            return result
        try:
            result[mask] = tslib.iNaT
            return Index(result)
        except TypeError:
            return result
Example #3
0
    def _evaluate_compare(self, other, op):
        """
        We have been called because a comparison between
        8 aware arrays. numpy >= 1.11 will
        now warn about NaT comparisons
        """

        # coerce to a similar object
        if not isinstance(other, type(self)):
            if not com.is_list_like(other):
                # scalar
                other = [other]
            elif lib.isscalar(lib.item_from_zerodim(other)):
                # ndarray scalar
                other = [other.item()]
            other = type(self)(other)

        # compare
        result = getattr(self.asi8, op)(other.asi8)

        # technically we could support bool dtyped Index
        # for now just return the indexing array directly
        mask = (self._isnan) | (other._isnan)
        if is_bool_dtype(result):
            result[mask] = False
            return result
        try:
            result[mask] = tslib.iNaT
            return Index(result)
        except TypeError:
            return result
Example #4
0
 def test_isscalar_numpy_zerodim_arrays(self):
     for zerodim in [np.array(1), np.array('foobar'),
                     np.array(np.datetime64('2014-01-01')),
                     np.array(np.timedelta64(1, 'h')),
                     np.array(np.datetime64('NaT'))]:
         self.assertFalse(lib.isscalar(zerodim))
         self.assertTrue(lib.isscalar(lib.item_from_zerodim(zerodim)))
Example #5
0
    def wrapper(self, other, axis=None):
        # Validate the axis parameter
        if axis is not None:
            self._get_axis_number(axis)

        if isinstance(other, ABCSeries):
            name = _maybe_match_name(self, other)
            if len(self) != len(other):
                raise ValueError('Series lengths must match to compare')
            return self._constructor(na_op(self.values, other.values),
                                     index=self.index, name=name)
        elif isinstance(other, pd.DataFrame):  # pragma: no cover
            return NotImplemented
        elif isinstance(other, (np.ndarray, pd.Index)):
            # do not check length of zerodim array
            # as it will broadcast
            if (not lib.isscalar(lib.item_from_zerodim(other)) and
                    len(self) != len(other)):
                raise ValueError('Lengths must match to compare')

            if isinstance(other, ABCPeriodIndex):
                # temp workaround until fixing GH 13637
                # tested in test_nat_comparisons
                # (pandas.tests.series.test_operators.TestSeriesOperators)
                return self._constructor(na_op(self.values,
                                               other.asobject.values),
                                         index=self.index)

            return self._constructor(na_op(self.values, np.asarray(other)),
                                     index=self.index).__finalize__(self)
        elif isinstance(other, pd.Categorical):
            if not is_categorical_dtype(self):
                msg = ("Cannot compare a Categorical for op {op} with Series "
                       "of dtype {typ}.\nIf you want to compare values, use "
                       "'series <op> np.asarray(other)'.")
                raise TypeError(msg.format(op=op, typ=self.dtype))

        if is_categorical_dtype(self):
            # cats are a special case as get_values() would return an ndarray,
            # which would then not take categories ordering into account
            # we can go directly to op, as the na_op would just test again and
            # dispatch to it.
            res = op(self.values, other)
        else:
            values = self.get_values()
            if isinstance(other, (list, np.ndarray)):
                other = np.asarray(other)

            res = na_op(values, other)
            if isscalar(res):
                raise TypeError('Could not compare %s type with Series' %
                                type(other))

            # always return a full value series here
            res = _values_from_object(res)

        res = pd.Series(res, index=self.index, name=self.name, dtype='bool')
        return res
Example #6
0
 def test_isscalar_numpy_zerodim_arrays(self):
     for zerodim in [
         np.array(1),
         np.array("foobar"),
         np.array(np.datetime64("2014-01-01")),
         np.array(np.timedelta64(1, "h")),
     ]:
         self.assertFalse(isscalar(zerodim))
         self.assertTrue(isscalar(item_from_zerodim(zerodim)))