本文整理匯總了Python中pandas.unique方法的典型用法代碼示例。如果您正苦於以下問題:Python pandas.unique方法的具體用法?Python pandas.unique怎麽用?Python pandas.unique使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pandas
的用法示例。
在下文中一共展示了pandas.unique方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_timedelta64_dtype_array_returned
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import unique [as 別名]
def test_timedelta64_dtype_array_returned(self):
# GH 9431
expected = np.array([31200, 45678, 10000], dtype='m8[ns]')
td_index = pd.to_timedelta([31200, 45678, 31200, 10000, 45678])
result = algos.unique(td_index)
tm.assert_numpy_array_equal(result, expected)
assert result.dtype == expected.dtype
s = Series(td_index)
result = algos.unique(s)
tm.assert_numpy_array_equal(result, expected)
assert result.dtype == expected.dtype
arr = s.values
result = algos.unique(arr)
tm.assert_numpy_array_equal(result, expected)
assert result.dtype == expected.dtype
示例2: test_first_nan_kept
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import unique [as 別名]
def test_first_nan_kept(self):
# GH 22295
# create different nans from bit-patterns:
bits_for_nan1 = 0xfff8000000000001
bits_for_nan2 = 0x7ff8000000000001
NAN1 = struct.unpack("d", struct.pack("=Q", bits_for_nan1))[0]
NAN2 = struct.unpack("d", struct.pack("=Q", bits_for_nan2))[0]
assert NAN1 != NAN1
assert NAN2 != NAN2
for el_type in [np.float64, np.object]:
a = np.array([NAN1, NAN2], dtype=el_type)
result = pd.unique(a)
assert result.size == 1
# use bit patterns to identify which nan was kept:
result_nan_bits = struct.unpack("=Q",
struct.pack("d", result[0]))[0]
assert result_nan_bits == bits_for_nan1
示例3: test_unpivot
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import unique [as 別名]
def test_unpivot():
frame = pd.DataFrame({'x': range(10), 'y': range(10), 'z': range(10)})
df = unpivot_frame(frame, var_name='foo', value_name='bar')
assert list(df.columns) == ['index', 'foo', 'bar']
assert set(pd.unique(df['foo'])) == {'x', 'y', 'z'}
df = unpivot_frame(frame, x='x')
assert list(df.columns) == ['x', 'variable', 'value']
assert set(pd.unique(df['variable'])) == {'y', 'z'}
df = unpivot_frame(frame, y='y')
assert list(df.columns) == ['index', 'variable', 'value']
assert set(pd.unique(df['variable'])) == {'y'}
df = unpivot_frame(frame, y=('y', 'z'))
assert list(df.columns) == ['index', 'variable', 'value']
assert set(pd.unique(df['variable'])) == {'y', 'z'}
df = unpivot_frame(frame, x=('x', 'y'), y='z')
assert list(df.columns) == ['x', 'y', 'variable', 'value']
assert set(pd.unique(df['variable'])) == {'z'}
示例4: test_qcut_binning_issues
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import unique [as 別名]
def test_qcut_binning_issues(self, datapath):
# #1978, 1979
cut_file = datapath(os.path.join('reshape', 'data', 'cut_data.csv'))
arr = np.loadtxt(cut_file)
result = qcut(arr, 20)
starts = []
ends = []
for lev in np.unique(result):
s = lev.left
e = lev.right
assert s != e
starts.append(float(s))
ends.append(float(e))
for (sp, sn), (ep, en) in zip(zip(starts[:-1], starts[1:]),
zip(ends[:-1], ends[1:])):
assert sp < sn
assert ep < en
assert ep <= sn
示例5: test_datetime64_dtype_array_returned
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import unique [as 別名]
def test_datetime64_dtype_array_returned(self):
# GH 9431
expected = np_array_datetime64_compat(
['2015-01-03T00:00:00.000000000+0000',
'2015-01-01T00:00:00.000000000+0000'],
dtype='M8[ns]')
dt_index = pd.to_datetime(['2015-01-03T00:00:00.000000000+0000',
'2015-01-01T00:00:00.000000000+0000',
'2015-01-01T00:00:00.000000000+0000'])
result = algos.unique(dt_index)
tm.assert_numpy_array_equal(result, expected)
assert result.dtype == expected.dtype
s = Series(dt_index)
result = algos.unique(s)
tm.assert_numpy_array_equal(result, expected)
assert result.dtype == expected.dtype
arr = s.values
result = algos.unique(arr)
tm.assert_numpy_array_equal(result, expected)
assert result.dtype == expected.dtype
示例6: test_cut_duplicates_bin
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import unique [as 別名]
def test_cut_duplicates_bin(kwargs, msg):
# see gh-20947
bins = [0, 2, 4, 6, 10, 10]
values = Series(np.array([1, 3, 5, 7, 9]), index=["a", "b", "c", "d", "e"])
if msg is not None:
with pytest.raises(ValueError, match=msg):
cut(values, bins, **kwargs)
else:
result = cut(values, bins, **kwargs)
expected = cut(values, pd.unique(bins))
tm.assert_series_equal(result, expected)
示例7: test_ints
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import unique [as 別名]
def test_ints(self):
arr = np.random.randint(0, 100, size=50)
result = algos.unique(arr)
assert isinstance(result, np.ndarray)
示例8: test_objects
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import unique [as 別名]
def test_objects(self):
arr = np.random.randint(0, 100, size=50).astype('O')
result = algos.unique(arr)
assert isinstance(result, np.ndarray)
示例9: test_object_refcount_bug
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import unique [as 別名]
def test_object_refcount_bug(self):
lst = ['A', 'B', 'C', 'D', 'E']
for i in range(1000):
len(algos.unique(lst))
示例10: test_on_index_object
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import unique [as 別名]
def test_on_index_object(self):
mindex = pd.MultiIndex.from_arrays([np.arange(5).repeat(5), np.tile(
np.arange(5), 5)])
expected = mindex.values
expected.sort()
mindex = mindex.repeat(2)
result = pd.unique(mindex)
result.sort()
tm.assert_almost_equal(result, expected)
示例11: test_uint64_overflow
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import unique [as 別名]
def test_uint64_overflow(self):
s = Series([1, 2, 2**63, 2**63], dtype=np.uint64)
exp = np.array([1, 2, 2**63], dtype=np.uint64)
tm.assert_numpy_array_equal(algos.unique(s), exp)
示例12: test_nan_in_object_array
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import unique [as 別名]
def test_nan_in_object_array(self):
duplicated_items = ['a', np.nan, 'c', 'c']
result = pd.unique(duplicated_items)
expected = np.array(['a', np.nan, 'c'], dtype=object)
tm.assert_numpy_array_equal(result, expected)
示例13: test_datetime64tz_aware
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import unique [as 別名]
def test_datetime64tz_aware(self):
# GH 15939
result = Series(
Index([Timestamp('20160101', tz='US/Eastern'),
Timestamp('20160101', tz='US/Eastern')])).unique()
expected = DatetimeArray._from_sequence(np.array([
Timestamp('2016-01-01 00:00:00-0500', tz="US/Eastern")
]))
tm.assert_extension_array_equal(result, expected)
result = Index([Timestamp('20160101', tz='US/Eastern'),
Timestamp('20160101', tz='US/Eastern')]).unique()
expected = DatetimeIndex(['2016-01-01 00:00:00'],
dtype='datetime64[ns, US/Eastern]', freq=None)
tm.assert_index_equal(result, expected)
result = pd.unique(
Series(Index([Timestamp('20160101', tz='US/Eastern'),
Timestamp('20160101', tz='US/Eastern')])))
expected = DatetimeArray._from_sequence(np.array([
Timestamp('2016-01-01', tz="US/Eastern"),
]))
tm.assert_extension_array_equal(result, expected)
result = pd.unique(Index([Timestamp('20160101', tz='US/Eastern'),
Timestamp('20160101', tz='US/Eastern')]))
expected = DatetimeIndex(['2016-01-01 00:00:00'],
dtype='datetime64[ns, US/Eastern]', freq=None)
tm.assert_index_equal(result, expected)
示例14: test_tuple_with_strings
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import unique [as 別名]
def test_tuple_with_strings(self, arg, expected):
# see GH 17108
result = pd.unique(arg)
tm.assert_numpy_array_equal(result, expected)
示例15: test_obj_none_preservation
# 需要導入模塊: import pandas [as 別名]
# 或者: from pandas import unique [as 別名]
def test_obj_none_preservation(self):
# GH 20866
arr = np.array(['foo', None], dtype=object)
result = pd.unique(arr)
expected = np.array(['foo', None], dtype=object)
tm.assert_numpy_array_equal(result, expected, strict_nan=True)