Python 如何导入一个csv文件,其中一列为时间格式hh:mm:ss,并使用熊猫在x轴上打印?

Python 如何导入一个csv文件,其中一列为时间格式hh:mm:ss,并使用熊猫在x轴上打印?,python,pandas,matplotlib,Python,Pandas,Matplotlib,这是csv文件中的数据的外观 Time stamp kW 09:41:12 5.412224215 09:41:22 5.057871807 09:41:32 5.357250571 09:41:42 5.427124444 09:41:52 5.485060159 09:42:02 4.942689557 09:42:19 4.914225079 09:42:29 5.032617133 09:42:39 5.131685175 09:42:49

这是csv文件中的数据的外观

Time stamp     kW
09:41:12   5.412224215
09:41:22   5.057871807
09:41:32   5.357250571
09:41:42   5.427124444
09:41:52   5.485060159
09:42:02   4.942689557
09:42:19   4.914225079
09:42:29   5.032617133
09:42:39   5.131685175
09:42:49   61.05746329
我正在尝试导入它,并将其绘制为hh:ss:ss的x_轴

这就是我的代码的样子:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import datetime as dt
headers = ["Time stamp", "kW"]
dataset = pd.read_csv('Wash_Data1.csv',sep = ";",names = headers)
dataset.plot()
在[28]中:df=df.设置索引(pd.到时间增量(df['Time stamp']))
In[29]:df
出[29]:
时间戳千瓦
时间戳
09:41:12     09:41:12   5.412224
09:41:22     09:41:22   5.057872
09:41:32     09:41:32   5.357251
09:41:42     09:41:42   5.427124
09:41:52     09:41:52   5.485060
09:42:02     09:42:02   4.942690
09:42:19     09:42:19   4.914225
09:42:29     09:42:29   5.032617
09:42:39     09:42:39   5.131685
09:42:49     09:42:49  61.057463
在[30]中:ax=df['kW']].plot()
In[33]:ax.set_xticklabels(df[“时间戳])
在[34]中:df.index.dtype

Out[34]:dtype('你的答案对我来说绝对太好了,我不可能把我那愚蠢的一行保留下来:-)@cᴏʟᴅsᴘᴇᴇᴅ, 但它将适用于问题时间序列中指定的时间序列,因此请取消删除它!你的也一样!它也做了同样的事情,只是更好。相信我,我会感觉好一点的。
In [28]: df = df.set_index(pd.to_timedelta(df['Time stamp']))

In [29]: df
Out[29]:
           Time stamp         kW
Time stamp
09:41:12     09:41:12   5.412224
09:41:22     09:41:22   5.057872
09:41:32     09:41:32   5.357251
09:41:42     09:41:42   5.427124
09:41:52     09:41:52   5.485060
09:42:02     09:42:02   4.942690
09:42:19     09:42:19   4.914225
09:42:29     09:42:29   5.032617
09:42:39     09:42:39   5.131685
09:42:49     09:42:49  61.057463

In [30]: ax = df[['kW']].plot()

In [33]: ax.set_xticklabels(df['Time stamp'])

In [34]: df.index.dtype
Out[34]: dtype('<m8[ns]')

In [35]: df.dtypes
Out[35]:
Time stamp     object
kW            float64
dtype: object