MCPcopy
hub / github.com/pandas-dev/pandas / difference

Method difference

pandas/core/indexes/base.py:3401–3470  ·  view source on GitHub ↗

Return a new Index with elements of index not in `other`. This is the set difference of two Index objects. Parameters ---------- other : Index or array-like Index object or an array-like object containing elements to be compared with

(self, other, sort: bool | None = None)

Source from the content-addressed store, hash-verified

3399
3400 @final
3401 def difference(self, other, sort: bool | None = None):
3402 """
3403 Return a new Index with elements of index not in `other`.
3404
3405 This is the set difference of two Index objects.
3406
3407 Parameters
3408 ----------
3409 other : Index or array-like
3410 Index object or an array-like object containing elements to be compared
3411 with the elements of the original Index.
3412 sort : bool or None, default None
3413 Whether to sort the resulting index. By default, the
3414 values are attempted to be sorted, but any TypeError from
3415 incomparable elements is caught by pandas.
3416
3417 * None : Attempt to sort the result, but catch any TypeErrors
3418 from comparing incomparable elements.
3419 * False : Do not sort the result.
3420 * True : Sort the result (which may raise TypeError).
3421
3422 Returns
3423 -------
3424 Index
3425 Returns a new Index object containing elements that are in the original
3426 Index but not in the `other` Index.
3427
3428 See Also
3429 --------
3430 Index.symmetric_difference : Compute the symmetric difference of two Index
3431 objects.
3432 Index.intersection : Form the intersection of two Index objects.
3433
3434 Examples
3435 --------
3436 >>> idx1 = pd.Index([2, 1, 3, 4])
3437 >>> idx2 = pd.Index([3, 4, 5, 6])
3438 >>> idx1.difference(idx2)
3439 Index([1, 2], dtype='int64')
3440 >>> idx1.difference(idx2, sort=False)
3441 Index([2, 1], dtype='int64')
3442 """
3443 self._validate_sort_keyword(sort)
3444 self._assert_can_do_setop(other)
3445 other, result_name = self._convert_can_do_setop(other)
3446
3447 # Note: we do NOT call _dti_setop_align_tzs here, as there
3448 # is no requirement that .difference be commutative, so it does
3449 # not cast to object.
3450
3451 if self.equals(other):
3452 # Note: we do not (yet) sort even if sort=None GH#24959
3453 return self[:0].rename(result_name)
3454
3455 if len(other) == 0:
3456 # Note: we do not (yet) sort even if sort=None GH#24959
3457 result = self.unique().rename(result_name)
3458 if sort is True:

Calls 10

_assert_can_do_setopMethod · 0.95
_convert_can_do_setopMethod · 0.95
equalsMethod · 0.95
uniqueMethod · 0.95
_should_compareMethod · 0.95
_differenceMethod · 0.95
renameMethod · 0.45
sort_valuesMethod · 0.45