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

Function _split_partition

dask/dataframe/multi.py:296–349  ·  view source on GitHub ↗

Split-by-hash a DataFrame into `nsplits` groups. Hashing will be performed on the columns or index specified by `on`.

(df, on, nsplits)

Source from the content-addressed store, hash-verified

294
295
296def _split_partition(df, on, nsplits):
297 """
298 Split-by-hash a DataFrame into `nsplits` groups.
299
300 Hashing will be performed on the columns or index specified by `on`.
301 """
302
303 if isinstance(on, bytes):
304 on = pickle.loads(on)
305
306 if isinstance(on, str) or pd.api.types.is_list_like(on):
307 # If `on` is a column name or list of column names, we
308 # can hash/split by those columns.
309 on = [on] if isinstance(on, str) else list(on)
310 nset = set(on)
311 if nset.intersection(set(df.columns)) == nset:
312 o = df[on]
313 dtypes = {}
314 for col, dtype in o.dtypes.items():
315 if pd.api.types.is_numeric_dtype(dtype):
316 dtypes[col] = np.float64
317 if not dtypes:
318 ind = hash_object_dispatch(df[on], index=False)
319 else:
320 ind = hash_object_dispatch(df[on].astype(dtypes), index=False)
321
322 ind = ind % nsplits
323 return group_split_dispatch(df, ind, nsplits, ignore_index=False)
324
325 # We are not joining (purely) on columns. Need to
326 # add a "_partitions" column to perform the split.
327 from dask.dataframe.dask_expr._collection import FrameBase
328
329 if not isinstance(on, FrameBase):
330 on = _select_columns_or_index(df, on)
331
332 dtypes = {}
333 for col, dtype in on.dtypes.items():
334 if pd.api.types.is_numeric_dtype(dtype):
335 dtypes[col] = np.float64
336 if not dtypes:
337 dtypes = None
338
339 partitions = partitioning_index(on, nsplits, cast_dtype=dtypes)
340 df2 = df.assign(_partitions=partitions)
341 return shuffle_group(
342 df2,
343 ["_partitions"],
344 0,
345 nsplits,
346 nsplits,
347 False,
348 nsplits,
349 )
350
351
352def _concat_wrapper(dfs):

Callers

nothing calls this directly

Calls 7

partitioning_indexFunction · 0.90
shuffle_groupFunction · 0.90
setClass · 0.85
assignMethod · 0.80
_select_columns_or_indexFunction · 0.70
itemsMethod · 0.45
astypeMethod · 0.45

Tested by

no test coverage detected