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

Function _normalize_spec

dask/dataframe/groupby.py:610–693  ·  view source on GitHub ↗

Return a list of ``(result_column, func, input_column)`` tuples. Spec can be - a function - a list of functions - a dictionary that maps input-columns to functions - a dictionary that maps input-columns to a lists of functions - a dictionary that maps input-columns to

(spec, non_group_columns)

Source from the content-addressed store, hash-verified

608
609
610def _normalize_spec(spec, non_group_columns):
611 """
612 Return a list of ``(result_column, func, input_column)`` tuples.
613
614 Spec can be
615
616 - a function
617 - a list of functions
618 - a dictionary that maps input-columns to functions
619 - a dictionary that maps input-columns to a lists of functions
620 - a dictionary that maps input-columns to a dictionaries that map
621 output-columns to functions.
622
623 The non-group columns are a list of all column names that are not used in
624 the groupby operation.
625
626 Usually, the result columns are mutli-level names, returned as tuples.
627 If only a single function is supplied or dictionary mapping columns
628 to single functions, simple names are returned as strings (see the first
629 two examples below).
630
631 Examples
632 --------
633 >>> _normalize_spec('mean', ['a', 'b', 'c'])
634 [('a', 'mean', 'a'), ('b', 'mean', 'b'), ('c', 'mean', 'c')]
635
636 >>> spec = collections.OrderedDict([('a', 'mean'), ('b', 'count')])
637 >>> _normalize_spec(spec, ['a', 'b', 'c'])
638 [('a', 'mean', 'a'), ('b', 'count', 'b')]
639
640 >>> _normalize_spec(['var', 'mean'], ['a', 'b', 'c'])
641 ... # doctest: +NORMALIZE_WHITESPACE
642 [(('a', 'var'), 'var', 'a'), (('a', 'mean'), 'mean', 'a'), \
643 (('b', 'var'), 'var', 'b'), (('b', 'mean'), 'mean', 'b'), \
644 (('c', 'var'), 'var', 'c'), (('c', 'mean'), 'mean', 'c')]
645
646 >>> spec = collections.OrderedDict([('a', 'mean'), ('b', ['sum', 'count'])])
647 >>> _normalize_spec(spec, ['a', 'b', 'c'])
648 ... # doctest: +NORMALIZE_WHITESPACE
649 [(('a', 'mean'), 'mean', 'a'), (('b', 'sum'), 'sum', 'b'), \
650 (('b', 'count'), 'count', 'b')]
651
652 >>> spec = collections.OrderedDict()
653 >>> spec['a'] = ['mean', 'size']
654 >>> spec['b'] = collections.OrderedDict([('e', 'count'), ('f', 'var')])
655 >>> _normalize_spec(spec, ['a', 'b', 'c'])
656 ... # doctest: +NORMALIZE_WHITESPACE
657 [(('a', 'mean'), 'mean', 'a'), (('a', 'size'), 'size', 'a'), \
658 (('b', 'e'), 'count', 'b'), (('b', 'f'), 'var', 'b')]
659 """
660 if not isinstance(spec, dict):
661 spec = collections.OrderedDict(zip(non_group_columns, it.repeat(spec)))
662
663 res = []
664
665 if isinstance(spec, dict):
666 for input_column, subspec in spec.items():
667 if isinstance(subspec, dict):

Callers 1

specMethod · 0.90

Calls 5

funcnameFunction · 0.90
anyFunction · 0.85
repeatMethod · 0.80
itemsMethod · 0.45
valuesMethod · 0.45

Tested by

no test coverage detected