Python pandas.core.indexes.base.Index() Examples

The following are 30 code examples of pandas.core.indexes.base.Index(). 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 also want to check out all available functions/classes of the module pandas.core.indexes.base , or try the search function .
Example #1
Source File: category.py    From Splunking-Crime with GNU Affero General Public License v3.0 6 votes vote down vote up
def _reindex_non_unique(self, target):
        """ reindex from a non-unique; which CategoricalIndex's are almost
        always
        """
        new_target, indexer = self.reindex(target)
        new_indexer = None

        check = indexer == -1
        if check.any():
            new_indexer = np.arange(len(self.take(indexer)))
            new_indexer[check] = -1

        cats = self.categories.get_indexer(target)
        if not (cats == -1).any():
            # .reindex returns normal Index. Revert to CategoricalIndex if
            # all targets are included in my categories
            new_target = self._shallow_copy(new_target)

        return new_target, indexer, new_indexer 
Example #2
Source File: range.py    From elasticintel with GNU General Public License v3.0 6 votes vote down vote up
def _simple_new(cls, start, stop=None, step=None, name=None,
                    dtype=None, **kwargs):
        result = object.__new__(cls)

        # handle passed None, non-integers
        if start is None and stop is None:
            # empty
            start, stop, step = 0, 0, 1

        if start is None or not is_integer(start):
            try:

                return RangeIndex(start, stop, step, name=name, **kwargs)
            except TypeError:
                return Index(start, stop, step, name=name, **kwargs)

        result._start = start
        result._stop = stop or 0
        result._step = step or 1
        result.name = name
        for k, v in compat.iteritems(kwargs):
            setattr(result, k, v)

        result._reset_identity()
        return result 
Example #3
Source File: category.py    From Splunking-Crime with GNU Affero General Public License v3.0 6 votes vote down vote up
def equals(self, other):
        """
        Determines if two CategorialIndex objects contain the same elements.
        """
        if self.is_(other):
            return True

        if not isinstance(other, Index):
            return False

        try:
            other = self._is_dtype_compat(other)
            return array_equivalent(self._data, other)
        except (TypeError, ValueError):
            pass

        return False 
Example #4
Source File: category.py    From Splunking-Crime with GNU Affero General Public License v3.0 6 votes vote down vote up
def _convert_list_indexer(self, keyarr, kind=None):
        # Return our indexer or raise if all of the values are not included in
        # the categories

        if self.categories._defer_to_indexing:
            indexer = self.categories._convert_list_indexer(keyarr, kind=kind)
            return Index(self.codes).get_indexer_for(indexer)

        indexer = self.categories.get_indexer(np.asarray(keyarr))
        if (indexer == -1).any():
            raise KeyError(
                "a list-indexer must only "
                "include values that are "
                "in the categories")

        return self.get_indexer(keyarr) 
Example #5
Source File: category.py    From elasticintel with GNU General Public License v3.0 6 votes vote down vote up
def _convert_list_indexer(self, keyarr, kind=None):
        # Return our indexer or raise if all of the values are not included in
        # the categories

        if self.categories._defer_to_indexing:
            indexer = self.categories._convert_list_indexer(keyarr, kind=kind)
            return Index(self.codes).get_indexer_for(indexer)

        indexer = self.categories.get_indexer(np.asarray(keyarr))
        if (indexer == -1).any():
            raise KeyError(
                "a list-indexer must only "
                "include values that are "
                "in the categories")

        return self.get_indexer(keyarr) 
Example #6
Source File: range.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def _simple_new(cls, start, stop=None, step=None, name=None,
                    dtype=None, **kwargs):
        result = object.__new__(cls)

        # handle passed None, non-integers
        if start is None and stop is None:
            # empty
            start, stop, step = 0, 0, 1

        if start is None or not is_integer(start):
            try:

                return RangeIndex(start, stop, step, name=name, **kwargs)
            except TypeError:
                return Index(start, stop, step, name=name, **kwargs)

        result._start = start
        result._stop = stop or 0
        result._step = step or 1
        result.name = name
        for k, v in compat.iteritems(kwargs):
            setattr(result, k, v)

        result._reset_identity()
        return result 
Example #7
Source File: category.py    From elasticintel with GNU General Public License v3.0 6 votes vote down vote up
def _reindex_non_unique(self, target):
        """ reindex from a non-unique; which CategoricalIndex's are almost
        always
        """
        new_target, indexer = self.reindex(target)
        new_indexer = None

        check = indexer == -1
        if check.any():
            new_indexer = np.arange(len(self.take(indexer)))
            new_indexer[check] = -1

        cats = self.categories.get_indexer(target)
        if not (cats == -1).any():
            # .reindex returns normal Index. Revert to CategoricalIndex if
            # all targets are included in my categories
            new_target = self._shallow_copy(new_target)

        return new_target, indexer, new_indexer 
