计算 Pandas 数据框中的平均真实范围列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35753914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:48:44  来源:igfitidea点击:

Calculating Average True Range column in Pandas dataframe

pythonpandastechnical-indicator

提问by ironfish

I am trying to add an Average True Range column to a dataframe that contains historical stock data.

我正在尝试将平均真实范围列添加到包含历史股票数据的数据框中。

The code I am using so far is:

到目前为止我使用的代码是:

def add_atr_to_dataframe (dataframe):
    dataframe['ATR1'] = abs (dataframe['High'] - dataframe['Low'])
    dataframe['ATR2'] = abs (dataframe['High'] - dataframe['Close'].shift())
    dataframe['ATR3'] = abs (dataframe['Low'] - dataframe['Close'].shift())
    dataframe['TrueRange'] = max (dataframe['ATR1'], dataframe['ATR2'], dataframe['ATR3'])
    return dataframe

The last line, containing the max function, gives the error:

最后一行,包含 max 函数,给出错误:

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

I have googled for days in trying to learn how to resolve this error, or do the code in a better way, etc and have found nothing that helps me along.

我在谷歌上搜索了几天,试图学习如何解决这个错误,或者以更好的方式编写代码,等等,但没有发现任何对我有帮助的东西。

Any help in the following would be much appreciated:

任何在以下方面的帮助将不胜感激:

  1. How to resolve the error

  2. How to do the code in a better way - I do not mean that I have to code it this way and there may be better ways to do it.

  1. 如何解决错误

  2. 如何以更好的方式编写代码 - 我并不是说我必须以这种方式编写代码,并且可能有更好的方法来做到这一点。

Thanks ahead of time.

提前致谢。

回答by Igor Raush

tl;drUse

tl;博士使用

dataframe[['ATR1', 'ATR2', 'ATR3']].max(axis=1)


Explanation

解释

You can't use the built-in maxon Pandas objects. Since the first argument you are passing to maxis iterable, thissignature of maxis invoked:

你不能max在 Pandas 对象上使用内置的。由于您传递给的第一个参数max是可迭代的,因此max调用了以下签名:

max(iterable[, key])

max(可迭代[ , 键])

This implicitly performs a __nonzero__(truthiness) check on the first argument to determine whether the iterable is empty, which is where your error is coming from. Numpy and Pandas objects do not coerce to booleans by design.

这会隐式地__nonzero__对第一个参数执行(真实性)检查,以确定可迭代对象是否为空,这就是您的错误的来源。Numpy 和 Pandas 对象在设计上不会强制转换为布尔值。

You are looking for something like this:

你正在寻找这样的东西:

dataframe['TrueRange'] = dataframe[['ATR1', 'ATR2', 'ATR3']].max(axis=1)

This computes the maximum of the ATR*columns along the horizontal axis and returns the result as a Series, which you then add as a new TrueRangecolumn in your dataframe.

这将计算ATR*沿水平轴的列的最大值,并将结果作为 a 返回Series,然后您将其作为新TrueRange列添加到数据框中。

回答by rde

not exactly sure if I get what you mean, but I'd suggest using pd.max()instead of max()in the problematic row.

不确定我是否明白你的意思,但我建议在有问题的行中使用pd.max()而不是max()