MCPcopy Create free account
hub / github.com/dask/dask / isin

Method isin

dask/dataframe/dask_expr/_collection.py:729–781  ·  view source on GitHub ↗
(self, values)

Source from the content-addressed store, hash-verified

727
728 @derived_from(pd.DataFrame)
729 def isin(self, values):
730 if isinstance(self, DataFrame):
731 # DataFrame.isin does weird alignment stuff
732 bad_types = (FrameBase, pd.Series, pd.DataFrame)
733 else:
734 bad_types = (FrameBase,)
735 if isinstance(values, bad_types):
736 if (
737 isinstance(values, FrameBase)
738 and values.ndim == 1
739 and values.npartitions == 1
740 ):
741 # Can broadcast
742 return new_collection(expr.Isin(self, values=values))
743 raise NotImplementedError(f"Passing a {typename(type(values))!r} to `isin`")
744
745 # We wrap values in a delayed for two reasons:
746 # - avoid serializing data in every task
747 # - avoid cost of traversal of large list in optimizations
748 if isinstance(values, list):
749 # Motivated by https://github.com/dask/dask/issues/9411. This appears to be
750 # caused by https://github.com/dask/distributed/issues/6368, and further
751 # exacerbated by the fact that the list contains duplicates. This is a patch until
752 # we can create a better fix for Serialization.
753 try:
754 values = list(set(values))
755 except TypeError:
756 pass
757 if not any(is_dask_collection(v) for v in values):
758 # Avoid always passing a numpy array of object dtype
759 inferred_type = pd.api.types.infer_dtype(values, skipna=False)
760 object_like = {
761 "mixed-integer",
762 "decimal",
763 "categorical",
764 "time",
765 "period",
766 "mixed",
767 "unknown-array",
768 }
769 if inferred_type in object_like:
770 values = np.fromiter(values, dtype=object, count=len(values))
771 else:
772 values = np.asarray(values)
773
774 return new_collection(
775 expr.Isin(
776 self,
777 values=expr._DelayedExpr(
778 delayed(values, name="delayed-" + _tokenize_deterministic(values))
779 ),
780 )
781 )
782
783 def _partitions(self, index):
784 # Used by `partitions` for partition-wise slicing

Callers 15

_non_agg_chunkFunction · 0.80
_resample_seriesFunction · 0.80
_metaMethod · 0.80
test_merge_leftsemiFunction · 0.80
test_rewrite_filtersFunction · 0.80
test_isinFunction · 0.80
test_isin_reprFunction · 0.80
test_isin_as_predicateFunction · 0.80
test_isin_headFunction · 0.80

Calls 7

new_collectionFunction · 0.90
typenameFunction · 0.90
is_dask_collectionFunction · 0.90
delayedFunction · 0.90
_tokenize_deterministicFunction · 0.90
setClass · 0.85
anyFunction · 0.85

Tested by 13

test_merge_leftsemiFunction · 0.64
test_rewrite_filtersFunction · 0.64
test_isinFunction · 0.64
test_isin_reprFunction · 0.64
test_isin_as_predicateFunction · 0.64
test_isin_headFunction · 0.64
test_isin_stringsFunction · 0.64
test_isinFunction · 0.64
test_isin_randFunction · 0.64