Python pandas.core.series.Series.unique() Examples
The following are 20
code examples of pandas.core.series.Series.unique().
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 elasticintel with GNU General Public License v3.0 | 6 votes |
def from_array(cls, data, **kwargs): """ .. deprecated:: 0.19.0 Use ``Categorical`` instead. Make a Categorical type from a single array-like object. For internal compatibility with numpy arrays. Parameters ---------- data : array-like Can be an Index or array-like. The categories are assumed to be the unique values of `data`. """ warn("Categorical.from_array is deprecated, use Categorical instead", FutureWarning, stacklevel=2) return cls(data, **kwargs)
Example #2
Source File: categorical.py From Splunking-Crime with GNU Affero General Public License v3.0 | 6 votes |
def from_array(cls, data, **kwargs): """ .. deprecated:: 0.19.0 Use ``Categorical`` instead. Make a Categorical type from a single array-like object. For internal compatibility with numpy arrays. Parameters ---------- data : array-like Can be an Index or array-like. The categories are assumed to be the unique values of `data`. """ warn("Categorical.from_array is deprecated, use Categorical instead", FutureWarning, stacklevel=2) return cls(data, **kwargs)
Example #3
Source File: categorical.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def categories(self): """The categories of this categorical. Setting assigns new values to each category (effectively a rename of each individual category). The assigned value has to be a list-like object. All items must be unique and the number of items in the new categories must be the same as the number of items in the old categories. Assigning to `categories` is a inplace operation! Raises ------ ValueError If the new categories do not validate as categories or if the number of new categories is unequal the number of old categories See also -------- rename_categories reorder_categories add_categories remove_categories remove_unused_categories set_categories """ return self.dtype.categories
Example #4
Source File: categorical.py From elasticintel with GNU General Public License v3.0 | 5 votes |
def remove_unused_categories(self, inplace=False): """ Removes categories which are not used. Parameters ---------- inplace : boolean (default: False) Whether or not to drop unused categories inplace or return a copy of this categorical with unused categories dropped. Returns ------- cat : Categorical with unused categories dropped or None if inplace. See also -------- rename_categories reorder_categories add_categories remove_categories set_categories """ inplace = validate_bool_kwarg(inplace, 'inplace') cat = self if inplace else self.copy() idx, inv = np.unique(cat._codes, return_inverse=True) if idx.size != 0 and idx[0] == -1: # na sentinel idx, inv = idx[1:], inv - 1 new_categories = cat.dtype.categories.take(idx) new_dtype = CategoricalDtype._from_fastpath(new_categories, ordered=self.ordered) cat._dtype = new_dtype cat._codes = coerce_indexer_dtype(inv, new_dtype.categories) if not inplace: return cat
Example #5
Source File: categorical.py From elasticintel with GNU General Public License v3.0 | 5 votes |
def _codes_for_groupby(self, sort): """ If sort=False, return a copy of self, coded with categories as returned by .unique(), followed by any categories not appearing in the data. If sort=True, return self. This method is needed solely to ensure the categorical index of the GroupBy result has categories in the order of appearance in the data (GH-8868). Parameters ---------- sort : boolean The value of the sort paramter groupby was called with. Returns ------- Categorical If sort=False, the new categories are set to the order of appearance in codes (unless ordered=True, in which case the original order is preserved), followed by any unrepresented categories in the original order. """ # Already sorted according to self.categories; all is fine if sort: return self # sort=False should order groups in as-encountered order (GH-8868) cat = self.unique() # But for groupby to work, all categories should be present, # including those missing from the data (GH-13179), which .unique() # above dropped cat.add_categories( self.categories[~self.categories.isin(cat.categories)], inplace=True) return self.reorder_categories(cat.categories)
Example #6
Source File: categorical.py From elasticintel with GNU General Public License v3.0 | 5 votes |
def from_codes(cls, codes, categories, ordered=False): """ Make a Categorical type from codes and categories arrays. This constructor is useful if you already have codes and categories and so do not need the (computation intensive) factorization step, which is usually done on the constructor. If your data does not follow this convention, please use the normal constructor. Parameters ---------- codes : array-like, integers An integer array, where each integer points to a category in categories or -1 for NaN categories : index-like The categories for the categorical. Items need to be unique. ordered : boolean, (default False) Whether or not this categorical is treated as a ordered categorical. If not given, the resulting categorical will be unordered. """ try: codes = np.asarray(codes, np.int64) except: raise ValueError( "codes need to be convertible to an arrays of integers") categories = CategoricalDtype._validate_categories(categories) if len(codes) and (codes.max() >= len(categories) or codes.min() < -1): raise ValueError("codes need to be between -1 and " "len(categories)-1") return cls(codes, categories=categories, ordered=ordered, fastpath=True)
Example #7
Source File: categorical.py From elasticintel with GNU General Public License v3.0 | 5 votes |
def categories(self): """The categories of this categorical. Setting assigns new values to each category (effectively a rename of each individual category). The assigned value has to be a list-like object. All items must be unique and the number of items in the new categories must be the same as the number of items in the old categories. Assigning to `categories` is a inplace operation! Raises ------ ValueError If the new categories do not validate as categories or if the number of new categories is unequal the number of old categories See also -------- rename_categories reorder_categories add_categories remove_categories remove_unused_categories set_categories """ return self.dtype.categories
Example #8
Source File: categorical.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def remove_unused_categories(self, inplace=False): """ Removes categories which are not used. Parameters ---------- inplace : boolean (default: False) Whether or not to drop unused categories inplace or return a copy of this categorical with unused categories dropped. Returns ------- cat : Categorical with unused categories dropped or None if inplace. See also -------- rename_categories reorder_categories add_categories remove_categories set_categories """ inplace = validate_bool_kwarg(inplace, 'inplace') cat = self if inplace else self.copy() idx, inv = np.unique(cat._codes, return_inverse=True) if idx.size != 0 and idx[0] == -1: # na sentinel idx, inv = idx[1:], inv - 1 new_categories = cat.dtype.categories.take(idx) new_dtype = CategoricalDtype._from_fastpath(new_categories, ordered=self.ordered) cat._dtype = new_dtype cat._codes = coerce_indexer_dtype(inv, new_dtype.categories) if not inplace: return cat
Example #9
Source File: categorical.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def _codes_for_groupby(self, sort): """ If sort=False, return a copy of self, coded with categories as returned by .unique(), followed by any categories not appearing in the data. If sort=True, return self. This method is needed solely to ensure the categorical index of the GroupBy result has categories in the order of appearance in the data (GH-8868). Parameters ---------- sort : boolean The value of the sort paramter groupby was called with. Returns ------- Categorical If sort=False, the new categories are set to the order of appearance in codes (unless ordered=True, in which case the original order is preserved), followed by any unrepresented categories in the original order. """ # Already sorted according to self.categories; all is fine if sort: return self # sort=False should order groups in as-encountered order (GH-8868) cat = self.unique() # But for groupby to work, all categories should be present, # including those missing from the data (GH-13179), which .unique() # above dropped cat.add_categories( self.categories[~self.categories.isin(cat.categories)], inplace=True) return self.reorder_categories(cat.categories)
Example #10
Source File: categorical.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def from_codes(cls, codes, categories, ordered=False): """ Make a Categorical type from codes and categories arrays. This constructor is useful if you already have codes and categories and so do not need the (computation intensive) factorization step, which is usually done on the constructor. If your data does not follow this convention, please use the normal constructor. Parameters ---------- codes : array-like, integers An integer array, where each integer points to a category in categories or -1 for NaN categories : index-like The categories for the categorical. Items need to be unique. ordered : boolean, (default False) Whether or not this categorical is treated as a ordered categorical. If not given, the resulting categorical will be unordered. """ try: codes = np.asarray(codes, np.int64) except: raise ValueError( "codes need to be convertible to an arrays of integers") categories = CategoricalDtype._validate_categories(categories) if len(codes) and (codes.max() >= len(categories) or codes.min() < -1): raise ValueError("codes need to be between -1 and " "len(categories)-1") return cls(codes, categories=categories, ordered=ordered, fastpath=True)
Example #11
Source File: categorical.py From recruit with Apache License 2.0 | 5 votes |
def categories(self): """ The categories of this categorical. Setting assigns new values to each category (effectively a rename of each individual category). The assigned value has to be a list-like object. All items must be unique and the number of items in the new categories must be the same as the number of items in the old categories. Assigning to `categories` is a inplace operation! Raises ------ ValueError If the new categories do not validate as categories or if the number of new categories is unequal the number of old categories See Also -------- rename_categories reorder_categories add_categories remove_categories remove_unused_categories set_categories """ return self.dtype.categories
Example #12
Source File: categorical.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def remove_unused_categories(self, inplace=False): """ Removes categories which are not used. Parameters ---------- inplace : boolean (default: False) Whether or not to drop unused categories inplace or return a copy of this categorical with unused categories dropped. Returns ------- cat : Categorical with unused categories dropped or None if inplace. See Also -------- rename_categories reorder_categories add_categories remove_categories set_categories """ inplace = validate_bool_kwarg(inplace, 'inplace') cat = self if inplace else self.copy() idx, inv = np.unique(cat._codes, return_inverse=True) if idx.size != 0 and idx[0] == -1: # na sentinel idx, inv = idx[1:], inv - 1 new_categories = cat.dtype.categories.take(idx) new_dtype = CategoricalDtype._from_fastpath(new_categories, ordered=self.ordered) cat._dtype = new_dtype cat._codes = coerce_indexer_dtype(inv, new_dtype.categories) if not inplace: return cat
Example #13
Source File: categorical.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def categories(self): """ The categories of this categorical. Setting assigns new values to each category (effectively a rename of each individual category). The assigned value has to be a list-like object. All items must be unique and the number of items in the new categories must be the same as the number of items in the old categories. Assigning to `categories` is a inplace operation! Raises ------ ValueError If the new categories do not validate as categories or if the number of new categories is unequal the number of old categories See Also -------- rename_categories reorder_categories add_categories remove_categories remove_unused_categories set_categories """ return self.dtype.categories
Example #14
Source File: categorical.py From vnpy_crypto with MIT License | 5 votes |
def remove_unused_categories(self, inplace=False): """ Removes categories which are not used. Parameters ---------- inplace : boolean (default: False) Whether or not to drop unused categories inplace or return a copy of this categorical with unused categories dropped. Returns ------- cat : Categorical with unused categories dropped or None if inplace. See also -------- rename_categories reorder_categories add_categories remove_categories set_categories """ inplace = validate_bool_kwarg(inplace, 'inplace') cat = self if inplace else self.copy() idx, inv = np.unique(cat._codes, return_inverse=True) if idx.size != 0 and idx[0] == -1: # na sentinel idx, inv = idx[1:], inv - 1 new_categories = cat.dtype.categories.take(idx) new_dtype = CategoricalDtype._from_fastpath(new_categories, ordered=self.ordered) cat._dtype = new_dtype cat._codes = coerce_indexer_dtype(inv, new_dtype.categories) if not inplace: return cat
Example #15
Source File: categorical.py From vnpy_crypto with MIT License | 5 votes |
def from_codes(cls, codes, categories, ordered=False): """ Make a Categorical type from codes and categories arrays. This constructor is useful if you already have codes and categories and so do not need the (computation intensive) factorization step, which is usually done on the constructor. If your data does not follow this convention, please use the normal constructor. Parameters ---------- codes : array-like, integers An integer array, where each integer points to a category in categories or -1 for NaN categories : index-like The categories for the categorical. Items need to be unique. ordered : boolean, (default False) Whether or not this categorical is treated as a ordered categorical. If not given, the resulting categorical will be unordered. """ try: codes = coerce_indexer_dtype(np.asarray(codes), categories) except (ValueError, TypeError): raise ValueError( "codes need to be convertible to an arrays of integers") categories = CategoricalDtype.validate_categories(categories) if len(codes) and (codes.max() >= len(categories) or codes.min() < -1): raise ValueError("codes need to be between -1 and " "len(categories)-1") return cls(codes, categories=categories, ordered=ordered, fastpath=True)
Example #16
Source File: categorical.py From vnpy_crypto with MIT License | 5 votes |
def categories(self): """The categories of this categorical. Setting assigns new values to each category (effectively a rename of each individual category). The assigned value has to be a list-like object. All items must be unique and the number of items in the new categories must be the same as the number of items in the old categories. Assigning to `categories` is a inplace operation! Raises ------ ValueError If the new categories do not validate as categories or if the number of new categories is unequal the number of old categories See also -------- rename_categories reorder_categories add_categories remove_categories remove_unused_categories set_categories """ return self.dtype.categories
Example #17
Source File: categorical.py From recruit with Apache License 2.0 | 5 votes |
def remove_unused_categories(self, inplace=False): """ Removes categories which are not used. Parameters ---------- inplace : boolean (default: False) Whether or not to drop unused categories inplace or return a copy of this categorical with unused categories dropped. Returns ------- cat : Categorical with unused categories dropped or None if inplace. See Also -------- rename_categories reorder_categories add_categories remove_categories set_categories """ inplace = validate_bool_kwarg(inplace, 'inplace') cat = self if inplace else self.copy() idx, inv = np.unique(cat._codes, return_inverse=True) if idx.size != 0 and idx[0] == -1: # na sentinel idx, inv = idx[1:], inv - 1 new_categories = cat.dtype.categories.take(idx) new_dtype = CategoricalDtype._from_fastpath(new_categories, ordered=self.ordered) cat._dtype = new_dtype cat._codes = coerce_indexer_dtype(inv, new_dtype.categories) if not inplace: return cat
Example #18
Source File: categorical.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 4 votes |
def unique(self): """ Return the ``Categorical`` which ``categories`` and ``codes`` are unique. Unused categories are NOT returned. - unordered category: values and categories are sorted by appearance order. - ordered category: values are sorted by appearance order, categories keeps existing order. Returns ------- unique values : ``Categorical`` Examples -------- An unordered Categorical will return categories in the order of appearance. >>> pd.Categorical(list('baabc')) [b, a, c] Categories (3, object): [b, a, c] >>> pd.Categorical(list('baabc'), categories=list('abc')) [b, a, c] Categories (3, object): [b, a, c] An ordered Categorical preserves the category ordering. >>> pd.Categorical(list('baabc'), ... categories=list('abc'), ... ordered=True) [b, a, c] Categories (3, object): [a < b < c] See Also -------- unique CategoricalIndex.unique Series.unique """ # unlike np.unique, unique1d does not sort unique_codes = unique1d(self.codes) cat = self.copy() # keep nan in codes cat._codes = unique_codes # exclude nan from indexer for categories take_codes = unique_codes[unique_codes != -1] if self.ordered: take_codes = np.sort(take_codes) return cat.set_categories(cat.categories.take(take_codes))
Example #19
Source File: categorical.py From vnpy_crypto with MIT License | 4 votes |
def unique(self): """ Return the ``Categorical`` which ``categories`` and ``codes`` are unique. Unused categories are NOT returned. - unordered category: values and categories are sorted by appearance order. - ordered category: values are sorted by appearance order, categories keeps existing order. Returns ------- unique values : ``Categorical`` Examples -------- An unordered Categorical will return categories in the order of appearance. >>> pd.Categorical(list('baabc')) [b, a, c] Categories (3, object): [b, a, c] >>> pd.Categorical(list('baabc'), categories=list('abc')) [b, a, c] Categories (3, object): [b, a, c] An ordered Categorical preserves the category ordering. >>> pd.Categorical(list('baabc'), ... categories=list('abc'), ... ordered=True) [b, a, c] Categories (3, object): [a < b < c] See Also -------- unique CategoricalIndex.unique Series.unique """ # unlike np.unique, unique1d does not sort unique_codes = unique1d(self.codes) cat = self.copy() # keep nan in codes cat._codes = unique_codes # exclude nan from indexer for categories take_codes = unique_codes[unique_codes != -1] if self.ordered: take_codes = np.sort(take_codes) return cat.set_categories(cat.categories.take(take_codes))
Example #20
Source File: categorical.py From recruit with Apache License 2.0 | 4 votes |
def unique(self): """ Return the ``Categorical`` which ``categories`` and ``codes`` are unique. Unused categories are NOT returned. - unordered category: values and categories are sorted by appearance order. - ordered category: values are sorted by appearance order, categories keeps existing order. Returns ------- unique values : ``Categorical`` Examples -------- An unordered Categorical will return categories in the order of appearance. >>> pd.Categorical(list('baabc')) [b, a, c] Categories (3, object): [b, a, c] >>> pd.Categorical(list('baabc'), categories=list('abc')) [b, a, c] Categories (3, object): [b, a, c] An ordered Categorical preserves the category ordering. >>> pd.Categorical(list('baabc'), ... categories=list('abc'), ... ordered=True) [b, a, c] Categories (3, object): [a < b < c] See Also -------- unique CategoricalIndex.unique Series.unique """ # unlike np.unique, unique1d does not sort unique_codes = unique1d(self.codes) cat = self.copy() # keep nan in codes cat._codes = unique_codes # exclude nan from indexer for categories take_codes = unique_codes[unique_codes != -1] if self.ordered: take_codes = np.sort(take_codes) return cat.set_categories(cat.categories.take(take_codes))