Search code examples
pythonarraysdimensions

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?


Solution

  • 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 ) )