Mann Kendall Test in Python for Trend Detection

Mann Kendall Original Test

import numpy as np
import pandas as pd
import pymannkendall as mk
import matplotlib.pyplot as plt
import statsmodels.api as sm
%matplotlib inline

# read Datset
df= pd.read_csv("daily-total-female-births-CA.csv")
from datetime import datetime
df['dated'] = pd.to_datetime(df['date'])
df = df.set_index('dated')
df.drop(['date'], axis=1, inplace=True)
fig, ax = plt.subplots(figsize=(12, 8)), lags=20, ax=ax)
import pymannkendall as mk
import matplotlib.pyplot as plt
import statsmodels.api as sm

print(mk.original_test(df, alpha=0.05))


Hamed and Rao Modified MK Test

shampoo_df = pd.read_csv('sales-of-shampoo-over-a-three-ye.csv', header=0)
shampoo_df.dropna(inplace=True) # Droping the last row which contains NaN
def preprocess_data(df):
processed_df = df.rename(columns={
'Sales of shampoo over a three year period': 'shampoo_sales'

processed_df['Month'] = pd.to_datetime(processed_df.Month.apply(lambda val: '190'+val))
processed_df = processed_df.set_index('Month')

return processed_df

processed_df = preprocess_data(shampoo_df)
# line plot
processed_df.plot(figsize=(16, 7));
fig, ax = plt.subplots(figsize=(12, 8)), lags=20, ax=ax);
print (mk.hamed_rao_modification_test(processed_df))

Output for Yue Wang, Trend Free Pre-Whiteneing & Pre-Whiteneing Tests

Seasonal MK Test

Passenger_data = pd.read_csv("AirPassengers.csv",parse_dates=['Month'],index_col='Month')





Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The power of data

A Deep Dive into the Variance-Covariance Matrices of Classical Linear Regression Models

Explaining data science, AI, ML and deep learning to management — a presentation and a script —…

Basics of Data Preprocessing

The Era of Data is here.

Facebook for Good

Mindful Machines: Neuroscience & Critical Theory for Ethical AI

Five Open-Source Machine learning libraries worth checking out

Performing Analysis of Meteorological Data

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
abhinaya rajaram

abhinaya rajaram

Data in Law

More from Medium

Soybean Crop Yield Prediction with ML Regression Techniques— Part 1: Tabular data

How to implement a VECM model

Dynamic Stochastic General Equilibrium (DSGE) Models — Free Course

Time Series Anomaly Detection with PyFBAD