You can do something similar to the following, taking this sample of your data:
Copydata = {
'x': [1539071748.0, 1539071752.0, 1539071755.0, 1539071757.0, 1539071759.0,
1539071760.0, 1539071764.0, 1539071765.0, 1539071768.0, 1539071872.0,
1539071979.0, 1539071998.0, 1539072006.0, 1539072123.0, 1539072137.0,
1539072226.0, 1539072250.0, 1539072386.0, 1539072398.0, 1539072450.0,
1539072637.0, 1539073158.0, 1539073243.0, 1539073268.0, 1539073615.0,
1539074097.0, 1539074101.0, 1539074533.0, 1539074691.0, 1539074763.0,
1539075159.0, 1539075623.0],
'y': [707, 1212, 1616, 1818, 2020, 2121, 2323, 2424, 2525, 6969, 11009, 11716,
12019, 16059, 16564, 19493, 20099, 23533, 23836, 25149, 29896, 43127,
45147, 45753, 55045, 66761, 66862, 77467, 81204, 82921, 92718, 104434]
}
To compute your derivative (note here that data['y_p'] will be of size n-1, therefore data['y_p'][i] is actually an approximation of the derivative at (data['x'][i] + data['x'][i+1]) / 2):
Copyimport numpy as np
data['y_p'] = np.diff(data['y']) / np.diff(data['x'])
data['x_p'] = (np.array(data['x'])[:-1] + np.array(data['x'])[1:]) / 2
Then plot your results:
Copyimport matplotlib.pyplot as plt
plt.figure(1)
plt.plot(data['x'], data['y'], 'r')
plt.plot(data['x_p'], data['y_p'], 'b')
plt.show()
Answer from rahlf23 on Stack OverflowPython / Matplotlib - How to compute/plot derivative without hard-coding it? - Stack Overflow
scipy - Numerical derivative in python - Computational Science Stack Exchange
numpy - Get derivative of data in python - Stack Overflow
python - How can I plot derivatives with matplotlib? - Stack Overflow
Videos
Among many others, there are two following methods:
Method 1
You can use derivative from scipy that takes a function f and returns its derivative w.r.t t. So you don't have to define the derivative function f1(t) explicitly.
from scipy.misc import derivative
def f(t):
return np.power(1 + t, n) - n * t - 1
# Rest of the code
t = np.linspace(a, b, 4000)
g = f(t)
plt.plot(t, g, 'r') # plotting t, g separately
plt.plot(t, derivative(f, t, dx=0.001), 'g')
Method 2
You can use gradient function of NumPy which uses central differences and returns the same shape as the input array.
t, dt = np.linspace(a, b, 4000, retstep=True)
g1 = np.gradient(f(t), dt)
plt.plot(t, g1, 'g')

You can use sympy to calculate the derivative symbolically. If you have a nice mathematical expression, this gives a better accuracy than numerical methods.
Sympy has its own plot functions, but they can be cumbersome if you want to combine many elements. In those cases, it can be easier to use lambdify to convert them to numpy functions.
from sympy import Pow, lambdify
from sympy.abc import t, n
f = Pow(1 + t, n) - n * t - 1
f1 = f.diff(t) # result: -n + n*(t + 1)**n/(t + 1)
f_np = lambdify(t, f.subs(n, 10))
f1_np = lambdify(t, f1.subs(n, 10))
import numpy as np
from matplotlib import pyplot as plt
a = -2.2
b = +0.25
x = np.linspace(a, b, 1000)
plt.plot(x, f_np(x), 'r')
plt.plot(x, f1_np(x), 'g')
plt.axhline(0, color='k')
plt.axvline(0, color='k')
plt.show()

PS: Purely staying within sympy, plotting can happen as follows:
from sympy import Pow, plot
from sympy.abc import t, n
a = -2.2
b = +0.25
f = Pow(1 + t, n) - n * t - 1
f1 = f.diff(t)
p1 = plot(f.subs(n, 10), (t, a, b), line_color='r', show=False)
p2 = plot(f1.subs(n, 10), (t, a, b), line_color='g', show=False)
p1.append(p2[0])
p1.show()

You have a loss of significance problem. It means that when adding a large floating point number to a small one, the precision of the small one is partially lost as a numpy double can only hold 64 bits of information.
To solve this issue you have to make sure that the scale of the numbers you add/multiply/divide is not too different. One simple solution is dividing x by 1e9 or multiplying h by 1e9. If you do this you get essentially the same precision as in your example.
Also if x has high enough resolution a simpler way to numerically differentiate the function would be der = np.diff(y) / np.diff(x). This way you do not have to worry about setting h. However in this case note that dy is one element shorter that y, and dy[i] is actually an approximation of the derivative at `(x[i] + x[i+1]) / 2. So to plot it you would do:
der = np.diff(y) / np.diff(x)
x2 = (x[:-1] + x[1:]) / 2
plt.plot(x2, der, 'r', x, y, 'g', x, -np.sin(x),'b')
Show my plots using np. gradient()

import numpy as np
import matplotlib.pyplot as plt
pi = np.pi
x = np.arange(0,5*pi,0.2*pi)
y = np.cos(x)
der = np.gradient(y,x)
plt.plot(x, der, 'r', x, y, 'g', x, -np.sin(x),'b')
plt.show()
I can smooth the plots using spline. If you need, I will post.
You can use df.diff().eval('y/X') to compute the derivative:
ax = df.plot(x='X', y='y')
df = df.assign(derivative=df.diff().eval('y/X'))
df.plot(x='X', y='derivative', ax=ax)

If your dataframe is enough dense, one possible way could be via the numpy polyfit, which coupled with poly1.deriv could directly bring to the first derivative. Then with polyval you can get the maximum in your range and, finally, with roots (subtracting half of the first derivative maximum to the polynomial constant term) the value you're looking for.
Key points are the right choice of the fitting polynomial degree (check residuals), and keeping in mind that beyond the range of the dataframe everything is dramatically uncertain.
