简体   繁体   中英

Pandas TA lib not working when using group_by

I have some OHLC 5m data like so:

                        timestamp    open    high  ...  symbol  volume_10_day  last_high_volume_high
0       2022-09-09 11:20:00+00:00  1.4000  1.4000  ...    AMAM            NaN                   0.50
1       2022-09-09 13:30:00+00:00  1.4100  1.4100  ...    AMAM            NaN                   0.50
2       2022-09-09 14:05:00+00:00  1.4749  1.4749  ...    AMAM            NaN                   0.50
3       2022-09-09 16:45:00+00:00  1.4700  1.4702  ...    AMAM            NaN                   0.50
4       2022-09-09 17:10:00+00:00  1.4300  1.4300  ...    AMAM            NaN                   0.50
...                           ...     ...     ...  ...     ...            ...                    ...
281476  2022-12-03 00:35:00+00:00  1.3300  1.3300  ...      ZH        31921.4                   1.07
281477  2022-12-03 00:40:00+00:00  1.3300  1.3300  ...      ZH        31921.4                   1.07
281478  2022-12-03 00:45:00+00:00  1.3200  1.3300  ...      ZH        31921.4                   1.07
281479  2022-12-03 00:50:00+00:00  1.3250  1.3250  ...      ZH        31921.4                   1.07
281480  2022-12-03 00:55:00+00:00  1.3300  1.3300  ...      ZH        31921.4                   1.07

I am then attempting to add a 72 ema, grouping everything by the symbol:

import pandas as pd
import pandas_ta as ta

df["EMA72"] = ta.ema(df.groupby('symbol')['close'], length=2) // length is 2 to demonstrate it isn't working

                       timestamp    open    high     low  ...      vwap  symbol  volume_10_day  EMA72
0       2022-09-09 11:20:00+00:00  1.4000  1.4000  1.4000  ...  1.400000    AMAM            NaN   None
1       2022-09-09 13:30:00+00:00  1.4100  1.4100  1.4100  ...  1.410000    AMAM            NaN   None
2       2022-09-09 14:05:00+00:00  1.4749  1.4749  1.4749  ...  1.474900    AMAM            NaN   None
3       2022-09-09 16:45:00+00:00  1.4700  1.4702  1.4100  ...  1.445265    AMAM            NaN   None
4       2022-09-09 17:10:00+00:00  1.4300  1.4300  1.4100  ...  1.413117    AMAM            NaN   None
...                           ...     ...     ...     ...  ...       ...     ...            ...    ...
281476  2022-12-03 00:35:00+00:00  1.3300  1.3300  1.3300  ...  1.330000      ZH        31921.4   None
281477  2022-12-03 00:40:00+00:00  1.3300  1.3300  1.3300  ...  1.330000      ZH        31921.4   None
281478  2022-12-03 00:45:00+00:00  1.3200  1.3300  1.3200  ...  1.322804      ZH        31921.4   None
281479  2022-12-03 00:50:00+00:00  1.3250  1.3250  1.3250  ...  1.325000      ZH        31921.4   None
281480  2022-12-03 00:55:00+00:00  1.3300  1.3300  1.3200  ...  1.326081      ZH        31921.4   None

What am I doing wrong here?

I think this is how:

ema72 = lambda x: ta.ema(df.loc[x.index, "close"], 72) 
df["EMA72"] = df.groupby(['symbol']).apply(ema72).reset_index(0,drop=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM