我试图绘制一些在夜间运行的程序的持续时间,我将程序持续时间数据导出到 CSV 文件中,以便稍后进行分析。 (像这样)
这是我的代码和 CSV 示例:
CSV:
na,programName,totaal,na,startDate,endDate,Date
?,"to/check.apl",54006,?,2017-02-27T20:04:07.233,2017-02- 27T20:05:01.239,2017-02-27T00:00:00.000
?,"to/ibx.apl",143887,?,2017-02-27T20:07:55.627,2017-02-27T20:10:19.514,2017-02-27T00:00:00.000
?,"to/checker.apl",2039600,?,2017-02-27T20:14:37.662,2017-02-27T20:48:37.262,2017-02-27T00:00:00.000
python 代码:
import matplotlib
from pandas import *
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
matplotlib.style.use('ggplot')
data = "miFile.csv"
df = pd.DataFrame.from_csv(data)
df = df.set_index('totaal')
newDf = df[['programName','startDate','endDate']]
到目前为止,我得到了日期时间错误,所以我尝试通过这样做来解决这个问题(也没有运气来绘制):
newDf['startDate'] = pd.to_datetime(newDf['startDate'])
newDf['endDate'] = pd.to_datetime(newDf['endDate'])
#pd.to_datetime(pd.Series(["2017-02-27T20:04:07.233"]) format= "%d, %m, %y, %H: %M: %S")
newDf.plot('programName','startDate','endDate')
plt.show()
请您参考如下方法:
我想你需要read_csv用于创建 df,然后获取列的差异和 convert timedelta到 分钟 plot :
temp=u"""na,programName,totaal,na,startDate,endDate,Date
?,"to/check.apl",54006,?,2017-02-27T20:04:07.233,2017-02-27T20:05:01.239,2017-02-27T00:00:00.000
?,"to/ibx.apl",143887,?,2017-02-27T20:07:55.627,2017-02-27T20:10:19.514,2017-02-27T00:00:00.000
?,"to/checker.apl",2039600,?,2017-02-27T20:14:37.662,2017-02-27T20:48:37.262,2017-02-27T00:00:00.000"""
#after testing replace 'StringIO(temp)' to 'filename.csv'
df = pd.read_csv(StringIO(temp), index_col=[2], parse_dates=[4,5,6])
print (df.dtypes)
na object
programName object
na.1 object
startDate datetime64[ns]
endDate datetime64[ns]
Date datetime64[ns]
dtype: object
df['duration'] = (df['endDate'] - df['startDate']).astype('timedelta64[m]')
newDf = df[['programName','duration']]
print (newDf)
programName duration
totaal
54006 to/check.apl 0.0
143887 to/ibx.apl 2.0
2039600 to/checker.apl 33.0
newDf.plot()
plt.show()
