python-如何使用熊猫系列绘制两个不同长度/开始日期的时间序列?

我正在绘制几个“每周事件总数”的熊猫系列对象.系列events_per_week中的数据如下所示:

Datetime
 1995-10-09     45
 1995-10-16     63
 1995-10-23     83
 1995-10-30     91
 1995-11-06    101 
Freq: W-SUN, dtype: int64

我的问题如下.所有熊猫系列的长度都相同,即从1995年开始.但是,其中一个阵列于2003年开始. events_per_week2003从2003年开始

 Datetime
     2003-09-08     25
     2003-09-15     36
     2003-09-22     74
     2003-09-29     25
     2003-09-05    193 
    Freq: W-SUN, dtype: int64

import matplotlib.pyplot as plt
fig = plt.figure(figsize=(20,5))
ax = plt.subplot(111)
plt.plot(events_per_week)
plt.plot(events_per_week2003)

我收到以下值错误.

ValueError: setting an array element with a sequence.

我怎样才能做到这一点?

解决方法:

我真的不了解您遇到的问题.
我试图重新创建数据框的一部分,并且没有问题.

import numpy, matplotlib
data = numpy.array([45,63,83,91,101])
df1 = pd.DataFrame(data, index=pd.date_range('2005-10-09', periods=5, freq='W'), columns=['events'])
df2 = pd.DataFrame(numpy.arange(10,21,2), index=pd.date_range('2003-01-09', periods=6, freq='W'), columns=['events'])
matplotlib.pyplot.plot(df1.index, df1.events)
matplotlib.pyplot.plot(df2.index, df2.events)
matplotlib.pyplot.show()

使用系列而不是数据框:

ds1 = pd.Series(data, index=pd.date_range('2005-10-09', periods=5, freq='W'))
ds2 = pd.Series(numpy.arange(10,21,2), index=pd.date_range('2003-01-09', periods=6, freq='W'))
matplotlib.pyplot.plot(ds1)
matplotlib.pyplot.plot(ds2)
matplotlib.pyplot.show()

相关文章

转载:一文讲述Pandas库的数据读取、数据获取、数据拼接、数...
Pandas是一个开源的第三方Python库,从Numpy和Matplotlib的基...
整体流程登录天池在线编程环境导入pandas和xrld操作EXCEL文件...
 一、numpy小结             二、pandas2.1为...
1、时间偏移DateOffset对象DateOffset类似于时间差Timedelta...
1、pandas内置样式空值高亮highlight_null最大最小值高亮背景...