MCPcopy Create free account
hub / github.com/numpy/numpy / find_duplicates

Function find_duplicates

numpy/lib/recfunctions.py:1411–1465  ·  view source on GitHub ↗

Find the duplicates in a structured array along a given key Parameters ---------- a : array-like Input array key : {string, None}, optional Name of the fields along which to check the duplicates. If None, the search is performed by records ignoremask

(a, key=None, ignoremask=True, return_index=False)

Source from the content-addressed store, hash-verified

1409
1410@array_function_dispatch(_find_duplicates_dispatcher)
1411def find_duplicates(a, key=None, ignoremask=True, return_index=False):
1412 """
1413 Find the duplicates in a structured array along a given key
1414
1415 Parameters
1416 ----------
1417 a : array-like
1418 Input array
1419 key : {string, None}, optional
1420 Name of the fields along which to check the duplicates.
1421 If None, the search is performed by records
1422 ignoremask : {True, False}, optional
1423 Whether masked data should be discarded or considered as duplicates.
1424 return_index : {False, True}, optional
1425 Whether to return the indices of the duplicated values.
1426
1427 Examples
1428 --------
1429 >>> from numpy.lib import recfunctions as rfn
1430 >>> ndtype = [('a', int)]
1431 >>> a = np.ma.array([1, 1, 1, 2, 2, 3, 3],
1432 ... mask=[0, 0, 1, 0, 0, 0, 1]).view(ndtype)
1433 >>> rfn.find_duplicates(a, ignoremask=True, return_index=True)
1434 (masked_array(data=[(1,), (1,), (2,), (2,)],
1435 mask=[(False,), (False,), (False,), (False,)],
1436 fill_value=(999999,),
1437 dtype=[('a', '<i8')]), array([0, 1, 3, 4]))
1438 """
1439 a = np.asanyarray(a).ravel()
1440 # Get a dictionary of fields
1441 fields = get_fieldstructure(a.dtype)
1442 # Get the sorting data (by selecting the corresponding field)
1443 base = a
1444 if key:
1445 for f in fields[key]:
1446 base = base[f]
1447 base = base[key]
1448 # Get the sorting indices and the sorted data
1449 sortidx = base.argsort()
1450 sortedbase = base[sortidx]
1451 sorteddata = sortedbase.filled()
1452 # Compare the sorting data
1453 flag = (sorteddata[:-1] == sorteddata[1:])
1454 # If masked data must be ignored, set the flag to false where needed
1455 if ignoremask:
1456 sortedmask = sortedbase.recordmask
1457 flag[sortedmask[1:]] = False
1458 flag = np.concatenate(([False], flag))
1459 # We need to take the point on the left as well (else we're missing it)
1460 flag[:-1] = flag[:-1] + flag[1:]
1461 duplicates = a[sortidx][flag]
1462 if return_index:
1463 return (duplicates, sortidx[flag])
1464 else:
1465 return duplicates
1466
1467
1468def _join_by_dispatcher(

Callers 2

test_find_duplicatesMethod · 0.90

Calls 4

get_fieldstructureFunction · 0.85
ravelMethod · 0.45
argsortMethod · 0.45
filledMethod · 0.45

Tested by 2

test_find_duplicatesMethod · 0.72