gpt4 book ai didi

python - 我正在尝试运行 Ipython 笔记本,除了它给我一个 "astype timedelta64"错误

转载 作者:太空宇宙 更新时间:2023-11-04 05:29:34 25 4
gpt4 key购买 nike

我对大数据很感兴趣,最近偶然发现了这个 Ipython 笔记本:https://github.com/lmart999/GIS/blob/master/SF_GIS_Crime.ipynb

并立即下载并尝试运行它。前十几个单元格运行正常,甚至是 matplotlib 图表。

直到我到达这些行:

#  Lets use real dates for plotting
days_from_start=pd.Series(t_all.index*30).astype('timedelta64[D]')
dates_for_plot=date.min()+days_from_start
time_labels=dates_for_plot.map(lambda x: str(x.year)+'-'+str(x.month))

给出了很长的错误信息:

--------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-27-4a793ff06024> in <module>()
1 # Lets use real dates for plotting
----> 2 days_from_start=pd.Series(t_all.index*30).astype('timedelta64[D]')
3 dates_for_plot=date.min()+days_from_start
4 time_labels=dates_for_plot.map(lambda x: str(x.year)+'-'+str(x.month))

[---50 行错误信息---]

lib.pyx in pandas.lib.astype_intsafe (pandas/lib.c:12697)()

util.pxd in util.set_value_at (pandas/lib.c:49357)()

ValueError: Could not convert object to NumPy timedelta

如果您需要我发布完整消息,我会发布,但我认为核心问题可能与 numpy 和 pandas 版本之间的冲突有关。

有经验的人可以告诉我应该如何改变吗?我正在运行 Ubuntu 14.04。

最佳答案

问题是 Numpy 不再支持从 float 创建 timedelta64 对象(大概在笔记本发布时这是可能的)。

在转换为 timedelta64 之前,您需要先将按比例放大的索引转换为整数。

days_from_start=pd.Series(t_all.index*30).astype('int').astype('timedelta64[D]')

关于python - 我正在尝试运行 Ipython 笔记本,除了它给我一个 "astype timedelta64"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37575608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com