本文整理汇总了Python中pandas.core.algorithms.factorize函数的典型用法代码示例。如果您正苦于以下问题:Python factorize函数的具体用法?Python factorize怎么用?Python factorize使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了factorize函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: from_array
def from_array(cls, data):
try:
labels, levels, _ = factorize(data, sort=True)
except TypeError:
labels, levels, _ = factorize(data, sort=False)
return Factor(labels, levels)
示例2: from_array
def from_array(cls, data):
try:
labels, levels, _ = factorize(data, sort=True)
except TypeError:
labels, levels, _ = factorize(data, sort=False)
return Categorical(labels, levels, name=getattr(data, "name", None))
示例3: test_datelike
def test_datelike(self):
# M8
v1 = pd.Timestamp('20130101 09:00:00.00004')
v2 = pd.Timestamp('20130101')
x = Series([v1, v1, v1, v2, v2, v1])
labels, uniques = algos.factorize(x)
self.assert_numpy_array_equal(labels, np.array(
[0, 0, 0, 1, 1, 0], dtype=np.int64))
self.assert_numpy_array_equal(uniques, np.array(
[v1.value, v2.value], dtype='M8[ns]'))
labels, uniques = algos.factorize(x, sort=True)
self.assert_numpy_array_equal(labels, np.array(
[1, 1, 1, 0, 0, 1], dtype=np.int64))
self.assert_numpy_array_equal(uniques, np.array(
[v2.value, v1.value], dtype='M8[ns]'))
# period
v1 = pd.Period('201302', freq='M')
v2 = pd.Period('201303', freq='M')
x = Series([v1, v1, v1, v2, v2, v1])
# periods are not 'sorted' as they are converted back into an index
labels, uniques = algos.factorize(x)
self.assert_numpy_array_equal(labels, np.array(
[0, 0, 0, 1, 1, 0], dtype=np.int64))
self.assert_numpy_array_equal(uniques, pd.PeriodIndex([v1, v2]))
labels, uniques = algos.factorize(x, sort=True)
self.assert_numpy_array_equal(labels, np.array(
[0, 0, 0, 1, 1, 0], dtype=np.int64))
self.assert_numpy_array_equal(uniques, pd.PeriodIndex([v1, v2]))
示例4: from_array
def from_array(cls, data):
from pandas.core.algorithms import factorize
try:
labels, levels, _ = factorize(data, sort=True)
except TypeError:
labels, levels, _ = factorize(data, sort=False)
return Factor(labels, levels)
示例5: from_array
def from_array(cls, data):
if isinstance(data, Index) and hasattr(data, 'factorize'):
labels, levels = data.factorize()
else:
try:
labels, levels = factorize(data, sort=True)
except TypeError:
labels, levels = factorize(data, sort=False)
return Categorical(labels, levels,
name=getattr(data, 'name', None))
示例6: test_mixed
def test_mixed(self):
# doc example reshaping.rst
x = Series(['A', 'A', np.nan, 'B', 3.14, np.inf])
labels, uniques = algos.factorize(x)
self.assert_numpy_array_equal(labels, np.array([ 0, 0, -1, 1, 2, 3],dtype=np.int64))
self.assert_numpy_array_equal(uniques, np.array(['A', 'B', 3.14, np.inf], dtype=object))
labels, uniques = algos.factorize(x, sort=True)
self.assert_numpy_array_equal(labels, np.array([ 2, 2, -1, 3, 0, 1],dtype=np.int64))
self.assert_numpy_array_equal(uniques, np.array([3.14, np.inf, 'A', 'B'], dtype=object))
示例7: __new__
def __new__(cls, data):
from pandas.core.index import _ensure_index
from pandas.core.algorithms import factorize
try:
labels, levels, _ = factorize(data, sort=True)
except TypeError:
labels, levels, _ = factorize(data, sort=False)
labels = labels.view(Factor)
labels.levels = _ensure_index(levels)
return labels
示例8: __init__
def __init__(self, labels, levels=None, name=None):
if levels is None:
if name is None:
name = getattr(labels, 'name', None)
try:
labels, levels = factorize(labels, sort=True)
except TypeError:
labels, levels = factorize(labels, sort=False)
self.labels = labels
self.levels = levels
self.name = name
示例9: __init__
def __init__(self, labels, levels=None, name=None):
if levels is None:
if name is None:
name = getattr(labels, 'name', None)
if isinstance(labels, Index) and hasattr(labels, 'factorize'):
labels, levels = labels.factorize()
else:
try:
labels, levels = factorize(labels, sort=True)
except TypeError:
labels, levels = factorize(labels, sort=False)
self.labels = labels
self.levels = levels
self.name = name
示例10: test_mixed
def test_mixed(self):
# doc example reshaping.rst
x = Series(['A', 'A', np.nan, 'B', 3.14, np.inf])
labels, uniques = algos.factorize(x)
exp = np.array([0, 0, -1, 1, 2, 3], dtype=np.int_)
self.assert_numpy_array_equal(labels, exp)
exp = pd.Index(['A', 'B', 3.14, np.inf])
tm.assert_index_equal(uniques, exp)
labels, uniques = algos.factorize(x, sort=True)
exp = np.array([2, 2, -1, 3, 0, 1], dtype=np.int_)
self.assert_numpy_array_equal(labels, exp)
exp = pd.Index([3.14, np.inf, 'A', 'B'])
tm.assert_index_equal(uniques, exp)
示例11: factorize
def factorize(self):
"""
Specialized factorize that boxes uniques
"""
from pandas.core.algorithms import factorize
labels, uniques = factorize(self.values)
uniques = PeriodIndex(ordinal=uniques, freq=self.freq)
return labels, uniques
示例12: _make_labels
def _make_labels(self):
if self._was_factor: # pragma: no cover
raise Exception('Should not call this method grouping by level')
else:
labs, uniques, counts = algos.factorize(self.grouper,
sort=self.sort)
uniques = Index(uniques, name=self.name)
self._labels = labs
self._group_index = uniques
self._counts = counts
示例13: from_array
def from_array(cls, data):
"""
Make a Categorical type from a single array-like object.
Parameters
----------
data : array-like
Can be an Index or array-like. The levels are assumed to be
the unique values of `data`.
"""
if isinstance(data, Index) and hasattr(data, "factorize"):
labels, levels = data.factorize()
else:
try:
labels, levels = factorize(data, sort=True)
except TypeError:
labels, levels = factorize(data, sort=False)
return Categorical(labels, levels, name=getattr(data, "name", None))
示例14: _make_labels
def _make_labels(self):
if self._labels is None or self._group_index is None:
# we have a list of groupers
if isinstance(self.grouper, BaseGrouper):
labels = self.grouper.label_info
uniques = self.grouper.result_index
else:
labels, uniques = algorithms.factorize(
self.grouper, sort=self.sort)
uniques = Index(uniques, name=self.name)
self._labels = labels
self._group_index = uniques
示例15: test_basic
def test_basic(self):
labels, uniques = algos.factorize(["a", "b", "b", "a", "a", "c", "c", "c"])
# self.assert_numpy_array_equal(labels, np.array([ 0, 1, 1, 0, 0, 2, 2, 2],dtype=np.int64))
self.assert_numpy_array_equal(uniques, np.array(["a", "b", "c"], dtype=object))
labels, uniques = algos.factorize(["a", "b", "b", "a", "a", "c", "c", "c"], sort=True)
self.assert_numpy_array_equal(labels, np.array([0, 1, 1, 0, 0, 2, 2, 2], dtype=np.int64))
self.assert_numpy_array_equal(uniques, np.array(["a", "b", "c"], dtype=object))
labels, uniques = algos.factorize(list(reversed(range(5))))
self.assert_numpy_array_equal(labels, np.array([0, 1, 2, 3, 4], dtype=np.int64))
self.assert_numpy_array_equal(uniques, np.array([4, 3, 2, 1, 0], dtype=np.int64))
labels, uniques = algos.factorize(list(reversed(range(5))), sort=True)
self.assert_numpy_array_equal(labels, np.array([4, 3, 2, 1, 0], dtype=np.int64))
self.assert_numpy_array_equal(uniques, np.array([0, 1, 2, 3, 4], dtype=np.int64))
labels, uniques = algos.factorize(list(reversed(np.arange(5.0))))
self.assert_numpy_array_equal(labels, np.array([0.0, 1.0, 2.0, 3.0, 4.0], dtype=np.float64))
self.assert_numpy_array_equal(uniques, np.array([4, 3, 2, 1, 0], dtype=np.int64))
labels, uniques = algos.factorize(list(reversed(np.arange(5.0))), sort=True)
self.assert_numpy_array_equal(labels, np.array([4, 3, 2, 1, 0], dtype=np.int64))
self.assert_numpy_array_equal(uniques, np.array([0.0, 1.0, 2.0, 3.0, 4.0], dtype=np.float64))