Categorical ,当输入是 Categorical dtype 时#
- pandas.unique(values)[源代码]#
ndarray,当输入是 Series/ndarray 时
返回 numpy.ndarray 或 ExtensionArray。
从 Index 返回唯一值。
- Parameters:
- values无序的 Categorical 将按出现顺序返回类别。
- Returns:
- numpy.ndarray 或 ExtensionArray
返回值可以是:
有序的 Categorical 会保留类别顺序。
元组数组
pandas.util.hash_array
给定一个一维数组,返回一个确定性整数数组。
参见
Index.uniquevals
Series.unique返回 Series 对象中的唯一值。
Examples
>>> pd.unique(pd.Series([2, 1, 3, 3])) array([2, 1, 3])
>>> pd.unique(pd.Series([2] + [1] * 5)) array([2, 1])
>>> pd.unique(pd.Series([pd.Timestamp("20160101"), pd.Timestamp("20160101")])) array(['2016-01-01T00:00:00.000000000'], dtype='datetime64[ns]')
>>> pd.unique( ... pd.Series( ... [ ... pd.Timestamp("20160101", tz="US/Eastern"), ... pd.Timestamp("20160101", tz="US/Eastern"), ... ] ... ) ... ) <DatetimeArray> ['2016-01-01 00:00:00-05:00'] Length: 1, dtype: datetime64[ns, US/Eastern]
>>> pd.unique( ... pd.Index( ... [ ... pd.Timestamp("20160101", tz="US/Eastern"), ... pd.Timestamp("20160101", tz="US/Eastern"), ... ] ... ) ... ) DatetimeIndex(['2016-01-01 00:00:00-05:00'], dtype='datetime64[ns, US/Eastern]', freq=None)
>>> pd.unique(np.array(list("baabc"), dtype="O")) array(['b', 'a', 'c'], dtype=object)
str,默认 ‘utf8’
>>> pd.unique(pd.Series(pd.Categorical(list("baabc")))) ['b', 'a', 'c'] Categories (3, object): ['a', 'b', 'c']
>>> pd.unique(pd.Series(pd.Categorical(list("baabc"), categories=list("abc")))) ['b', 'a', 'c'] Categories (3, object): ['a', 'b', 'c']
str,默认 _default_hash_key
>>> pd.unique( ... pd.Series( ... pd.Categorical(list("baabc"), categories=list("abc"), ordered=True) ... ) ... ) ['b', 'a', 'c'] Categories (3, object): ['a' < 'b' < 'c']
ndarray[np.uint64, ndim=1]
>>> pd.unique(pd.Series([("a", "b"), ("b", "a"), ("a", "c"), ("b", "a")]).values) array([('a', 'b'), ('b', 'a'), ('a', 'c')], dtype=object)