【问题标题】:Axes3D Plot Wireframe from DateTime来自 DateTime 的 Axes3D 绘制线框
【发布时间】:2022-03-12 09:04:39
【问题描述】:

我正在使用 Axes3D,我想创建一个包含两个浮点数和一个 pandas 系列时间戳的 3d 图。绘图函数如下所示:

ax.plot_wireframe(X = response['PCT_MONEYNESS'], Y = response['IVOL'], Z=response['EXPIRE_DT'])

当我尝试这个时,我得到了一个错误

"ufunc multiply cannot use operands with types dtype('float64') and dtype('<M8[ns]')"

我可以将日期时间改为仅几天,所以我回到我的数据框并使用以下代码对其进行了编辑

response['EXPIRE_DT'] = response['EXPIRE_DT'].apply(lambda x: (x.to_pydatetime()-datetime.datetime.today()).days).astype(float)

当我只返回 response['EXPIRE_DT'] 时,我得到了一系列数据类型 float 的 pandas,这是预期的。但是,错误仍然存​​在。尽管我没有尝试使用数据类型<M8[ns] 绘制任何内容,但是否还有其他原因可能会弹出此错误?

编辑:根据要求,从响应['EXPIRE_DT'] 输出,包括编辑前后。

之前--

ID
AAPL US 01/19/18 P85 Equity      2018-01-19
AAPL US 02/09/18 P170 Equity     2018-02-09
AAPL US 02/09/18 P167.5 Equity   2018-02-09
AAPL US 02/09/18 P165 Equity     2018-02-09
Name: EXPIRE_DT, dtype: datetime64[ns]

之后——

ID
AAPL US 01/19/18 P85 Equity        21.0
AAPL US 02/09/18 P170 Equity       42.0
AAPL US 02/09/18 P167.5 Equity     42.0
AAPL US 02/09/18 P165 Equity       42.0
Name: EXPIRE_DT, dtype: float64

其他两个数据框是系列的

Name: PCT_MONEYNESS, dtype: float64

Name: IVOL, dtype: float64

分别。所以三个变量都应该是 float64 类型

【问题讨论】:

  • 你能发布几行response DataFrame,最好编辑成你的问题吗?我不确定为什么即使在将 EXPIRE_DT 列替换为从今天开始的日期的浮点表示后,您也会收到完全相同的错误。
  • 添加了所有三种相关的系列数据类型。

标签: python pandas datetime matplotlib plot


【解决方案1】:

你可以试试:

import matplotlib.dates as mdates
mdates.date2num(response['EXPIRE_DT'])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-16
    • 1970-01-01
    • 1970-01-01
    • 2017-03-13
    • 2017-07-19
    • 1970-01-01
    • 2016-06-11
    • 1970-01-01
    相关资源
    最近更新 更多