Example #8
Source File: category.py    From elasticintel with GNU General Public License v3.0 6 votes vote down vote up
def equals(self, other):
        """
        Determines if two CategorialIndex objects contain the same elements.
        """
        if self.is_(other):
            return True

        if not isinstance(other, Index):
            return False

        try:
            other = self._is_dtype_compat(other)
            return array_equivalent(self._data, other)
        except (TypeError, ValueError):
            pass

        return False 
Example #9
Source File: range.py    From Splunking-Crime with GNU Affero General Public License v3.0 6 votes vote down vote up
def _simple_new(cls, start, stop=None, step=None, name=None,
                    dtype=None, **kwargs):
        result = object.__new__(cls)

        # handle passed None, non-integers
        if start is None and stop is None:
            # empty
            start, stop, step = 0, 0, 1

        if start is None or not is_integer(start):
            try:

                return RangeIndex(start, stop, step, name=name, **kwargs)
            except TypeError:
                return Index(start, stop, step, name=name, **kwargs)

        result._start = start
        result._stop = stop or 0
        result._step = step or 1
        result.name = name
        for k, v in compat.iteritems(kwargs):
            setattr(result, k, v)

        result._reset_identity()
        return result 
Example #10
Source File: category.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def is_monotonic_increasing(self):
        return Index(self.codes).is_monotonic_increasing 
Example #11
Source File: category.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def delete(self, loc):
        """
        Make new Index with passed location(-s) deleted

        Returns
        -------
        new_index : Index
        """
        return self._create_from_codes(np.delete(self.codes, loc)) 
Example #12
Source File: category.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def insert(self, loc, item):
        """
        Make new Index inserting new item at location. Follows
        Python list.append semantics for negative values

        Parameters
        ----------
        loc : int
        item : object

        Returns
        -------
        new_index : Index

        Raises
        ------
        ValueError if the item is not in the categories

        """
        code = self.categories.get_indexer([item])
        if (code == -1):
            raise TypeError("cannot insert an item into a CategoricalIndex "
                            "that is not already an existing category")

        codes = self.codes
        codes = np.concatenate((codes[:loc], code, codes[loc:]))
        return self._create_from_codes(codes) 
Example #13
Source File: category.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def _add_comparison_methods(cls):
        """ add in comparison methods """

        def _make_compare(op):
            def _evaluate_compare(self, other):

                # if we have a Categorical type, then must have the same
                # categories
                if isinstance(other, CategoricalIndex):
                    other = other._values
                elif isinstance(other, Index):
                    other = self._create_categorical(
                        self, other._values, categories=self.categories,
                        ordered=self.ordered)

                if isinstance(other, (ABCCategorical, np.ndarray,
                                      ABCSeries)):
                    if len(self.values) != len(other):
                        raise ValueError("Lengths must match to compare")

                if isinstance(other, ABCCategorical):
                    if not self.values.is_dtype_equal(other):
                        raise TypeError("categorical index comparisions must "
                                        "have the same categories and ordered "
                                        "attributes")

                return getattr(self.values, op)(other)

            return _evaluate_compare

        cls.__eq__ = _make_compare('__eq__')
        cls.__ne__ = _make_compare('__ne__')
        cls.__lt__ = _make_compare('__lt__')
        cls.__gt__ = _make_compare('__gt__')
        cls.__le__ = _make_compare('__le__')
        cls.__ge__ = _make_compare('__ge__') 
Example #14
Source File: range.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def equals(self, other):
        """
        Determines if two Index objects contain the same elements.
        """
        if isinstance(other, RangeIndex):
            ls = len(self)
            lo = len(other)
            return (ls == lo == 0 or
                    ls == lo == 1 and
                    self._start == other._start or
                    ls == lo and
                    self._start == other._start and
                    self._step == other._step)

        return super(RangeIndex, self).equals(other) 
Example #15
Source File: interval.py    From recruit with Apache License 2.0 5 votes vote down vote up
def left(self):
        """
        Return the left endpoints of each Interval in the IntervalArray as
        an Index
        """
        return self._left 
Example #16
Source File: category.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def is_monotonic_decreasing(self):
        return Index(self.codes).is_monotonic_decreasing 
Example #17
Source File: category.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def delete(self, loc):
        """
        Make new Index with passed location(-s) deleted

        Returns
        -------
        new_index : Index
        """
        return self._create_from_codes(np.delete(self.codes, loc)) 
