0

I am trying to convert an array of strings of times in the times_from_text_file array into datetimes using a for loop, and then I want to plot those datetimes against another array called temperature, print false and return the values of the temperature array. The only time nothing will be plotted, just true printed, is if all the values in the temperature array are the same. The problem I am having is converting all the string times in my times_from_text_file array into datetimes which is resulting in the error below.

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

1 Answer 1

2

Use a list comprehension:

new_times = [datetime.datetime.strptime(x, "%H:%M") for x in final_times]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.