Skip to content

Lookback methods (rolling/ewm/etc.) should accept series instead of just int/float #26920

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
JakeTheSnake3p0 opened this issue Jun 18, 2019 · 1 comment

Comments

@JakeTheSnake3p0
Copy link

JakeTheSnake3p0 commented Jun 18, 2019

Issue: Using variable lookback-lengths in pandas methods

Per underlying stock asset, I utilize a homodyne discriminator derived from close prices which determines the appropriate lookback length for other indicators such as macd, smart volume, ehlers rsi, etc.

When I retrieve history on an asset, the data arrives in bulk. I want to apply my indicators en-masse to the entire dataset instead of having to iterate through each row in order to generate the appropriate values (lookback period, then indicator values). That means ideally I would create the 'lookback period' column on the entire dataset through a homodyne discriminator method/class and subsequently apply all of my indicator classes to the same dataset, most of which may depend on the lookback period.

Given the current implementation of ewm/rolling which seemingly accept only a static integer/float, all of my calculations will be off.

Ideal code which generates the error:

import pandas as pd
import numpy as np

# Initialize dataframe with source data
df = pd.DataFrame()
df['source'] = pd.Series(range(100, 200))

# Typically 'period' is generated by some class
# such as a homodyne discriminator for which
# other classes depend on for lookback length
df['period'] = np.nan
df.period.iloc[0] = 1
df.period.iloc[-1] = 10
df.period.interpolate(inplace=True)
df.period = df.period.astype('int64', copy=False)
print(df)

# A given class that depends on the lookback
# length determined by the discriminator
df['dependant'] = df.source.ewm(span=df.period, adjust=False).mean()
print(df)

Error:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

INSTALLED VERSIONS

commit: None
python: 3.6.7.final.0
python-bits: 64
OS: Linux
OS-release: 4.4.0-1084-aws
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: en_US.UTF-8
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.23.4
pytest: 4.2.0
pip: 19.0.1
setuptools: 40.8.0
Cython: 0.29.2
numpy: 1.14.5
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 7.2.0
sphinx: None
patsy: 0.5.1
dateutil: 2.7.5
pytz: 2018.9
blosc: None
bottleneck: None
tables: 3.4.4
numexpr: 2.6.8
feather: None
matplotlib: 3.0.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: 1.2.16
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None
None

@TomAugspurger
Copy link
Contributor

Looks like a duplicate of #23002

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants