2

I have a long text file that I am importing with numpy genfromtext:

00:00:01 W 348 18.2 55.9 049 1008.8 0.000 
00:00:02 W 012 12.5 55.9 049 1008.8 0.000 
00:00:03 W 012 12.5 55.9 049 1008.8 0.000
00:00:04 W 357 18.2 55.9 049 1008.8 0.000 
00:00:05 W 357 18.2 55.9 049 1008.8 0.000 
00:00:06 W 339 17.6 55.9 049 1008.8 0.000 

testdata = np.genfromtxt(itertools.islice(f_in, 0, None, 60),\
                         names=('time','ew','d12','s12','t12','p12'.....)

time = (testdata['time'])

This is organizing all the data into an array. The first column of data in the file is a timestamp for each row. In the text file it is formatted as 00:00:00 so in format (%H:%m:%s). However in the actual array that is generated, it turns it into 1900-01-01 00:00:00 . When plotting my data with time, I cannot get it to drop the Y-m-d.

I have tried time = time.strftime('%H:%M:%S') and dt.datetime.strptime(time.decode('ascii'), '%H:%M:%S')

Both do nothing. How can I turn my whole array of times to keep the original %H:%m:%s format without it adding in the %Y-%m-%d?

Jonathon
  • 251
  • 1
  • 7
  • 20

1 Answers1

2

EDIT: based on data provided, you can import your file like this:

str2date = lambda x: datetime.strptime(x.decode("utf-8"), '%H:%M:%S').time()

data = np.genfromtxt(itertools.islice(f_in, 0, None, 60), dtype=None,names=('time','ew','d12','s12','t12','p12'.....), delimiter=' ', converters = {0: str2date})

print(data['time'])

output:

00:00:01

Note than you would need to .decode("utf-8") your input to str2date since it accepts bytes. You can set your dtype in np.genfromtxt() according to your specific file content.

You can also use this if your data is in right format:

dt.datetime.strptime(time,"%H:%M:%S").time()
Ehsan
  • 12,072
  • 2
  • 20
  • 33
  • What would "start" be in this instance? – Jonathon Apr 20 '20 at 15:47
  • @Jonathon editted. Just your time. Same as your code. just add `.time()` to end of it to extract hours:minutes:secs/msecs. You might need to decode `time` too (not sure what format you saved your time in your data) – Ehsan Apr 20 '20 at 15:48
  • I tried that before and get ```strptime() argument 1 must be str, not numpy.ndarray```. When I do tostring(), I get ```strptime() argument 1 must be str, not bytes``` – Jonathon Apr 20 '20 at 15:53
  • @Jonathon Can you provide a sample of your data here? – Ehsan Apr 20 '20 at 15:57
  • 1
    I included it in the question – Jonathon Apr 20 '20 at 16:02
  • @Jonathon Please fine my edit to the post if it helps. Thank you. – Ehsan Apr 20 '20 at 16:22