You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## let's say we have the below DFrand=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 etcdefmy_tau_indx(indx):
x=dff.iloc[indx, 0]
y=dff.iloc[indx, 1]
tau=sp.stats.mstats.kendalltau(x, y)[0]
returntaugrp=dff.sort_values(['A', 'C']).groupby('C', group_keys=False)
func=lambdax: 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'.
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
Code Sample, a copy-pastable example if possible
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
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()
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
The text was updated successfully, but these errors were encountered: