python - 导致“TypeError:无法使用此索引类型 datetimeindex 执行 truediv”的日期减法
问题描述
我试图从日期列中减去一个日期并计算月份的差异:
pd.to_datetime('2020-01-20')-pd.to_datetime(df['date'])/np.timedelta64(1,'M')
但我明白了
> TypeError: cannot perform __truediv__ with this index type datetimeindex
我怎样才能更正我的代码以找出几个月的差异?
解决方案
我认为您缺少一对括号:
(pd.to_datetime('2020-01-20')-pd.to_datetime(df['date'])) / pd.to_timedelta(`1min')
^ ^
this and this
推荐阅读
- reactjs - 如何在 React 的第二个 API 调用中使用一个 API 的结果?
- file - 如何将我的 Rust 程序拆分为多个文件?
- html - html表单提交到带有参数的快递路线
- swift - Swift:如何取消延迟的 pushViewController?
- ios - NSURL URLByAppendingPathExtension 崩溃
- react-native - GitHub User Repositories - GraphQL API 使用 typePolicy 进行无限滚动和分页
- html - HTML:IT 将我重定向到同一个文件而不是 Inscription.html
- python - Pygame:我不能用箭头键移动一个用“pygame.draw.circle”函数绘制的圆圈
- javascript - 如何检测来自 javascript 中 html 的变量中的禁用字母?
- reactjs - 即使使用默认值,useContext 也会返回 undefined