Matplotlib和熊猫添加到情节时出错?

时间:2018-08-21 17:01:28

标签: pandas matplotlib

编辑:添加了数据以供参考。

我正在使用jupyter实验室,并尝试将一些地块添加到现有地块中。我不明白为什么我在这里遇到错误或为什么捕获异常导致输出看起来正确...

ax = dataframe[columns].plot(title=title, ax=ax)
if(not guide_columns == None):
    dataframe[guide_columns].plot(linestyle="--", ax=ax)

它运行了,并且出现了一个长错误(底部)。但是情节仍然发生并且看起来正确。因此,我向其中添加了一个try / except。...

ax = dataframe[columns].plot(title=title, ax=ax)
if(not guide_columns == None):
   try:
      dataframe[guide_columns].plot(linestyle="--", ax=ax)
   except KeyError:
      pass

一切似乎都很高兴,但似乎错了。我认为这可能与熊猫生成数据框子集的方式有关吗?

数据

dataframe["+20% Rate"] = rate_avg * 1.2
dataframe["-20% Rate"] = rate_avg * 0.8
columns = ["Rate (uCi/m^3)", 'Temp (C)', 'Pressure (kPa)',
               'Airflow (L/min)', 'DC (V)', 'Battery (V)']
guide_columns = ["+20% Rate", "-20% Rate"]

print(dataframe.head())
                           ts        id      sn  Rate (uCi/m^3)  Temp (C)  \
Test Time                                                                   
0.0       2018-08-13 16:01:57  43367947  336434         257.868      21.3   
2.0       2018-08-13 16:01:59  43367948  336434         268.182      21.3   
4.0       2018-08-13 16:02:01  43367949  336434         261.251      21.3   
6.0       2018-08-13 16:02:03  43367950  336434         261.251      21.3   
8.0       2018-08-13 16:02:05  43367951  336434         249.005      21.3   

           Pressure (kPa)  Airflow (L/min)  DC (V)  Battery (V)   +20% Rate \
Test Time                                                                     
0.0                    95              1.6    12.0         11.1  281.312188   
2.0                    95              1.7    12.0         11.1  281.312188   
4.0                    94              1.8    12.0         11.1  281.312188   
6.0                    94              1.8    12.0         11.1  281.312188   
8.0                    94              1.9    11.9         11.0  281.312188   

            -20% Rate  
Test Time              
0.0        187.541459  
2.0        187.541459  
4.0        187.541459  
6.0        187.541459  
8.0        187.541459

错误(即使它正在绘图...):

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/pandas/core/indexes/base.py in 
get_loc(self, key, method, tolerance)
   3077             try:
-> 3078                 return self._engine.get_loc(key)
   3079             except KeyError:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in 
pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in 
pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: True

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-25-591dc1863d4e> in <module>()
     20         status_log ("Processing " + file)
     21         dat = load_data(source_folder + file)
---> 22         analyse(dat, out_folder)
     23 
     24         #Get stop time

<ipython-input-22-7f43f1fe67f2> in analyse(dat, output_folder)
     36 
     37         #Do user plots
---> 38         process_each_sn(sn_records, t_sn, output_folder)
     39 
     40         #Gen statistics on data

<ipython-input-24-4359c5fd8837> in process_each_sn(sn_records, sn, 
output_folder)
     27     #Plot Average per time
     28     #Do 1hr Avg
---> 29     m3100_plot_avg_of_hours(sn_records, sn, 1, output_folder, 
y_axis_log=True)
     30     #Do 8hr Avg
     31     m3100_plot_avg_of_hours(sn_records, sn, 8, output_folder, 
y_axis_log=True)

<ipython-input-20-306b064316e1> in m3100_plot_avg_of_hours(dataframe, sn, 
hr_avg_time, out_folder, y_axis_log)
    108     #Plot avg data for these columns
    109     columns = ["Rate (uCi/m^3)", 'Temp (C)', 'Pressure (kPa)', 
'Airflow (L/min)', 'DC (V)', 'Battery (V)']
--> 110     m3100_plot_single_sn(avg_data, sn, columns, title, out_folder, 
y_axis_log)
    111 
    112     #Convert time stamp to hours for table

<ipython-input-20-306b064316e1> in m3100_plot_single_sn(dataframe, sn, 
columns, title, out_folder, guide_columns, y_axis_log, fig_size)
     55     ax = dataframe[columns].plot(title="SN: " + str(sn) + " - " + 
title, ax=ax)
     56     if(not guide_columns == None):
---> 57         dataframe[guide_columns].plot(linestyle="--", ax=ax)
     58         #try:
     59             #dataframe.loc[:,guide_columns].plot(linestyle="--", 
ax=ax)

/usr/local/lib/python3.6/dist-packages/pandas/core/frame.py in 
__getitem__(self, key)
   2686             return self._getitem_multilevel(key)
   2687         else:
-> 2688             return self._getitem_column(key)
   2689 
   2690     def _getitem_column(self, key):

/usr/local/lib/python3.6/dist-packages/pandas/core/frame.py in 
_getitem_column(self, key)
   2693         # get column
   2694         if self.columns.is_unique:
-> 2695             return self._get_item_cache(key)
   2696 
   2697         # duplicate columns & possible reduce dimensionality

/usr/local/lib/python3.6/dist-packages/pandas/core/generic.py in 
_get_item_cache(self, item)
   2487         res = cache.get(item)
   2488         if res is None:
-> 2489             values = self._data.get(item)
   2490             res = self._box_item_values(item, values)
   2491             cache[item] = res

/usr/local/lib/python3.6/dist-packages/pandas/core/internals.py in get(self, 
item, fastpath)
   4113 
   4114             if not isna(item):
-> 4115                 loc = self.items.get_loc(item)
   4116             else:
   4117                 indexer = np.arange(len(self.items)) 
[isna(self.items)]

/usr/local/lib/python3.6/dist-packages/pandas/core/indexes/base.py in 
get_loc(self, key, method, tolerance)
   3078                 return self._engine.get_loc(key)
   3079             except KeyError:
-> 3080                 return 
self._engine.get_loc(self._maybe_cast_indexer(key))
   3081 
   3082         indexer = self.get_indexer([key], method=method, 
tolerance=tolerance)

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in 
pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in 
pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: True

0 个答案:

没有答案