2017-03-24 19 views
0

times_from_text_file配列の文字列の配列をforループを使ってdatetimesに変換しようとしています。次に、temperature、printという別の配列に対してこれらのdatetimesをプロットします。 falseを返し、温度配列の値を返します。温度配列のすべての値が同じであれば、何もプロットされず、実際に印刷されるだけです。私が抱えている問題は、times_from_text_file配列のすべての文字列をdatetimesに変換することで、以下のエラーが発生します。文字列の配列をmatplotlibのdatetimes pythonに変換する

def values_equal_np_array(temp, time_created_index, time_modified_index, times_from_text_file): 
    if (len(np.unique(temp)) == 1): 
     return True 
    else: 
     #tells user the temps are not equal for a specified interval of time 
     print ('False') 

     plt.xlabel('Time') 
     plt.ylabel('Temperature') 
     plt.title('Time vs Temperature')   

     #specifying the beginning to end interval of temperature collection 
     final_times = times_from_text_file[time_created_index:`time_modified_index]     

     for i in range(len(final_times)): 
      new_time = datetime.datetime.strptime(final_times, "%H:%M") 
      datetimes.append(new_time) 
      new_array[i] = new_time 

     final_times = matplotlib.dates.date2num(new_time) 
     matplotlib.pyplot.plot_date(new_array, temp) 
     plt.show() 

     #the values that appear in the non-uniform temperature array on display  
     return np.unique(temp) 

#testing the function 
values_equal_np_array([1, 1, 1, 2, 3, 4], 3, 8, ['10:15','12:31','12:32','1:33', '12:34', '1:35', '2:36', '2:37', '3:38', '1:39']) 

False 

--------------------------------------------------------------------------- 
TypeError         Traceback (most recent call last) 
<ipython-input-11-6151e84dc0c7> in <module>() 
----> 1 values_equal_np_array([1, 1, 1, 2, 3, 4], 3, 8, ['10:15','12:31','12:32','1:33', '12:34', '1:35', '2:36', '2:37', '3:38', '1:39']) 

<ipython-input-10-96555d9f1618> in values_equal_np_array(temp, time_created_index, time_modified_index, times_from_text_file) 
    15 
    16   for i in range(len(final_times)): 
---> 17    new_time = datetime.datetime.strptime(final_times, "%H:%M") 
    18    datetimes.append(new_time) 
    19    new_array[i] = new_time 

TypeError: strptime() argument 1 must be str, not list 

答えて

2

list comprehensionを使用します。

new_times = [datetime.datetime.strptime(x, "%H:%M") for x in final_times] 
関連する問題