7

I have a 2D array of dates of the form:

[Y Y Y ... ]
[M M M ... ]
[D D D ... ]
[H H H ... ]
[M M M ... ]
[S S S ... ]

So it looks like

data = np.array([
    [2015, 2015, 2015, 2015, 2015, 2015], # ...
    [   1,    1,    1,    1,    1,    1],
    [   1,    1,    1,    2,    2,    2],
    [  23,   23,   23,    0,    0,    0],
    [   4,    5,    5,   37,   37,   37],
    [  59,    1,    2,   25,   27,   29]
])

What would be the best way to convert this into one list of datetime objects?

3
  • Can you use pandas also or do you need to stick purely on numpy arrays? Commented Oct 20, 2016 at 22:20
  • And a follow-up question - can this be done without round-tripping through datetime.datetime, and producing a np.array of np.datetime64s? Commented Oct 21, 2016 at 1:00
  • @Boud, please expound on how pandas help here. Commented Sep 12, 2017 at 23:21

2 Answers 2

7
import datetime
import numpy as np

data = np.array(
    [[2015, 2015, 2015, 2015, 2015, 2015],
     [   1,    1,    1,    1,    1,    1],
     [   1,    1,    1,    2,    2,    2],
     [  23,   23,   23,    0,    0,    0],
     [   4,    5,    5,   37,   37,   37],
     [  59,    1,    2,   25,   27,   29]]
)

# Transpose the data so that columns become rows.
data = data.T

# A simple list comprehension does the trick, '*' making sure
# the values are unpacked for 'datetime.datetime'.
new_data = [datetime.datetime(*x) for x in data]

print(new_data)

[datetime.datetime(2015, 1, 1, 23, 4, 59), datetime.datetime(2015, 1, 1, 23, 5, 1), datetime.datetime(2015, 1, 1, 23, 5, 2), datetime.datetime(2015, 1, 2, 0, 37, 25), datetime.datetime(2015, 1, 2, 0, 37, 27), datetime.datetime(2015, 1, 2, 0, 37, 29)]

Sign up to request clarification or add additional context in comments.

1 Comment

Upvote. I was trying to concatenate datetime.date to the datetime object. (obviously I use from datetime import datetime to make the code easier to read) Nice to know this way works; I never saw this in the documentation.
0

If you want np.datetime64 objects, then this works:

import functools

units = 'YMDhms'
first_vals = np.array([1970, 1, 1, 0, 0, 0])
epoch = np.datetime64('1970')

results = functools.reduce(
    np.add,
    [
        d.astype('timedelta64[{}]'.format(unit))
        for d, unit in zip(data - first_vals[:,np.newaxis], units)
    ],
    epoch
)

Which gives:

array(['2015-01-01T23:04:59',
       '2015-01-01T23:05:01',
       '2015-01-01T23:05:02',
       '2015-01-02T00:37:25',
       '2015-01-02T00:37:27',
       '2015-01-02T00:37:29'], dtype='datetime64[s]')

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.