来自CSV数据的时间序列(时间戳和事件)

时间:2017-05-02 10:16:31

标签: python pandas matplotlib dataframe time-series

我想使用python的pandas模块通过时间序列表示,如下所示可视化CSV数据(参见下面的链接)。

df1的样本数据:

             TIMESTAMP  eventid
0  2017-03-20 02:38:24        1
1  2017-03-21 05:59:41        1
2  2017-03-23 12:59:58        1
3  2017-03-24 01:00:07        1
4  2017-03-27 03:00:13        1

'eventid'列始终包含值1,我试图显示数据集中每天的事件总和。是

pandas.Series.cumsum() 

用于此目的的正确功能?

到目前为止

脚本:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

df1 = pd.read_csv('timestamp01.csv')
print df1.columns # u'TIMESTAMP', u'eventid'

# I: ts = pd.Series(df1['eventid'], index=df1['TIMESTAMP']) 
# O: Blank plot

# I: ts = pd.Series(df1['eventid'], index=pd.date_range(df1['TIMESTAMP'], periods=1000)) 
# O: TypeError: Cannot convert input ... Name: TIMESTAMP, dtype: object] of type <class 'pandas.core.series.Series'> to Timestamp

# working test example:
# I: ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))
# O: See first link below (first plot).

ts = ts.cumsum()
ts.plot()
plt.show()

我试图遵循的链接:

http://pandas.pydata.org/pandas-docs/stable/visualization.html

Aggregating timeseries from sensors

(上面的示例有不同的值,而不是我的'eventid'数据)

d3: timeseries from data

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

您似乎需要通过read_csv中的参数TIMESTAMPdatetime列转换为parse_dates

import pandas as pd
from pandas.compat import StringIO

temp=u"""TIMESTAMP,eventid
2017-03-20 02:38:24,1
2017-03-20 05:38:24,1
2017-03-21 05:59:41,1
2017-03-23 12:59:58,1
2017-03-24 01:00:07,1
2017-03-27 03:00:13,1"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
df = pd.read_csv(StringIO(temp),  parse_dates=True, index_col='TIMESTAMP')
print (df)
                     eventid
TIMESTAMP                   
2017-03-20 02:38:24        1
2017-03-20 05:38:24        1
2017-03-21 05:59:41        1
2017-03-23 12:59:58        1
2017-03-24 01:00:07        1
2017-03-27 03:00:13        1

print (df.index)
DatetimeIndex(['2017-03-20 02:38:24', '2017-03-20 05:38:24',
               '2017-03-21 05:59:41', '2017-03-23 12:59:58',
               '2017-03-24 01:00:07', '2017-03-27 03:00:13'],
              dtype='datetime64[ns]', name='TIMESTAMP', freq=None)

然后按days使用resample并按size功能获取计数。最后Series.plot

print (df.resample('D').size())
TIMESTAMP
2017-03-20    2
2017-03-21    1
2017-03-22    0
2017-03-23    1
2017-03-24    1
2017-03-25    0
2017-03-26    0
2017-03-27    1
Freq: D, dtype: int64

df.resample('D').size().plot()

如果需要更改tickers的格式:

import matplotlib.ticker as ticker

ax = df.resample('D').size().plot()
ax.xaxis.set_major_formatter(ticker.FixedFormatter(df.index.strftime('%Y-%m-%d')))

答案 1 :(得分:2)

另一种绘图方式是使用groupby和count timess:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('timestamp01.csv', parse_dates=[0], index_col=[0]) # set timestamp as index
ts = df.groupby(df.index.date).count() # count occurrences
ax = ts.plot() # plot
plt.setp(ax.xaxis.get_majorticklabels(), rotation=10) # format x axis
plt.show()

enter image description here