Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring and filling an array in Python from fetching MySql array

Whenever I print out, for example, a table "time" which was in datetime data type in MySql, using python I see this:

((datetime.datetime(2015, 5, 2, 23, 39, 38),), (datetime.datetime(2015, 5, 2, 23, 40, 39),), (datetime.datetime(2015, 5, 2, 23, 41, 39),), (datetime.datetime(2015, 5, 2, 23, 42, 40),), (datetime.datetime(2015, 5, 2, 23, 43, 40),), (datetime.datetime(2015, 5, 2, 23, 43, 40),))

But in the MySql databases, the variables are defined like this:

| 2016-05-18 14:11:06 |
| 2016-05-18 14:12:04 |
| 2016-05-18 14:13:05 |
| 2016-05-18 14:14:04 |
| 2016-05-18 14:15:07 | ...

What I want is to declare an array and fill the time in datetime format.

For a float type variable, normally I do this

var1 = array('f',[0])

But how I can declare the "time" variable in datetime data type in python and fill it?

like image 928
user193422 Avatar asked Mar 01 '26 03:03

user193422


1 Answers

Numpy has a datetime64 data type - in your example we could do the following:

>>> a = datetime.datetime(2015, 5, 2, 23, 39, 38)
>>> np.array([a], dtype='datetime64[s]')
array(['2015-05-02T23:39:38'], dtype='datetime64[s]')
like image 164
CDJB Avatar answered Mar 03 '26 15:03

CDJB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!