Python 使用pandas仅解析小时:分钟:秒

Python 使用pandas仅解析小时:分钟:秒,python,pandas,Python,Pandas,我有以下数据: 23:10:50 all 28.36 0.00 0.38 0.25 0.00 71.02 23:10:51 all 22.77 0.00 0.84 0.12 0.00 76.27 23:10:52 all 32.06 0.00 0.86 0.00 0.00 67.08 23

我有以下数据:

23:10:50        all     28.36      0.00      0.38      0.25      0.00     71.02
23:10:51        all     22.77      0.00      0.84      0.12      0.00     76.27
23:10:52        all     32.06      0.00      0.86      0.00      0.00     67.08
23:10:53        all     31.38      0.00      0.61      0.00      0.00     68.01
23:10:54        all     27.17      0.00      1.36      0.25      0.00     71.22
23:10:55        all     37.48      0.00      0.75      0.00      0.00     61.77
23:10:56        all     29.02      0.00      0.75      1.76      0.00     68.47
23:10:57        all     41.82      0.00      1.37      0.12      0.00     56.68
23:10:58        all     29.01      0.00      1.10      0.00      0.00     69.89
23:10:59        all     37.00      0.00      1.50      1.88      0.00     59.62
23:11:00        all     44.25      0.00      1.12      0.00      0.00     54.62
23:11:01        all     27.72      0.00      0.62      0.00      0.00     71.66
23:11:02        all     30.71      0.00      1.11      0.00      0.00     68.18
23:11:03        all     27.40      0.00      0.62      0.00      0.00     71.98
...
我用以下方法对其进行解析:

dateparse = lambda x: pd.datetime.strptime(x, '%H:%M:%S')

data = pd.read_csv('../../data/cpu.dat', delim_whitespace=True, header=None, usecols=[0,2,4,7], names=['Time','User','System','Idle'], parse_dates=[0], date_parser=dateparse)
第一列是Hour:Minutes:Seconds,我的目的是让pandes以这种方式解析它。但是,它会创建以下内容:

0    1900-01-01 23:10:50
1    1900-01-01 23:10:51
2    1900-01-01 23:10:52
3    1900-01-01 23:10:53
4    1900-01-01 23:10:54
5    1900-01-01 23:10:55
6    1900-01-01 23:10:56
7    1900-01-01 23:10:57
8    1900-01-01 23:10:58
9    1900-01-01 23:10:59
10   1900-01-01 23:11:00
11   1900-01-01 23:11:01
12   1900-01-01 23:11:02
13   1900-01-01 23:11:03
有没有办法摆脱一年一个月的日子

问候,,
Max

试试这个,其中
timestr
是包含时间字符串表示的列的名称:

data['time'] = pd.to_datetime(data['timestr']).dt.time

IIUC你的问题是,我更改了你数据框的时间列名

df.rename(columns={0:'Time'}, inplace= True)
df
Time            1         2         3         4         5         6         7 
23:10:50        all     28.36      0.00      0.38      0.25      0.00     71.02
23:10:51        all     22.77      0.00      0.84      0.12      0.00     76.27
23:10:52        all     32.06      0.00      0.86      0.00      0.00     67.08
23:10:53        all     31.38      0.00      0.61      0.00      0.00     68.01...
现在我可以将您的时间列更改为
timedelta64[ns]

df.Time = pd.to_timedelta(df.Time)
df
当我输入df.dtypes时,我得到

Time    timedelta64[ns]
1                object
2               float64
3               float64
4               float64
5               float64
6               float64
7               float64
dtype: object

因此,您必须将列转换为
timedelta
,您的
seaborn
绘图应该可以工作。

我想问题是,为什么您不希望在数据框中包含日期?通常,出于所有实际目的,将日期与时间关联起来是完全有意义的。它的缺点到底是什么?知道了这一点,我们可以为实际问题提出另一种解决方案,即确保某些数据帧中没有日期,不会造成任何伤害。同样的想法:只需更改
dateparse=lambda x:pd.datetime.strtime(x,'%H:%M:%S')。time()
@stephan:nice!工作正常,但现在的问题是它显示为dtype:object,而不是dtype:datetime64[ns],因此在打印时,I get-TypeError:float()参数必须是字符串或数字。对于打印帮助,也许稍后对该问题的回答会有所帮助: