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

Method assign

dask/dataframe/dask_expr/_collection.py:2839–2880  ·  view source on GitHub ↗
(self, **pairs)

Source from the content-addressed store, hash-verified

2837
2838 @derived_from(pd.DataFrame)
2839 def assign(self, **pairs):
2840 result = self
2841 args = []
2842 for k, v in pairs.items():
2843 v = _maybe_from_pandas([v])[0]
2844 if not isinstance(k, str):
2845 raise TypeError(f"Column name cannot be type {type(k)}")
2846
2847 if callable(v):
2848 result = new_collection(expr.Assign(result, *args))
2849 args = []
2850 result = new_collection(expr.Assign(result, k, v(result)))
2851 continue
2852
2853 elif isinstance(v, (Scalar, Series)):
2854 if isinstance(v, Series):
2855 if not expr.are_co_aligned(result.expr, v.expr):
2856 if len(args) > 0:
2857 result = expr.Assign(result, *args)
2858 args = []
2859 result = new_collection(expr.AssignAlign(result, k, v.expr))
2860 continue
2861
2862 elif not isinstance(v, FrameBase) and isinstance(v, Hashable):
2863 pass
2864 elif isinstance(v, Array):
2865 if len(v.shape) > 1:
2866 raise ValueError("Array assignment only supports 1-D arrays")
2867 if v.npartitions != result.npartitions:
2868 raise ValueError(
2869 "Number of partitions do not match "
2870 f"({v.npartitions} != {result.npartitions})"
2871 )
2872 v = from_dask_array(v, index=result.index, meta=result._meta)
2873 else:
2874 raise TypeError(f"Column assignment doesn't support type {type(v)}")
2875 args.extend([k, v])
2876
2877 if len(args) > 0:
2878 result = new_collection(expr.Assign(result, *args))
2879
2880 return result
2881
2882 @derived_from(pd.DataFrame)
2883 def clip(self, lower=None, upper=None, axis=None, **kwargs):

Calls 4

_maybe_from_pandasFunction · 0.90
new_collectionFunction · 0.90
from_dask_arrayFunction · 0.90
itemsMethod · 0.45