1

How can I create a list (or a numpy array if possible) in python that takes datetime objects in the first column and other data types in other columns?

For example, the list would be something like this:

list = [[<datetime object>, 0, 0.]
        [<datetime object>, 0, 0.]
        [<datetime object>, 0, 0.]]

What is the best way to create and initialize a list like this?

So far, I have tried using np.empty, np.zeros, and a list comprehension, similar to this:

list = [[None for x in xrange(3)] for x in xrange(3)]

But if I do this, I would need a for loop to populate the first column and there doesn't seem to be a way to assign it in a simpler way like the following:

list[0][:] = another_list_same_length

2 Answers 2

2

You mean something like:

In [17]: import numpy as np

In [18]: np.array([[[datetime.now(),np.zeros(2)] for x in range(10)]])
Out[18]: 
array([[[datetime.datetime(2014, 8, 7, 23, 45, 12, 151489),
         array([ 0.,  0.])],
        [datetime.datetime(2014, 8, 7, 23, 45, 12, 151560),
         array([ 0.,  0.])],
        [datetime.datetime(2014, 8, 7, 23, 45, 12, 151595),
         array([ 0.,  0.])],
        [datetime.datetime(2014, 8, 7, 23, 45, 12, 151619),
         array([ 0.,  0.])],
        [datetime.datetime(2014, 8, 7, 23, 45, 12, 151634),
         array([ 0.,  0.])],
        [datetime.datetime(2014, 8, 7, 23, 45, 12, 151648),
         array([ 0.,  0.])],
        [datetime.datetime(2014, 8, 7, 23, 45, 12, 151662),
         array([ 0.,  0.])],
        [datetime.datetime(2014, 8, 7, 23, 45, 12, 151677),
         array([ 0.,  0.])],
        [datetime.datetime(2014, 8, 7, 23, 45, 12, 151691),
         array([ 0.,  0.])],
        [datetime.datetime(2014, 8, 7, 23, 45, 12, 151706),
         array([ 0.,  0.])]]], dtype=object)
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the answer! The first one is similar to what I wrote in the question. The use of numpy in the second one might be what I am looking for, but it might have the same issue I pointed out at the end of my question. I have a separate list of timestamps (datetime objects) and I want to populate the first column using that list. Using this method, I will have to use a for loop to achieve that goal. I was curious to know if there is a better way to do it, to avoid a for loop and take advantage of capabilities of lists or arrays in python.
what is your datetime data in, a list or array?
It is a list. What I did for now is to use for loops and your answer definitely is helpful. But do you think there is a way to assign to the first column of this new array a list of datetime objects without the use of a for loop? something like this: array[0,:] = list_of_time
I just tried doing something like this with the array that you had suggested: my_array[:,0]=datetime_list and it worked without the need for a for loop. Thanks!
1

Use zip

>>> column1 = [1, 1, 1]
>>> column2 = [2, 2, 2]
>>> column3 = [3, 3, 3]
>>> zip(column1, column2, column3)
[(1, 2, 3), (1, 2, 3), (1, 2, 3)]
>>> # Or, if you'd like a list of lists:
... 
>>> [list(tup) for tup in zip(column1, column2, column3)]
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]
>>> 

This would allow you to build-up the columns separately and then combine them. column1 could be dates (or anything else.)

Hope this helps.

Comments

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.