python - Numpy unwrap error when using apply() on Pandas dataframe
问题描述
I have a Pandas DataFrame which has two columns containing some angles in the range [-pi, pi). I need to calculate the instantaneous angular velocity on each row, which I can do using diff(), however this naive approach fails when my data crosses the discontinuity from pi to -pi, e.g.
I'm trying to use numpy.unwrap() on my columns but when I try the code below I get a ValueError.
angle_data["theta"].apply(np.unwrap)
<Traceback message>
ValueError: diff requires input that is at least one dimensional
This also occurs if I copy the columns to a Pandas Series and try to use apply(np.unwrap)
.
I can workaround this by doing
angle_data["theta"] = pd.Series(np.unwrap(angle_data["theta"]))
or by using apply on multiple columns at once, but I'd like to know why the apply(np.unwrap)
method doesn't work for a Pandas Series.
解决方案
From the doc :
Help on function unwrap in module numpy:
unwrap(p, discont=3.141592653589793, axis=-1)
...
Parameters
----------
p : array_like
Input array.
...
What your traceback is saying is that by using apply, you are iterating over the column, then applying unwrap to each individual value (which goes against the doc about p).
You can see what is happening by using some custom print like this :
def my_print(x):
print(x)
print('-'*50)
df['theta'].apply(my_print)
You will see that each value of the column is passed as an argument one after the other. In other terms, you are looping as you would through a list : quite inefficient.
You already found the right way to use unwrap : by applying it straight to the series, which doesn't iterate over it : np.unwrap(df['theta']).
This is the way to use all numpy functions (spoiler alert : huge performances gains are due if you drop the "apply" method).
So as a rule of thumb : stay away of "apply" when you can (an most of the time, you can indeed) and stick to numpy or built-in functions from pandas.
推荐阅读
- sql-server - 无法从 VS2017 执行 SSIS 包
- android - 已为元素“android.support.v7.widget.CardView”指定了 logcat 属性“xmlns:card_view”上的 Android Studio 错误
- concurrency - 了解 Gremlin 在并发环境中的合并行为
- python - 如何从另一个 python 脚本重复运行 python 脚本?
- python - 为什么 a = a['k'] = {} 会创建一个无限嵌套的字典?
- xcode - 在 Xcode 上使用 dart 进行编码是否可行?
- jquery - Jquery - Bootgrid 在加载时预选行
- php - cors(cors 策略:对预检请求的响应未通过访问控制检查)角度 7 中的错误
- html - CSS网格,右侧空间
- python - 停止 Tkinter .after 循环的正确方法