Convert numpy array to list of datetimes

2024/9/22 12:48:30

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?

Answer
import datetime
import numpy as npdata = 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)]

https://en.xdnf.cn/q/71946.html

Related Q&A

PyQt: how to handle event without inheritance

How can I handle mouse event without a inheritance, the usecase can be described as follows:Suppose that I wanna let the QLabel object to handel MouseMoveEvent, the way in the tutorial often goes in th…

DHT22 Sensor import Adafruit_DHT error

So Ive properly attached DHT22 Humidity Sensor to my BeagleBone Black Rev C. Im running OS Mavericks on my MacBook Pro and I followed the directions provided by Adafruit on how to use my DHT22 The webs…

Whats the purpose of package.egg-info folder?

Im developing a python package foo. My project structure looks like this:. ├── foo │ ├── foo │ │ ├── bar.py │ │ ├── foo.py │ │ ├── __init__.py │ ├── README.md …

Implement Causal CNN in Keras for multivariate time-series prediction

This question is a followup to my previous question here: Multi-feature causal CNN - Keras implementation, however, there are numerous things that are unclear to me that I think it warrants a new quest…

How to decode a numpy array of dtype=numpy.string_?

I need to decode, with Python 3, a string that was encoded the following way:>>> s = numpy.asarray(numpy.string_("hello\nworld")) >>> s array(bhello\nworld, dtype=|S11)I tri…

Cosine similarity of word2vec more than 1

I used a word2vec algorithm of spark to compute documents vector of a text. I then used the findSynonyms function of the model object to get synonyms of few words. I see something like this: w2vmodel.f…

Handling empty case with tuple filtering and unpacking

I have a situation with some parallel lists that need to be filtered based on the values in one of the lists. Sometimes I write something like this to filter them:lista = [1, 2, 3] listb = [7, 8, 9] f…

pip3 install pyautogui fails with error code 1 Mac OS

I tried installing the autogui python extension:pip3 install pyautoguiAnd this installation attempt results in the following error message:Collecting pyautoguiUsing cached PyAutoGUI-0.9.33.zipComplete …

BERT get sentence embedding

I am replicating code from this page. I have downloaded the BERT model to my local system and getting sentence embedding. I have around 500,000 sentences for which I need sentence embedding and it is t…

Python Subversion wrapper library

In Subversions documentation theres an example of using Subversion from Python#!/usr/bin/python import svn.fs, svn.core, svn.reposdef crawl_filesystem_dir(root, directory):"""Recursively…