Python pandas.core.series.Series.sort_values() Examples

The following are 7 code examples of pandas.core.series.Series.sort_values(). 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.series.Series , or try the search function .
Example #1
Source File: categorical.py    From Splunking-Crime with GNU Affero General Public License v3.0 6 votes vote down vote up
def argsort(self, ascending=True, kind='quicksort', *args, **kwargs):
        """
        Returns the indices that would sort the Categorical instance if
        'sort_values' was called. This function is implemented to provide
        compatibility with numpy ndarray objects.

        While an ordering is applied to the category values, arg-sorting
        in this context refers more to organizing and grouping together
        based on matching category values. Thus, this function can be
        called on an unordered Categorical instance unlike the functions
        'Categorical.min' and 'Categorical.max'.

        Returns
        -------
        argsorted : numpy array

        See also
        --------
        numpy.ndarray.argsort
        """
        ascending = nv.validate_argsort_with_ascending(ascending, args, kwargs)
        result = np.argsort(self._codes.copy(), kind=kind, **kwargs)
        if not ascending:
            result = result[::-1]
        return result 
Example #2
Source File: categorical.py    From elasticintel with GNU General Public License v3.0 6 votes vote down vote up
def argsort(self, ascending=True, kind='quicksort', *args, **kwargs):
        """
        Returns the indices that would sort the Categorical instance if
        'sort_values' was called. This function is implemented to provide
        compatibility with numpy ndarray objects.

        While an ordering is applied to the category values, arg-sorting
        in this context refers more to organizing and grouping together
        based on matching category values. Thus, this function can be
        called on an unordered Categorical instance unlike the functions
        'Categorical.min' and 'Categorical.max'.

        Returns
        -------
        argsorted : numpy array

        See also
        --------
        numpy.ndarray.argsort
        """
        ascending = nv.validate_argsort_with_ascending(ascending, args, kwargs)
        result = np.argsort(self._codes.copy(), kind=kind, **kwargs)
        if not ascending:
            result = result[::-1]
        return result 
Example #3
Source File: categorical.py    From recruit with Apache License 2.0 4 votes vote down vote up
def _from_inferred_categories(cls, inferred_categories, inferred_codes,
                                  dtype, true_values=None):
        """
        Construct a Categorical from inferred values.

        For inferred categories (`dtype` is None) the categories are sorted.
        For explicit `dtype`, the `inferred_categories` are cast to the
        appropriate type.

        Parameters
        ----------
        inferred_categories : Index
        inferred_codes : Index
        dtype : CategoricalDtype or 'category'
        true_values : list, optional
            If none are provided, the default ones are
            "True", "TRUE", and "true."

        Returns
        -------
        Categorical
        """
        from pandas import Index, to_numeric, to_datetime, to_timedelta

        cats = Index(inferred_categories)
        known_categories = (isinstance(dtype, CategoricalDtype) and
                            dtype.categories is not None)

        if known_categories:
            # Convert to a specialized type with `dtype` if specified.
            if dtype.categories.is_numeric():
                cats = to_numeric(inferred_categories, errors="coerce")
            elif is_datetime64_dtype(dtype.categories):
                cats = to_datetime(inferred_categories, errors="coerce")
            elif is_timedelta64_dtype(dtype.categories):
                cats = to_timedelta(inferred_categories, errors="coerce")
            elif dtype.categories.is_boolean():
                if true_values is None:
                    true_values = ["True", "TRUE", "true"]

                cats = cats.isin(true_values)

        if known_categories:
            # Recode from observation order to dtype.categories order.
            categories = dtype.categories
            codes = _recode_for_categories(inferred_codes, cats, categories)
        elif not cats.is_monotonic_increasing:
            # Sort categories and recode for unknown categories.
            unsorted = cats.copy()
            categories = cats.sort_values()

            codes = _recode_for_categories(inferred_codes, unsorted,
                                           categories)
            dtype = CategoricalDtype(categories, ordered=False)
        else:
            dtype = CategoricalDtype(cats, ordered=False)
            codes = inferred_codes

        return cls(codes, dtype=dtype, fastpath=True) 
