3

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?

3
  • Can you explain what you mean by declare an array and fill the time in datetime format? I had never heard of the array library. Commented Nov 25, 2019 at 0:14
  • If you see the the variable i showed for float type 'var1 = array('f',[0])', how would you do this for datetime data type? Commented Nov 25, 2019 at 0:16
  • I still don't understand. Can you explain what that code is doing? It would also help if you could like all the code and data relevant to this question. See: minimal reproducible example. Commented Nov 25, 2019 at 0:22

1 Answer 1

1

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]')
Sign up to request clarification or add additional context in comments.

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.