2

I have a numpy.ndarray of strings like that

HHMM = ['0000' '0001' '0002' '0003' '0004' '0005' '0006' '0007' '0008' '0009' ...]

Here the first two elements are the hour and the last two the minute. In order to convert to time format (using datetime), I want to separate this characters.

I tried doing

hour   = HHMM[::][0:2]
minute = HHMM[::][2:4]

but the result is this

print hour
['0000' '0001']

print minute
['0002' '0003']
4
  • What is your expected result? Do you want to end up with an array of datetime64 values or just arrays of strings? Commented Feb 4, 2016 at 12:34
  • I want to split the two first and the two last characters (hours and minutes strings) in order to convert in a integer individuallly. Once this was made, I can make a map and convert them in datetime format. I only need to separate in individually arrays. Commented Feb 4, 2016 at 12:39
  • HHMM.view('S2,S2') might split it into a 2 field structured array. Commented Feb 4, 2016 at 12:48
  • Is this a list or real array? Commented Feb 4, 2016 at 13:08

2 Answers 2

4

Why not simple comprehension lists:

[u[:2] for u in HHMM]
#Out[39]: ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09']

[u[-2:] for u in HHMM]
#Out[40]: ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09']
Sign up to request clarification or add additional context in comments.

Comments

1

Example:

for i in range(len(HHMM)):
    hour = HHMM[i][0:2]
    minute = HHMM[i][2:]
    print('Time: {}:{}'.format(hour, minute))

Output:

Time: 00:00
Time: 00:01
Time: 00:02
Time: 00:03
Time: 00:04
Time: 00:05
Time: 00:06
Time: 00:07
Time: 00:08
Time: 00:09

EDIT:

HHMM = ['0000', '0001', '0002', '0003', '0004', '0005', '0006', '0007', '0008', '0009']

hours = []
minutes = []

for i in range(len(HHMM)):
    hours.append(HHMM[i][0:2])
    minutes.append(HHMM[i][2:])

print('Hours: {}'.format(list(hours)))
print('Minutes: {}'.format(list(minutes)))

4 Comments

This only print the values, hour is not an array, still when I define hour = [ ] before making the iteration. The same for minute. When print hour and minute, just print the last value.
@nandhos dude, here you go - is this what you wanted? Now hours and minutes are a type of collection
sorry, I missed append!
@nandhos no worries man, thank you for acknowladging my contribution ;D

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.