类型错误:无法将系列转换为 <class 'float'> [英] TypeError: cannot convert the series to &lt;class &#39;float&#39;&gt;

查看:872
本文介绍了类型错误:无法将系列转换为 <class 'float'>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的数据框 (df):

I have a dataframe (df) that looks like:

date                 A
2001-01-02      1.0022
2001-01-03      1.1033
2001-01-04      1.1496
2001-01-05      1.1033

2015-03-30    126.3700
2015-03-31    124.4300
2015-04-01    124.2500
2015-04-02    124.8900

对于整个时间序列,我试图将今天的值除以昨天的值并使用以下内容记录结果:

For the entire time-series I'm trying to divide today's value by yesterdays and log the result using the following:

df["B"] = math.log(df["A"] / df["A"].shift(1))

但是我收到以下错误:

TypeError: cannot convert the series to <class 'float'>

我该如何解决这个问题?我尝试使用以下方法将其转换为浮点数:

How can I fix this? I've tried to cast as float using:

df["B"] .astype(float)

但是什么也做不了.

推荐答案

您可以改用 numpy.log.Math.log 需要一个数字,而不是数组.

You can use numpy.log instead. Math.log is expecting a single number, not array.

这篇关于类型错误:无法将系列转换为 <class 'float'>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