HM14
HM14

Reputation: 709

Create 3D array from existing 2D array in Python

I am trying to create an array that is with dimensions:

a(Days,Hours,Station)

I have hourly data for array 'a' for 2 stations over 61 days so currently I have this array with these dimensions:

a(1464,2)

Where the 1464 is the number of hourly data points per station that I have (24 hours*61 days). However I want to break it down even further and add another dimension that has Days so the dimensions would then be:

a(61 days,24 hours/day, 2 stations)

Any ideas on how I would correctly be able to take the array 'a' that I currently have and change it into these 3 dimensions?

Upvotes: 1

Views: 189

Answers (3)

Arnial
Arnial

Reputation: 1441

This will split array a to chunks with maximum length size.

def chunks( a, size ):
    arr = iter( a )
    for v in arr:
        tmp = [ v ]
        for i,v in zip( range( size - 1 ), arr ):
            tmp.append( v )

        yield tmp

splitted = list( chunks( a, 24 ) )

Upvotes: 1

Zach King
Zach King

Reputation: 1208

If you're first field is hours * days, a transformation would simply be: a(x, y) => a(x // 24, x % 24, y)

x // 24 is floor division so 1500 // 24 = 62, the days. You did not specify, but I assume the "Hours" field would be the remaining hours; so x % 24 gets the remaining hours, and 1500 % 25 = 12, the number of hours. Lastly, the station field remains the same.

I don't think you can modify the structure of a list/array in Python, so you would need to create a new one. I'm also not sure if you're actually using the built-in list or the array. I'm not too familiar with the array class so this isn't a complete answer, but I hope it points you in the right direction arithmetically.

Upvotes: 0

kcborys
kcborys

Reputation: 316

You could try to make a 61x24x2 array. This should work:

b = []
for i in xrange(61):
    b.append(a[i*61:(i+1)*61])

Upvotes: 1

Related Questions