Skip to content

rolling window function with multiple arguments by group #15178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
robpla opened this issue Jan 20, 2017 · 1 comment
Closed

rolling window function with multiple arguments by group #15178

robpla opened this issue Jan 20, 2017 · 1 comment
Labels
Duplicate Report Duplicate issue or pull request

Comments

@robpla
Copy link

robpla commented Jan 20, 2017

Code Sample, a copy-pastable example if possible

## let's say we have the below DF
rand = np.random.RandomState(1)
dff = pd.DataFrame({'A' : np.arange(20),
                    'B' : rand.randint(100, 120, 20),
                    'C' : rand.randint(0, 2, 20)})

### Ideal solution, this does not work of course
#dff['tau'] = dff.groupby('C').sort_values('A').rolling(window = 5).apply(sp.stats.mstats.kendalltau, args = ({'x' : 'A', 'y' : 'B'))

## workaround - need to pass an index, use a global DF, and (?) hard-code columns to use, use a to-be-deprecated function etc etc
def my_tau_indx(indx):
    x = dff.iloc[indx, 0]
    y = dff.iloc[indx, 1]
    tau = sp.stats.mstats.kendalltau(x, y)[0]
    return tau

grp = dff.sort_values(['A', 'C']).groupby('C', group_keys=False)
func = lambda x: pd.Series(pd.rolling_apply(np.arange(len(x)), 5, my_tau_indx), x.index)
t = grp.apply(func)
dff.reindex(t.index).assign(tau=t)

Problem description

There is no simple way to run a scipy/custom function requiring multiple arguments (by group) in a rolling window.

In the code above, let's say that the 'C' column below is used for grouping.
I need to

  • Group by column 'C'
  • Within each group, sort by 'A'
  • Withing each group, apply a rolling function taking two (or more) arguments, like kendalltau, to arguments 'A' and 'B'.

I asked this question here: http://stackoverflow.com/questions/41715814/python-pandas-rolling-function-with-two-arguments-in-a-grouped-dataframe/41719105#41719105 .

And the only viable solution I received uses a to-be-deprecated pd.rolling_apply.

Expected Output

See the link above for the expected results. See the code sample for what I'd like to write to get the result.

Output of pd.show_versions()

# Paste the output here pd.show_versions() here

DEBUG:pip.vcs:Registered VCS backend: git
DEBUG:pip.vcs:Registered VCS backend: hg
DEBUG:pip.pep425tags:Config variable 'Py_DEBUG' is unset, Python ABI tag may be incorrect
DEBUG:pip.pep425tags:Config variable 'WITH_PYMALLOC' is unset, Python ABI tag may be incorrect
DEBUG:pip.pep425tags:Config variable 'Py_DEBUG' is unset, Python ABI tag may be incorrect
DEBUG:pip.pep425tags:Config variable 'WITH_PYMALLOC' is unset, Python ABI tag may be incorrect
DEBUG:pip.vcs:Registered VCS backend: svn
DEBUG:pip.vcs:Registered VCS backend: bzr

INSTALLED VERSIONS

commit: None
python: 3.5.2.final.0
python-bits: 64
OS: Windows
OS-release: 7
machine: AMD64
processor: Intel64 Family 6 Model 62 Stepping 4, GenuineIntel
byteorder: little
LC_ALL: None
LANG: en
LOCALE: None.None

pandas: 0.19.2
nose: None
pip: 9.0.1
setuptools: 27.2.0
Cython: None
numpy: 1.12.0rc2
scipy: 0.18.1
statsmodels: None
xarray: None
IPython: 5.1.0
sphinx: 1.4.8
patsy: 0.4.1
dateutil: 2.6.0
pytz: 2016.7
blosc: None
bottleneck: None
tables: None
numexpr: None
matplotlib: 1.5.3
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.8
boto: None
pandas_datareader: None

@TomAugspurger
Copy link
Contributor

Dupe of #15095

Still trying to figure out the API. We're potentially constrained by backwards-compat. Post there if you have any thought.

@TomAugspurger TomAugspurger added the Duplicate Report Duplicate issue or pull request label Jan 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate Report Duplicate issue or pull request
Projects
None yet
Development

No branches or pull requests

2 participants