Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shifting all rows in dask dataframe

In Pandas, there is a method DataFrame.shift(n) which shifts the contents of an array by n rows, relative to the index, similarly to np.roll(a, n). I can't seem to find a way to get a similar behaviour working with Dask. I realise things like row-shifts may be difficult to manage with Dask's chunked system, but I don't know of a better way to compare each row with the subsequent one.

What I'd like to be able to do is this:

import numpy as np
import pandas as pd
import dask.DataFrame as dd

with pd.HDFStore(path) as store:
    data = dd.from_hdf(store, 'sim')[col1]
    shifted = data.shift(1)

    idx = data.apply(np.sign) != shifted.apply(np.sign)

in order to create a boolean series indicating the locations of sign changes in the data. (I am aware that method would also catch changes from a signed value to zero) I would then use the boolean series to index a different Dask dataframe for plotting.

like image 983
TroyHurts Avatar asked Nov 08 '22 23:11

TroyHurts


1 Answers

Rolling functions

Currently dask.dataframe does not implement the shift operation. It could though if you raise an issue. In principle this is not so dissimilar from rolling operations that dask.dataframe does support, like rolling_mean, rolling_sum, etc..

Actually, if you were to create a Pandas function that adheres to the same API as these pandas.rolling_foo functions then you can use the dask.dataframe.rolling.wrap_rolling function to turn your pandas style rolling function into a dask.dataframe rolling function.

dask.dataframe.rolling_sum = wrap_rolling(pandas.rolling_sum)
like image 188
MRocklin Avatar answered Nov 14 '22 23:11

MRocklin