Common - generic functions (pynance.common)

pynance.common.decorate(fn, *args, **kwargs)[source]

Return a new function that replicates the behavior of the input but also returns an additional value. Used for creating functions of the proper type to pass to labeledfeatures().


fn : function

*args : any

Additional parameters that the returned function will return

**kwargs : dict

Each element in kwargs will become an attribute of the output function.


wrapped : function

New function that acts like fn except that it also returns an additional value.


If fn returns multiple values, these will be returned in sequence as the first values returned by add_rets(fn, arg0, arg1, arg2). See example above.


>>> from functools import partial
>>> forecast_interval = 32
>>> features, labels =, 256, featurefn,
...        decorate(partial(, forecast_interval, 'Adj Close'), forecast_interval))
>>> def f():
...    return 0, 1 
>>> pn.decorate(f, 3, 4, 5)()
(0, 1, 3, 4, 5)
>>> pn.decorate(lambda x: x * .5, 3, 4, 5)(1.)
(1., 3, 4, 5)
>>> pn.decorate(lambda x: x, 1 2)('foo')
('foo', 1, 2)
>>> pn.decorate(f, 'foo'):
(0, 1, 'foo')
pn.decorate(f, 0, foo='bar').foo
>>> 'bar'
pynance.common.expand(fn, col, inputtype=<class 'pandas.core.frame.DataFrame'>)[source]

Wrap a function applying to a single column to make a function applying to a multi-dimensional dataframe or ndarray


fn : function

Function that applies to a series or vector.

col : str or int

Index of column to which to apply fn.

inputtype : class or type

Type of input to be expected by the wrapped function. Normally pd.DataFrame or np.ndarray. Defaults to pd.DataFrame.


wrapped : function

Function that takes an input of type inputtype and applies fn to the specified col.

pynance.common.featurize(equity_data, n_sessions, **kwargs)[source]

Generate a raw (unnormalized) feature set from the input data. The value at column on the given date is taken as a feature, and each row contains values for n_sessions


equity_data : DataFrame

data from which to generate features

n_sessions : int

number of sessions to use as features

selection : str, default: ‘Adj Close’

column of equity_data from which to generate features.

columns : list, default: map(str, range((-n_sessions + 1), 1))

column names for output DataFrame. Default will look like: [‘-5’, ‘-4’, ‘-3’, ‘-2’, ‘-1’, ‘0’].


out : DataFrame

Each row is a sequence of n_sessions session values where the last column matches the value on the date specified by the DataFrame index.


>>> pn.featurize(equity_data, n_sessions, **kwargs)

Return false if eqdata contains no missing values.


eqdata : DataFrame or ndarray

Data to check for missing values (NaN, None)


answer : bool

False iff eqdata contains no missing values.