踩坑记录 2020-08-06 17:13 采纳率: 33.3%
浏览 1198

时间序列画图 pandas Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex'

这是我的数据

图片说明

数据类型为


JD         datetime64[ns]
Mage               object
Magnorm            object
dtype: object

想以时间为x轴的图

down_timedf = target_star_df['Magnorm'].resample('3T').mean()

down_timedf.plot()

plt.legend(bbox_to_anchor=(1.25, 0.5))
plt.title('time_Magnorm')
sns.despine()


出现

TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex'

错误

TypeError                                 Traceback (most recent call last)
<ipython-input-138-8281def7eb17> in <module>()
      1 
----> 2 down_timedf = target_star_df['Magnorm'].resample('3T').mean()
      3 
      4 down_timedf.plot()
      5 # 图示的说明

/opt/conda/lib/python3.6/site-packages/pandas/core/generic.py in resample(self, rule, how, axis, fill_method, closed, label, convention, kind, loffset, limit, base, on, level)
   5520                      axis=axis, kind=kind, loffset=loffset,
   5521                      convention=convention,
-> 5522                      base=base, key=on, level=level)
   5523         return _maybe_process_deprecations(r,
   5524                                            how=how,

/opt/conda/lib/python3.6/site-packages/pandas/core/resample.py in resample(obj, kind, **kwds)
    997     """ create a TimeGrouper and return our resampler """
    998     tg = TimeGrouper(**kwds)
--> 999     return tg._get_resampler(obj, kind=kind)
   1000 
   1001 

/opt/conda/lib/python3.6/site-packages/pandas/core/resample.py in _get_resampler(self, obj, kind)
   1114         raise TypeError("Only valid with DatetimeIndex, "
   1115                         "TimedeltaIndex or PeriodIndex, "
-> 1116                         "but got an instance of %r" % type(ax).__name__)
   1117 
   1118     def _get_grouper(self, obj, validate=True):

TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'RangeIndex'

请问各位大佬 这是什么问题呢
如何修改呢

  • 写回答

1条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