我正在尝试 运行 一个 Ipython 笔记本,但它给我一个 "astype timedelta64" 错误
I'm trying to run an Ipython notebook, except it's giving me an "astype timedelta64" error
我对大数据很感兴趣,最近偶然发现了这个 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
如果你需要我 post 我会的,但我认为核心问题可能与 numpy 和 pandas 版本之间的冲突有关。
有经验的人可以告诉我应该如何改变吗?我正在 运行宁 Ubuntu 14.04。
问题是 Numpy 不再支持从浮点数创建 timedelta64
对象(大概在笔记本发布时是可能的)。
您需要先将按比例放大的索引转换为整数,然后再转换为 timedelta64
。
days_from_start=pd.Series(t_all.index*30).astype('int').astype('timedelta64[D]')
我对大数据很感兴趣,最近偶然发现了这个 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
如果你需要我 post 我会的,但我认为核心问题可能与 numpy 和 pandas 版本之间的冲突有关。
有经验的人可以告诉我应该如何改变吗?我正在 运行宁 Ubuntu 14.04。
问题是 Numpy 不再支持从浮点数创建 timedelta64
对象(大概在笔记本发布时是可能的)。
您需要先将按比例放大的索引转换为整数,然后再转换为 timedelta64
。
days_from_start=pd.Series(t_all.index*30).astype('int').astype('timedelta64[D]')