KeyError:“ [datetime.time(12,19,29)不在索引中”

时间:2018-08-13 07:08:56

标签: python pandas matplotlib

数据以纪元纳秒为单位,如下例所示:

time               duration
1.530706769E+018    40000000000
1.53070683E+018     41000000000
1.530706891E+018    41000000000
1.530706951E+018    41000000000
1.530707011E+018    41000000000
1.530707068E+018    37000000000
1.530707128E+018    41000000000
1.530707188E+018    41000000000
1.530707248E+018    40000000000
1.530707313E+018    45000000000
1.530707368E+018    40000000000
1.530707432E+018    44000000000
1.530707492E+018    44000000000
1.530707552E+018    44000000000
1.530707612E+018    39000000000
1.530707673E+018    41000000000
1.530707729E+018    37000000000
1.530712952E+018    45000000000
1.530713007E+018    40000000000
1.530713067E+018    35000000000
1.530713132E+018    40000000000
1.530713187E+018    36000000000
1.530713248E+018    36000000000
1.530713308E+018    40000000000
1.530713369E+018    41000000000
1.530713429E+018    40000000000
1.53071349E+018     41000000000
1.530713551E+018    42000000000
1.530713611E+018    40000000000
1.530713671E+018    40000000000
1.530713732E+018    40000000000
1.530713792E+018    39000000000
1.530713852E+018    39000000000
1.530713913E+018    40000000000
1.530713973E+018    40000000000
1.530714029E+018    36000000000
1.53071409E+018     36000000000
1.530714151E+018    41000000000
1.530714212E+018    41000000000
1.530714272E+018    41000000000
1.530714332E+018    40000000000
1.530714393E+018    41000000000
1.530714454E+018    41000000000
1.530714515E+018    42000000000
1.530714574E+018    39000000000
1.53071463E+018     39000000000
1.530714694E+018    44000000000
1.530714753E+018    40000000000
1.530714814E+018    41000000000
1.530714874E+018    42000000000
1.530714934E+018    41000000000
1.530714995E+018    41000000000
1.530715056E+018    41000000000
1.530715112E+018    37000000000
1.530715245E+018    16000000000
1.530715275E+018    15000000000
1.530715305E+018    15000000000
1.530715335E+018    15000000000
1.530715366E+018    16000000000
1.530715396E+018    15000000000
1.530715427E+018    16000000000
1.530715456E+018    15000000000
1.530715487E+018    16000000000
1.530715517E+018    15000000000
1.530715548E+018    16000000000
1.530715578E+018    15000000000

要进行转换,我在熊猫中使用以下代码:

import pandas as pd
import matplotlib.pyplot as plt 
import datetime from datetime

df = pd.read_csv('d1.csv')

df['time']=pd.to_datetime(df['time'], unit='ns')

df['Time'] = df['time'].dt.time

以上代码将数据转换为以下形式:

df.head(20)
Out[100]: 
              time             d      Time
0  2018-07-04 12:19:29  4.000000e+10  12:19:29
1  2018-07-04 12:20:30  4.100000e+10  12:20:30
2  2018-07-04 12:21:31  4.100000e+10  12:21:31
3  2018-07-04 12:22:31  4.100000e+10  12:22:31
4  2018-07-04 12:23:31  4.100000e+10  12:23:31
5  2018-07-04 12:24:28  3.700000e+10  12:24:28
6  2018-07-04 12:25:28  4.100000e+10  12:25:28
7  2018-07-04 12:26:28  4.100000e+10  12:26:28
8  2018-07-04 12:27:28  4.000000e+10  12:27:28
9  2018-07-04 12:28:33  4.500000e+10  12:28:33
10 2018-07-04 12:29:28  4.000000e+10  12:29:28
11 2018-07-04 12:30:32  4.400000e+10  12:30:32
12 2018-07-04 12:31:32  4.400000e+10  12:31:32
13 2018-07-04 12:32:32  4.400000e+10  12:32:32
14 2018-07-04 12:33:32  3.900000e+10  12:33:32
15 2018-07-04 12:34:33  4.100000e+10  12:34:33
16 2018-07-04 12:35:29  3.700000e+10  12:35:29
17 2018-07-04 14:02:32  4.500000e+10  14:02:32
18 2018-07-04 14:03:27  4.000000e+10  14:03:27
19 2018-07-04 14:04:27  3.500000e+10  14:04:27

现在我需要一个Timed之间的散点图    所以我编码为:

x = df['Time']

y = df['d']

df.plot(x,y, 'scatter')

但是出现错误:

KeyError: '[datetime.time(12, 19, 29) datetime.time(12, 20, 30)\n datetime.time(12, 21, 31) ... datetime.time(23, 54, 39)\n datetime.time(23, 55, 16) datetime.time(23, 55, 52)] not in index'

此错误是什么意思...如何解决此问题并在此处获取散点图?

4 个答案:

答案 0 :(得分:1)

enter code here设置为索引并绘制

time

答案 1 :(得分:1)

您可以使用此:

GetDomainByName

答案 2 :(得分:0)

pandas.DataFrame.plot.scatter — pandas 0.23.4 documentation

  

x:int或str

     

用作每个点的水平坐标的列名或列位置。

     

y:int或str

     

用作每个点的垂直坐标的列名或列位置。

所以应该是

df.plot('Time','d','scatter')

答案 3 :(得分:0)

尝试使用matplotlib

将matplotlib.pyplot导入为plt

plt.plot(df ['Time'],df ['d'])