0

I have lists of datetimes and values like this:

import datetime
x = [datetime.datetime(2016, 9, 26, 0, 0), datetime.datetime(2016, 9, 27, 0, 0), 
     datetime.datetime(2016, 9, 28, 0, 0), datetime.datetime(2016, 9, 29, 0, 0),
     datetime.datetime(2016, 9, 30, 0, 0), datetime.datetime(2016, 10, 1, 0, 0)]
y = [26060, 23243, 22834, 22541, 22441, 23248]

And can plot them like this:

import matplotlib.pyplot as plt
plt.plot(x, y)

I would like to be able to plot a smooth version using more x-points. So first I do this:

delta_t = max(x) - min(x)
N_points = 300
xnew = [min(x) + i*delta_t/N_points for i in range(N_points)]

Then attempting a spline fit with scipy:

from scipy.interpolate import spline
ynew = spline(x, y, xnew)

TypeError: Cannot cast array data from dtype('O') to dtype('float64') according to the rule 'safe'

What is the best way to proceed? I am open to solutions involving other libraries such as pandas or plotly.

2 Answers 2

1

You're trying to pass a list of datetimes to the spline function, which are Python objects (hence dtype('O')). You need to convert the datetimes to a numeric format first, and then convert them back if you wish:

int_x = [i.total_seconds() for i in x]
ynew = spline(int_x, y, xnew)

Edit: total_seconds() is actually a timedelta method, not for datetimes. However it looks like you sorted it out so I'll leave this answer as is.

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

Comments

1

Figured something out:

x_ts = [x_.timestamp() for x_ in x]
xnew_ts = [x_.timestamp() for x_ in xnew]

ynew = spline(x_ts, y, xnew_ts)
plt.plot(xnew, ynew)

This works very nicely, but I'm still open to ideas for simpler methods.

3 Comments

Look at np.datetime64; it represents a date as a float.
Hi @AlexG, after using .timestamp() to convert a datetime into a number, how to convert this number back to datetime?
@YQ.Wang try datetime.fromtimestamp

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.