Example #18
Source File: category.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def insert(self, loc, item):
        """
        Make new Index inserting new item at location. Follows
        Python list.append semantics for negative values

        Parameters
        ----------
        loc : int
        item : object

        Returns
        -------
        new_index : Index

        Raises
        ------
        ValueError if the item is not in the categories

        """
        code = self.categories.get_indexer([item])
        if (code == -1):
            raise TypeError("cannot insert an item into a CategoricalIndex "
                            "that is not already an existing category")

        codes = self.codes
        codes = np.concatenate((codes[:loc], code, codes[loc:]))
        return self._create_from_codes(codes) 
Example #19
Source File: category.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def _add_comparison_methods(cls):
        """ add in comparison methods """

        def _make_compare(op):
            def _evaluate_compare(self, other):

                # if we have a Categorical type, then must have the same
                # categories
                if isinstance(other, CategoricalIndex):
                    other = other._values
                elif isinstance(other, Index):
                    other = self._create_categorical(
                        self, other._values, categories=self.categories,
                        ordered=self.ordered)

                if isinstance(other, (ABCCategorical, np.ndarray,
                                      ABCSeries)):
                    if len(self.values) != len(other):
                        raise ValueError("Lengths must match to compare")

                if isinstance(other, ABCCategorical):
                    if not self.values.is_dtype_equal(other):
                        raise TypeError("categorical index comparisions must "
                                        "have the same categories and ordered "
                                        "attributes")

                return getattr(self.values, op)(other)

            return _evaluate_compare

        cls.__eq__ = _make_compare('__eq__')
        cls.__ne__ = _make_compare('__ne__')
        cls.__lt__ = _make_compare('__lt__')
        cls.__gt__ = _make_compare('__gt__')
        cls.__le__ = _make_compare('__le__')
        cls.__ge__ = _make_compare('__ge__') 
Example #20
Source File: range.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def equals(self, other):
        """
        Determines if two Index objects contain the same elements.
        """
        if isinstance(other, RangeIndex):
            ls = len(self)
            lo = len(other)
            return (ls == lo == 0 or
                    ls == lo == 1 and
                    self._start == other._start or
                    ls == lo and
                    self._start == other._start and
                    self._step == other._step)

        return super(RangeIndex, self).equals(other) 
Example #21
Source File: category.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def is_monotonic_increasing(self):
        return Index(self.codes).is_monotonic_increasing 
Example #22
Source File: category.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def is_monotonic_decreasing(self):
        return Index(self.codes).is_monotonic_decreasing 
Example #23
Source File: range.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def equals(self, other):
        """
        Determines if two Index objects contain the same elements.
        """
        if isinstance(other, RangeIndex):
            ls = len(self)
            lo = len(other)
            return (ls == lo == 0 or
                    ls == lo == 1 and
                    self._start == other._start or
                    ls == lo and
                    self._start == other._start and
                    self._step == other._step)

        return super(RangeIndex, self).equals(other) 
Example #24
Source File: interval.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def mid(self):
        """
        Return the midpoint of each Interval in the IntervalArray as an Index
        """
        try:
            return 0.5 * (self.left + self.right)
        except TypeError:
            # datetime safe version
            return self.left + 0.5 * self.length 
Example #25
Source File: interval.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def length(self):
        """
        Return an Index with entries denoting the length of each Interval in
        the IntervalArray
        """
        try:
            return self.right - self.left
        except TypeError:
            # length not defined for some types, e.g. string
            msg = ('IntervalArray contains Intervals without defined length, '
                   'e.g. Intervals with string endpoints')
            raise TypeError(msg) 
Example #26
Source File: interval.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def right(self):
        """
        Return the right endpoints of each Interval in the IntervalArray as
        an Index
        """
        return self._right 
Example #27
Source File: interval.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def left(self):
        """
        Return the left endpoints of each Interval in the IntervalArray as
        an Index
        """
        return self._left 
Example #28
Source File: interval.py    From recruit with Apache License 2.0 5 votes vote down vote up
def __getitem__(self, value):
        left = self.left[value]
        right = self.right[value]

        # scalar
        if not isinstance(left, Index):
            if isna(left):
                return self._fill_value
            return Interval(left, right, self.closed)

        return self._shallow_copy(left, right) 
Example #29
Source File: interval.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def __getitem__(self, value):
        left = self.left[value]
        right = self.right[value]

        # scalar
        if not isinstance(left, Index):
            if isna(left):
                return self._fill_value
            return Interval(left, right, self.closed)

        return self._shallow_copy(left, right) 
Example #30
Source File: interval.py    From recruit with Apache License 2.0 5 votes vote down vote up
def right(self):
        """
        Return the right endpoints of each Interval in the IntervalArray as
        an Index
        """
        return self._right