Python Forum

Full Version: Array input with time module (Python 3)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I have to generate a graphic from a .dat file in which the 2nd columns (which will be the x axis) is filled with different times of format HH:MM. The intervals between each time vary a lot (from 1 min to 7 min) and absolutely randomly.

I have tried :
import numpy as np
import matplotlib.pyplot as plt
import time

signal = np.genfromtxt('path\file.DAT')
Time = signal[:,1]
t = time.strptime(Time,'%I:%M')
y = signal[:,2]
plt.plot(t,y)
plt.show()
Running the program I get the following error message :
Error:
Traceback (most recent call last):

Sorry for the short ending,I made a typing error and for a reason I cannot repaste the full Error message at once and it is really long (it took me more than ten minutes to write it all down befoere editing and then I lost it all because the editing time had passed)...

At the end I get :
Error:
TypeError: strptime() argument 0 must be str, not <class 'numpy.ndarray'
You could try parsing the data in the .DAT file into a string before you create Time. Essentially, the argument your passing 'Time' is not what strptime() is expecting. Check the type of 'signal' and the type of 'Time' so you know what your dealing with:

print(type(signal), type(Time))
Thank you a lot for the advice. They are both numpy.ndarray... Can you help me in "parsing the data into a string" please? (I am really bad with vocabulary and found so many different things on the internet forums I am completely lost)
I have never used numpy before but a quick google search says there exists a function called: array_str() which converts a numpy array into a string: https://docs.scipy.org/doc/numpy-1.14.0/...y_str.html
OK, thank you a lot (for the link and all)! I'll try right away and see what comes of it! =)