1

Here is my simple object:

[numpy.datetime64('2017-01-03T00:00:00.000000000'),
 numpy.datetime64('2017-01-04T00:00:00.000000000'),
 numpy.datetime64('2017-01-05T00:00:00.000000000'),
 numpy.datetime64('2017-01-06T00:00:00.000000000'),
 numpy.datetime64('2017-01-09T00:00:00.000000000'),
 numpy.datetime64('2017-01-10T00:00:00.000000000'),
 numpy.datetime64('2017-01-11T00:00:00.000000000'),
 numpy.datetime64('2017-01-12T00:00:00.000000000'),
 numpy.datetime64('2017-01-13T00:00:00.000000000'),
 numpy.datetime64('2017-01-16T00:00:00.000000000'),
 numpy.datetime64('2017-01-17T00:00:00.000000000'),
 numpy.datetime64('2017-01-18T00:00:00.000000000'),
 numpy.datetime64('2017-01-19T00:00:00.000000000'),
 numpy.datetime64('2017-01-20T00:00:00.000000000'),
 numpy.datetime64('2017-01-23T00:00:00.000000000'),
 numpy.datetime64('2017-01-24T00:00:00.000000000'),
 numpy.datetime64('2017-01-25T00:00:00.000000000'),
 numpy.datetime64('2017-01-26T00:00:00.000000000'),
 numpy.datetime64('2017-01-27T00:00:00.000000000'),
 numpy.datetime64('2017-02-01T00:00:00.000000000')]

instead of using a loop an empty list convert one by one, is there any shortcuts for that? Thanks.

3
  • 1
    Hmm, list comprehensions / generator expressions? But they still process items one by one. Commented Sep 23, 2017 at 5:14
  • 1
    stackoverflow.com/questions/34843513/… Does this help? Commented Sep 23, 2017 at 5:17
  • 1
    map a function?? Commented Sep 23, 2017 at 5:18

1 Answer 1

4

My favourite solution here would be one that seems a bit hidden in this thread: Converting between datetime, Timestamp and datetime64, which is to use tolist(). Because tolist() returns different types, depending on the array type, a conversion to ms is needed to get datetime objects. datetime objects can be directly plotted with matplotlib, or one can apply matplotlib.dates.date2num() on them.

So if a is the numpy array as above,

x = a.astype("M8[ms]").tolist()

results in a list of datetime objects.

Complete example:

import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime
import matplotlib.dates as mdates

a = np.array([np.datetime64('2017-01-03T00:00:00.000000000'),
     np.datetime64('2017-01-04T00:00:00.000000000'),
     np.datetime64('2017-01-05T00:00:00.000000000'),
     np.datetime64('2017-01-06T00:00:00.000000000'),
     np.datetime64('2017-01-09T00:00:00.000000000'),
     np.datetime64('2017-01-10T00:00:00.000000000'),
     np.datetime64('2017-01-11T00:00:00.000000000'),
     np.datetime64('2017-01-12T00:00:00.000000000'),
     np.datetime64('2017-01-13T00:00:00.000000000'),
     np.datetime64('2017-01-16T00:00:00.000000000'),
     np.datetime64('2017-01-17T00:00:00.000000000'),
     np.datetime64('2017-01-18T00:00:00.000000000'),
     np.datetime64('2017-01-19T00:00:00.000000000'),
     np.datetime64('2017-01-20T00:00:00.000000000'),
     np.datetime64('2017-01-23T00:00:00.000000000'),
     np.datetime64('2017-01-24T00:00:00.000000000'),
     np.datetime64('2017-01-25T00:00:00.000000000'),
     np.datetime64('2017-01-26T00:00:00.000000000'),
     np.datetime64('2017-01-27T00:00:00.000000000'),
     np.datetime64('2017-02-01T00:00:00.000000000')])

x = a.astype("M8[ms]").tolist()
y = np.random.rand(len(a))

plt.plot(x, y, color="limegreen")

plt.show()
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.