Python pandas.Series.cat() Examples
The following are 30
code examples of pandas.Series.cat().
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.Series
, or try the search function
.
Example #1
Source File: categorical.py From recruit with Apache License 2.0 | 6 votes |
def set_ordered(self, value, inplace=False): """ Sets the ordered attribute to the boolean value Parameters ---------- value : boolean to set whether this categorical is ordered (True) or not (False) inplace : boolean (default: False) Whether or not to set the ordered attribute inplace or return a copy of this categorical with ordered set to the value """ inplace = validate_bool_kwarg(inplace, 'inplace') new_dtype = CategoricalDtype(self.categories, ordered=value) cat = self if inplace else self.copy() cat._dtype = new_dtype if not inplace: return cat
Example #2
Source File: categorical.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 6 votes |
def set_ordered(self, value, inplace=False): """ Sets the ordered attribute to the boolean value Parameters ---------- value : boolean to set whether this categorical is ordered (True) or not (False) inplace : boolean (default: False) Whether or not to set the ordered attribute inplace or return a copy of this categorical with ordered set to the value """ inplace = validate_bool_kwarg(inplace, 'inplace') new_dtype = CategoricalDtype(self.categories, ordered=value) cat = self if inplace else self.copy() cat._dtype = new_dtype if not inplace: return cat
Example #3
Source File: categorical.py From elasticintel with GNU General Public License v3.0 | 6 votes |
def set_ordered(self, value, inplace=False): """ Sets the ordered attribute to the boolean value Parameters ---------- value : boolean to set whether this categorical is ordered (True) or not (False) inplace : boolean (default: False) Whether or not to set the ordered attribute inplace or return a copy of this categorical with ordered set to the value """ inplace = validate_bool_kwarg(inplace, 'inplace') new_dtype = CategoricalDtype(self.categories, ordered=value) cat = self if inplace else self.copy() cat._dtype = new_dtype if not inplace: return cat
Example #4
Source File: categorical.py From Splunking-Crime with GNU Affero General Public License v3.0 | 6 votes |
def set_ordered(self, value, inplace=False): """ Sets the ordered attribute to the boolean value Parameters ---------- value : boolean to set whether this categorical is ordered (True) or not (False) inplace : boolean (default: False) Whether or not to set the ordered attribute inplace or return a copy of this categorical with ordered set to the value """ inplace = validate_bool_kwarg(inplace, 'inplace') new_dtype = CategoricalDtype(self.categories, ordered=value) cat = self if inplace else self.copy() cat._dtype = new_dtype if not inplace: return cat
Example #5
Source File: categorical.py From recruit with Apache License 2.0 | 5 votes |
def index(self): # Note: Upon deprecation, `test_tab_completion_with_categorical` will # need to be updated. `index` will need to be removed from # ok_for_cat`. warn("`Series.cat.index` has been deprecated. Use `Series.index` " "instead.", FutureWarning, stacklevel=2) return self._index # utility routines
Example #6
Source File: test_api.py From twitter-stock-recommendation with MIT License | 5 votes |
def test_cat_accessor_no_new_attributes(self): # https://github.com/pandas-dev/pandas/issues/10673 c = Series(list('aabbcde')).astype('category') with tm.assert_raises_regex(AttributeError, "You cannot add any new attribute"): c.cat.xlabel = "a"
Example #7
Source File: categorical.py From recruit with Apache License 2.0 | 5 votes |
def name(self): # Note: Upon deprecation, `test_tab_completion_with_categorical` will # need to be updated. `name` will need to be removed from # `ok_for_cat`. warn("`Series.cat.name` has been deprecated. Use `Series.name` " "instead.", FutureWarning, stacklevel=2) return self._name
Example #8
Source File: test_api.py From vnpy_crypto with MIT License | 5 votes |
def test_cat_accessor_api(self): # GH 9322 from pandas.core.arrays.categorical import CategoricalAccessor assert Series.cat is CategoricalAccessor s = Series(list('aabbcde')).astype('category') assert isinstance(s.cat, CategoricalAccessor) invalid = Series([1]) with tm.assert_raises_regex(AttributeError, "only use .cat accessor"): invalid.cat assert not hasattr(invalid, 'cat')
Example #9
Source File: test_api.py From vnpy_crypto with MIT License | 5 votes |
def test_cat_accessor_no_new_attributes(self): # https://github.com/pandas-dev/pandas/issues/10673 c = Series(list('aabbcde')).astype('category') with tm.assert_raises_regex(AttributeError, "You cannot add any new attribute"): c.cat.xlabel = "a"
Example #10
Source File: categorical.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def value_counts(self, dropna=True): """ Returns a Series containing counts of each category. Every category will have an entry, even those with a count of 0. Parameters ---------- dropna : boolean, default True Don't include counts of NaN. Returns ------- counts : Series See Also -------- Series.value_counts """ from numpy import bincount from pandas import Series, CategoricalIndex code, cat = self._codes, self.categories ncat, mask = len(cat), 0 <= code ix, clean = np.arange(ncat), mask.all() if dropna or clean: obs = code if clean else code[mask] count = bincount(obs, minlength=ncat or None) else: count = bincount(np.where(mask, code, ncat)) ix = np.append(ix, -1) ix = self._constructor(ix, dtype=self.dtype, fastpath=True) return Series(count, index=CategoricalIndex(ix), dtype='int64')
Example #11
Source File: categorical.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def _validate(data): if not is_categorical_dtype(data.dtype): raise AttributeError("Can only use .cat accessor with a " "'category' dtype")
Example #12
Source File: categorical.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def name(self): # Note: Upon deprecation, `test_tab_completion_with_categorical` will # need to be updated. `name` will need to be removed from # `ok_for_cat`. warn("`Series.cat.name` has been deprecated. Use `Series.name` " "instead.", FutureWarning, stacklevel=2) return self._name
Example #13
Source File: categorical.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def index(self): # Note: Upon deprecation, `test_tab_completion_with_categorical` will # need to be updated. `index` will need to be removed from # ok_for_cat`. warn("`Series.cat.index` has been deprecated. Use `Series.index` " "instead.", FutureWarning, stacklevel=2) return self._index # utility routines
Example #14
Source File: categorical.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def _factorize_from_iterable(values): """ Factorize an input `values` into `categories` and `codes`. Preserves categorical dtype in `categories`. *This is an internal function* Parameters ---------- values : list-like Returns ------- codes : ndarray categories : Index If `values` has a categorical dtype, then `categories` is a CategoricalIndex keeping the categories and order of `values`. """ from pandas.core.indexes.category import CategoricalIndex if not is_list_like(values): raise TypeError("Input must be list-like") if is_categorical(values): if isinstance(values, (ABCCategoricalIndex, ABCSeries)): values = values._values categories = CategoricalIndex(values.categories, categories=values.categories, ordered=values.ordered) codes = values.codes else: # The value of ordered is irrelevant since we don't use cat as such, # but only the resulting categories, the order of which is independent # from ordered. Set ordered to False as default. See GH #15457 cat = Categorical(values, ordered=False) categories = cat.categories codes = cat.codes return codes, categories
Example #15
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 #16
Source File: categorical.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def reorder_categories(self, new_categories, ordered=None, inplace=False): """ Reorders categories as specified in new_categories. `new_categories` need to include all old categories and no new category items. Raises ------ ValueError If the new categories do not contain all old category items or any new ones Parameters ---------- new_categories : Index-like The categories in new order. ordered : boolean, optional Whether or not the categorical is treated as a ordered categorical. If not given, do not change the ordered information. inplace : boolean (default: False) Whether or not to reorder the categories inplace or return a copy of this categorical with reordered categories. Returns ------- cat : Categorical with reordered categories or None if inplace. See also -------- rename_categories add_categories remove_categories remove_unused_categories set_categories """ inplace = validate_bool_kwarg(inplace, 'inplace') if set(self.dtype.categories) != set(new_categories): raise ValueError("items in new_categories are not the same as in " "old categories") return self.set_categories(new_categories, ordered=ordered, inplace=inplace)
Example #17
Source File: categorical.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def value_counts(self, dropna=True): """ Returns a Series containing counts of each category. Every category will have an entry, even those with a count of 0. Parameters ---------- dropna : boolean, default True Don't include counts of NaN, even if NaN is a category. Returns ------- counts : Series See Also -------- Series.value_counts """ from numpy import bincount from pandas import isna, Series, CategoricalIndex obj = (self.remove_categories([np.nan]) if dropna and isna(self.categories).any() else self) code, cat = obj._codes, obj.categories ncat, mask = len(cat), 0 <= code ix, clean = np.arange(ncat), mask.all() if dropna or clean: obs = code if clean else code[mask] count = bincount(obs, minlength=ncat or None) else: count = bincount(np.where(mask, code, ncat)) ix = np.append(ix, -1) ix = self._constructor(ix, dtype=self.dtype, fastpath=True) return Series(count, index=CategoricalIndex(ix), dtype='int64')
Example #18
Source File: categorical.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def repeat(self, repeats, *args, **kwargs): """ Repeat elements of a Categorical. See also -------- numpy.ndarray.repeat """ nv.validate_repeat(args, kwargs) codes = self._codes.repeat(repeats) return self._constructor(values=codes, categories=self.categories, ordered=self.ordered, fastpath=True) # The Series.cat accessor
Example #19
Source File: categorical.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def _make_accessor(cls, data): if not is_categorical_dtype(data.dtype): raise AttributeError("Can only use .cat accessor with a " "'category' dtype") return CategoricalAccessor(data.values, data.index, getattr(data, 'name', None),)
Example #20
Source File: categorical.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def _factorize_from_iterable(values): """ Factorize an input `values` into `categories` and `codes`. Preserves categorical dtype in `categories`. *This is an internal function* Parameters ---------- values : list-like Returns ------- codes : ndarray categories : Index If `values` has a categorical dtype, then `categories` is a CategoricalIndex keeping the categories and order of `values`. """ from pandas.core.indexes.category import CategoricalIndex if not is_list_like(values): raise TypeError("Input must be list-like") if is_categorical(values): if isinstance(values, (ABCCategoricalIndex, ABCSeries)): values = values._values categories = CategoricalIndex(values.categories, categories=values.categories, ordered=values.ordered) codes = values.codes else: cat = Categorical(values, ordered=True) categories = cat.categories codes = cat.codes return codes, categories
Example #21
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 #22
Source File: categorical.py From elasticintel with GNU General Public License v3.0 | 5 votes |
def reorder_categories(self, new_categories, ordered=None, inplace=False): """ Reorders categories as specified in new_categories. `new_categories` need to include all old categories and no new category items. Raises ------ ValueError If the new categories do not contain all old category items or any new ones Parameters ---------- new_categories : Index-like The categories in new order. ordered : boolean, optional Whether or not the categorical is treated as a ordered categorical. If not given, do not change the ordered information. inplace : boolean (default: False) Whether or not to reorder the categories inplace or return a copy of this categorical with reordered categories. Returns ------- cat : Categorical with reordered categories or None if inplace. See also -------- rename_categories add_categories remove_categories remove_unused_categories set_categories """ inplace = validate_bool_kwarg(inplace, 'inplace') if set(self.dtype.categories) != set(new_categories): raise ValueError("items in new_categories are not the same as in " "old categories") return self.set_categories(new_categories, ordered=ordered, inplace=inplace)
Example #23
Source File: categorical.py From elasticintel with GNU General Public License v3.0 | 5 votes |
def value_counts(self, dropna=True): """ Returns a Series containing counts of each category. Every category will have an entry, even those with a count of 0. Parameters ---------- dropna : boolean, default True Don't include counts of NaN, even if NaN is a category. Returns ------- counts : Series See Also -------- Series.value_counts """ from numpy import bincount from pandas import isna, Series, CategoricalIndex obj = (self.remove_categories([np.nan]) if dropna and isna(self.categories).any() else self) code, cat = obj._codes, obj.categories ncat, mask = len(cat), 0 <= code ix, clean = np.arange(ncat), mask.all() if dropna or clean: obs = code if clean else code[mask] count = bincount(obs, minlength=ncat or None) else: count = bincount(np.where(mask, code, ncat)) ix = np.append(ix, -1) ix = self._constructor(ix, dtype=self.dtype, fastpath=True) return Series(count, index=CategoricalIndex(ix), dtype='int64')
Example #24
Source File: categorical.py From elasticintel with GNU General Public License v3.0 | 5 votes |
def repeat(self, repeats, *args, **kwargs): """ Repeat elements of a Categorical. See also -------- numpy.ndarray.repeat """ nv.validate_repeat(args, kwargs) codes = self._codes.repeat(repeats) return self._constructor(values=codes, categories=self.categories, ordered=self.ordered, fastpath=True) # The Series.cat accessor
Example #25
Source File: categorical.py From elasticintel with GNU General Public License v3.0 | 5 votes |
def _make_accessor(cls, data): if not is_categorical_dtype(data.dtype): raise AttributeError("Can only use .cat accessor with a " "'category' dtype") return CategoricalAccessor(data.values, data.index, getattr(data, 'name', None),)
Example #26
Source File: categorical.py From elasticintel with GNU General Public License v3.0 | 5 votes |
def _factorize_from_iterable(values): """ Factorize an input `values` into `categories` and `codes`. Preserves categorical dtype in `categories`. *This is an internal function* Parameters ---------- values : list-like Returns ------- codes : ndarray categories : Index If `values` has a categorical dtype, then `categories` is a CategoricalIndex keeping the categories and order of `values`. """ from pandas.core.indexes.category import CategoricalIndex if not is_list_like(values): raise TypeError("Input must be list-like") if is_categorical(values): if isinstance(values, (ABCCategoricalIndex, ABCSeries)): values = values._values categories = CategoricalIndex(values.categories, categories=values.categories, ordered=values.ordered) codes = values.codes else: cat = Categorical(values, ordered=True) categories = cat.categories codes = cat.codes return codes, categories
Example #27
Source File: test_api.py From twitter-stock-recommendation with MIT License | 5 votes |
def test_tab_completion(self): # GH 9910 s = Series(list('abcd')) # Series of str values should have .str but not .dt/.cat in __dir__ assert 'str' in dir(s) assert 'dt' not in dir(s) assert 'cat' not in dir(s) # similarly for .dt s = Series(date_range('1/1/2015', periods=5)) assert 'dt' in dir(s) assert 'str' not in dir(s) assert 'cat' not in dir(s) # Similarly for .cat, but with the twist that str and dt should be # there if the categories are of that type first cat and str. s = Series(list('abbcd'), dtype="category") assert 'cat' in dir(s) assert 'str' in dir(s) # as it is a string categorical assert 'dt' not in dir(s) # similar to cat and str s = Series(date_range('1/1/2015', periods=5)).astype("category") assert 'cat' in dir(s) assert 'str' not in dir(s) assert 'dt' in dir(s) # as it is a datetime categorical
Example #28
Source File: test_api.py From twitter-stock-recommendation with MIT License | 5 votes |
def test_tab_completion_with_categorical(self): # test the tab completion display ok_for_cat = ['categories', 'codes', 'ordered', 'set_categories', 'add_categories', 'remove_categories', 'rename_categories', 'reorder_categories', 'remove_unused_categories', 'as_ordered', 'as_unordered'] def get_dir(s): results = [r for r in s.cat.__dir__() if not r.startswith('_')] return list(sorted(set(results))) s = Series(list('aabbcde')).astype('category') results = get_dir(s) tm.assert_almost_equal(results, list(sorted(set(ok_for_cat))))
Example #29
Source File: test_api.py From twitter-stock-recommendation with MIT License | 5 votes |
def test_cat_accessor(self): s = Series(Categorical(["a", "b", np.nan, "a"])) tm.assert_index_equal(s.cat.categories, Index(["a", "b"])) assert not s.cat.ordered, False exp = Categorical(["a", "b", np.nan, "a"], categories=["b", "a"]) s.cat.set_categories(["b", "a"], inplace=True) tm.assert_categorical_equal(s.values, exp) res = s.cat.set_categories(["b", "a"]) tm.assert_categorical_equal(res.values, exp) s[:] = "a" s = s.cat.remove_unused_categories() tm.assert_index_equal(s.cat.categories, Index(["a"]))
Example #30
Source File: test_api.py From twitter-stock-recommendation with MIT License | 5 votes |
def test_cat_accessor_api(self): # GH 9322 from pandas.core.arrays.categorical import CategoricalAccessor assert Series.cat is CategoricalAccessor s = Series(list('aabbcde')).astype('category') assert isinstance(s.cat, CategoricalAccessor) invalid = Series([1]) with tm.assert_raises_regex(AttributeError, "only use .cat accessor"): invalid.cat assert not hasattr(invalid, 'cat')