Search code examples
pythonscientific-computing

Time-varying data: list of tuples vs 2D array?


My example code is in python but I'm asking about the general principle.

If I have a set of data in time-value pairs, should I store these as a 2D array or as a list of tuples? for instance, if I have this data:

v=[1,4,4,4,23,4]
t=[1,2,3,4,5,6]

Is it generally better to store it like this:

data=[v,t]

or as a list of tuples:

data=[(1,1),(4,2)(4,3)...]

Is there a "standard" way of doing this?


Solution

  • The aggregate array container is probably the best choice. Assuming that your time points are not regularly spaced (and therefore you need to keep track of it rather than just use the indexing), this allows you to take slices of your entire data set like:

    import numpy as np
    v=[1,4,4,4,23,4]
    t=[1,2,3,4,5,6]
    
    data = np.array([v,t])
    

    Then you could slice it to get a subset of the data easily:

    data[:,2:4]  #array([[4, 4],[3, 4]])
    
    ii = [1,2,5] # Fancy indexing
    data[:,ii] # array([[4, 4, 4],
               #        [2, 3, 6]])