from clumper.sequence import *

A collection of functions to be used in mutate/map-verbs.

expanding

This stateful function can be used to expand a key into a large list containing all the seen values.

Parameters

Name Type Description Default
key the key to apply the smoothing to None

Usage:

from clumper import Clumper
from clumper.sequence import expanding

list_dicts = [
    {'a': 1, 'b': 2},
    {'a': 2, 'b': 3},
    {'a': 3},
    {'a': 4}
]

(Clumper(list_dicts)
  .mutate(r=expanding(key='a'))
  .collect())

impute

This stateful function can be used to calculate row numbers. Uses exponential smoothing.

Parameters

Name Type Description Default
key the key to apply the smoothing to required
strategy the strategy to apply 'prev'
fallback if the strategy fails, what value to use None

Usage:

from clumper import Clumper
from clumper.sequence import impute

list_dicts = [
    {'a': 1, 'b': 2},
    {'a': 2, 'b': 3},
    {'a': 3},
    {'a': 4, 'b': 6},
    {'a': 5},
]

(Clumper(list_dicts)
  .mutate(b=impute('b', strategy='prev'),
          c=lambda d: d['a'] + d['b'])
  .collect())

(Clumper(list_dicts)
  .mutate(b=impute('b', strategy='value', fallback=0))
  .collect())

rolling

This stateful function can be used to create a moving window over a key.

Parameters

Name Type Description Default
key the key to apply the smoothing to None
window the size of the window to create 5

Usage:

from clumper import Clumper
from clumper.sequence import rolling

list_dicts = [
    {'a': 1, 'b': 2},
    {'a': 2, 'b': 3},
    {'a': 3},
    {'a': 4}
]

(Clumper(list_dicts)
  .mutate(r=rolling(window=2, key='a'))
  .collect())

row_number

This stateful function can be used to calculate row numbers.

Usage:

from clumper import Clumper
from clumper.sequence import row_number

list_dicts = [
    {'a': 1, 'b': 2},
    {'a': 2, 'b': 3},
    {'a': 3},
    {'a': 4}
]

(Clumper(list_dicts)
  .mutate(r=row_number())
  .collect())

smoothing

This stateful function can be used to calculate row numbers. Uses exponential smoothing.

Parameters

Name Type Description Default
key the key to apply the smoothing to None
weight exponential smoothing parameter, if 1.0 then we don't listen to the past anymore 0.5

Usage:

from clumper import Clumper
from clumper.sequence import smoothing

list_dicts = [
    {'a': 1, 'b': 2},
    {'a': 2, 'b': 3},
    {'a': 3},
    {'a': 4}
]

(Clumper(list_dicts)
  .mutate(s1=smoothing(key='a', weight=0.5),
          s2=smoothing(key='a', weight=0.9))
  .collect())