【问题标题】:Time Series Plot Python时间序列图 Python
【发布时间】:2015-06-15 09:22:01
【问题描述】:

我正在使用 pandas,我想制作一个时间序列图。我有这个数据框,我想在 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

【问题讨论】:

  • 您的日期是一个字符串,并且只是“2014-10-13”条目吗?您可以使用to_datetime 进行转换,例如df['date'] = pd.to_datetime(df['date'])
  • 好的,是的,这很容易。我用' df1_99.plot(x='date', y='units') '绘图。我得到了我想要的情节。您知道如何轻松更改 x 轴上的刻度吗?我一直在阅读文档,但无法弄清楚。
  • 我应该发帖作为答案吗?

标签: python pandas plot time-series


【解决方案1】:

由于您的日期是字符串,您可以使用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')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-22
    • 2021-10-31
    • 2018-06-24
    • 1970-01-01
    • 2021-05-06
    • 2016-10-26
    • 1970-01-01
    • 2017-10-16
    相关资源
    最近更新 更多