时间序列绘图Python

时间:2015-04-09 20:19:18

标签: python pandas plot time-series

我正在使用熊猫,我想制作一个时间序列图。我有这个数据帧,我想在x轴上绘制日期,并在y轴上绘制单位数。我假设我需要将我的日期对象转换为日期时间才能制作此图。

df1_99.dtypes

 date            object
 store_nbr        int64
 units            int64
 tavg             int64
 preciptotal    float64
 dtype: object


df1_99
        date        store_nbr      units        tavg    preciptotal
 101885 2014-10-13       1          2            49       0.00
 101996 2014-10-14       1          1            67       0.00
 102107 2014-10-15       1          0            70       0.00
 102218 2014-10-16       1          0            67       0.87
 102329 2014-10-17       1          3            65       0.01

1 个答案:

答案 0 :(得分:0)

由于您的日期是字符串,您可以使用to_datetime转换为日期时间对象:

In [4]:

df['date'] = pd.to_datetime(df['date'])
df.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 5 entries, 101885 to 102329
Data columns (total 5 columns):
date           5 non-null datetime64[ns]
store_nbr      5 non-null int64
units          5 non-null int64
tavg           5 non-null int64
preciptotal    5 non-null float64
dtypes: datetime64[ns](1), float64(1), int64(3)
memory usage: 240.0 bytes

然后你可以绘制这个:

df.plot(x='date', y='units')

enter image description here

相关问题