you can use numpy.polyfit, you can provide order as Degree of the fitting polynomial.
Refer:numpy.polyfit documentation
import numpy as np
import pandas as pd
def trendline(data, order=1):
coeffs = np.polyfit(data.index.values, list(data), order)
slope = coeffs[-2]
return float(slope)
#Sample Dataframe
revenue = [0.85, 0.99, 1.01, 1.12, 1.25, 1.36, 1.28, 1.44]
year = [1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000]
# check if values are exactly same
if (len(set(revenue))) <= 1:
print(0)
else:
df = pd.DataFrame({'year': year, 'revenue': revenue})
slope = trendline(df['revenue'])
print(slope)
so now if the value of the slope is +ve the trend is increasing, if it is 0 trend is constant, else decreasing
In your given data slope is 0.0804761904762. So, the trend is increasing
Update: trendline fails in case of exactly constant value, you can add custom check (len(set(revenue))) <= 1
to verify, if that is the case return 0.