python pandas: how to calculate derivative/gradient
pd.Series.diff()
only takes the differences. It doesn't divide by the delta of the index as well.
This gets you the answer
recv.diff() / recv.index.to_series().diff().dt.total_seconds()2017-01-20 20:00:00 NaN2017-01-20 20:05:00 4521.4933332017-01-20 20:10:00 4533.7600002017-01-20 20:15:00 4557.4933332017-01-20 20:20:00 4536.0533332017-01-20 20:25:00 4567.8133332017-01-20 20:30:00 4406.1600002017-01-20 20:35:00 4366.7200002017-01-20 20:40:00 4407.5200002017-01-20 20:45:00 4421.173333Freq: 300S, dtype: float64
You could also use numpy.gradient
passing the bytes_in
and the delta you expect to have. This will not reduce the length by one, instead making assumptions about the edges.
np.gradient(bytes_in, 300) * 8array([ 4521.49333333, 4527.62666667, 4545.62666667, 4546.77333333, 4551.93333333, 4486.98666667, 4386.44 , 4387.12 , 4414.34666667, 4421.17333333])
A naive explanation would be that diff literally subtracts following entries while np.gradient uses a central difference scheme.
Can you explain why np.gradient doesn't produce the same results as the first proposed answer. – Darthtrader May 5 at 9:58
np.gradient uses a 2nd order scheme while .diff() uses a 1st order scheme. This means that the results from np.gradient will be continuous as will the derivative. The results from .diff() do not have to have a continuous derivative. Essentially np.gradient gives 'smoother' results.