Example #4
Source File: categorical.py    From vnpy_crypto with MIT License 4 votes vote down vote up
def _from_inferred_categories(cls, inferred_categories, inferred_codes,
                                  dtype):
        """Construct a Categorical from inferred values

        For inferred categories (`dtype` is None) the categories are sorted.
        For explicit `dtype`, the `inferred_categories` are cast to the
        appropriate type.

        Parameters
        ----------

        inferred_categories : Index
        inferred_codes : Index
        dtype : CategoricalDtype or 'category'

        Returns
        -------
        Categorical
        """
        from pandas import Index, to_numeric, to_datetime, to_timedelta

        cats = Index(inferred_categories)

        known_categories = (isinstance(dtype, CategoricalDtype) and
                            dtype.categories is not None)

        if known_categories:
            # Convert to a specialzed type with `dtype` if specified
            if dtype.categories.is_numeric():
                cats = to_numeric(inferred_categories, errors='coerce')
            elif is_datetime64_dtype(dtype.categories):
                cats = to_datetime(inferred_categories, errors='coerce')
            elif is_timedelta64_dtype(dtype.categories):
                cats = to_timedelta(inferred_categories, errors='coerce')

        if known_categories:
            # recode from observation order to dtype.categories order
            categories = dtype.categories
            codes = _recode_for_categories(inferred_codes, cats, categories)
        elif not cats.is_monotonic_increasing:
            # sort categories and recode for unknown categories
            unsorted = cats.copy()
            categories = cats.sort_values()
            codes = _recode_for_categories(inferred_codes, unsorted,
                                           categories)
            dtype = CategoricalDtype(categories, ordered=False)
        else:
            dtype = CategoricalDtype(cats, ordered=False)
            codes = inferred_codes

        return cls(codes, dtype=dtype, fastpath=True) 
Example #5
Source File: categorical.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 4 votes vote down vote up
def _from_inferred_categories(cls, inferred_categories, inferred_codes,
                                  dtype, true_values=None):
        """
        Construct a Categorical from inferred values.

        For inferred categories (`dtype` is None) the categories are sorted.
        For explicit `dtype`, the `inferred_categories` are cast to the
        appropriate type.

        Parameters
        ----------
        inferred_categories : Index
        inferred_codes : Index
        dtype : CategoricalDtype or 'category'
        true_values : list, optional
            If none are provided, the default ones are
            "True", "TRUE", and "true."

        Returns
        -------
        Categorical
        """
        from pandas import Index, to_numeric, to_datetime, to_timedelta

        cats = Index(inferred_categories)
        known_categories = (isinstance(dtype, CategoricalDtype) and
                            dtype.categories is not None)

        if known_categories:
            # Convert to a specialized type with `dtype` if specified.
            if dtype.categories.is_numeric():
                cats = to_numeric(inferred_categories, errors="coerce")
            elif is_datetime64_dtype(dtype.categories):
                cats = to_datetime(inferred_categories, errors="coerce")
            elif is_timedelta64_dtype(dtype.categories):
                cats = to_timedelta(inferred_categories, errors="coerce")
            elif dtype.categories.is_boolean():
                if true_values is None:
                    true_values = ["True", "TRUE", "true"]

                cats = cats.isin(true_values)

        if known_categories:
            # Recode from observation order to dtype.categories order.
            categories = dtype.categories
            codes = _recode_for_categories(inferred_codes, cats, categories)
        elif not cats.is_monotonic_increasing:
            # Sort categories and recode for unknown categories.
            unsorted = cats.copy()
            categories = cats.sort_values()

            codes = _recode_for_categories(inferred_codes, unsorted,
                                           categories)
            dtype = CategoricalDtype(categories, ordered=False)
        else:
            dtype = CategoricalDtype(cats, ordered=False)
            codes = inferred_codes

        return cls(codes, dtype=dtype, fastpath=True) 
Example #6
Source File: categorical.py    From Splunking-Crime with GNU Affero General Public License v3.0 4 votes vote down vote up
def _from_inferred_categories(cls, inferred_categories, inferred_codes,
                                  dtype):
        """Construct a Categorical from inferred values

        For inferred categories (`dtype` is None) the categories are sorted.
        For explicit `dtype`, the `inferred_categories` are cast to the
        appropriate type.

        Parameters
        ----------

        inferred_categories : Index
        inferred_codes : Index
        dtype : CategoricalDtype or 'category'

        Returns
        -------
        Categorical
        """
        from pandas import Index, to_numeric, to_datetime, to_timedelta

        cats = Index(inferred_categories)

        known_categories = (isinstance(dtype, CategoricalDtype) and
                            dtype.categories is not None)

        if known_categories:
            # Convert to a specialzed type with `dtype` if specified
            if dtype.categories.is_numeric():
                cats = to_numeric(inferred_categories, errors='coerce')
            elif is_datetime64_dtype(dtype.categories):
                cats = to_datetime(inferred_categories, errors='coerce')
            elif is_timedelta64_dtype(dtype.categories):
                cats = to_timedelta(inferred_categories, errors='coerce')

        if known_categories:
            # recode from observation oder to dtype.categories order
            categories = dtype.categories
            codes = _recode_for_categories(inferred_codes, cats, categories)
        elif not cats.is_monotonic_increasing:
            # sort categories and recode for unknown categories
            unsorted = cats.copy()
            categories = cats.sort_values()
            codes = _recode_for_categories(inferred_codes, unsorted,
                                           categories)
            dtype = CategoricalDtype(categories, ordered=False)
        else:
            dtype = CategoricalDtype(cats, ordered=False)
            codes = inferred_codes

        return cls(codes, dtype=dtype, fastpath=True) 
Example #7
Source File: categorical.py    From elasticintel with GNU General Public License v3.0 4 votes vote down vote up
def _from_inferred_categories(cls, inferred_categories, inferred_codes,
                                  dtype):
        """Construct a Categorical from inferred values

        For inferred categories (`dtype` is None) the categories are sorted.
        For explicit `dtype`, the `inferred_categories` are cast to the
        appropriate type.

        Parameters
        ----------

        inferred_categories : Index
        inferred_codes : Index
        dtype : CategoricalDtype or 'category'

        Returns
        -------
        Categorical
        """
        from pandas import Index, to_numeric, to_datetime, to_timedelta

        cats = Index(inferred_categories)

        known_categories = (isinstance(dtype, CategoricalDtype) and
                            dtype.categories is not None)

        if known_categories:
            # Convert to a specialzed type with `dtype` if specified
            if dtype.categories.is_numeric():
                cats = to_numeric(inferred_categories, errors='coerce')
            elif is_datetime64_dtype(dtype.categories):
                cats = to_datetime(inferred_categories, errors='coerce')
            elif is_timedelta64_dtype(dtype.categories):
                cats = to_timedelta(inferred_categories, errors='coerce')

        if known_categories:
            # recode from observation oder to dtype.categories order
            categories = dtype.categories
            codes = _recode_for_categories(inferred_codes, cats, categories)
        elif not cats.is_monotonic_increasing:
            # sort categories and recode for unknown categories
            unsorted = cats.copy()
            categories = cats.sort_values()
            codes = _recode_for_categories(inferred_codes, unsorted,
                                           categories)
            dtype = CategoricalDtype(categories, ordered=False)
        else:
            dtype = CategoricalDtype(cats, ordered=False)
            codes = inferred_codes

        return cls(codes, dtype=dtype, fastpath=True)