Skip to main content
MannKS Logo

MannKS

(Mann-Kendall Sen)

Robust Trend Analysis in Python


📦 Installation

pip install mannks

Requirements: Python 3.9+, NumPy, Pandas (≥ 2.2), SciPy, Matplotlib, Seaborn, Piecewise-Regression


✨ What is MannKS?

MannKS (Mann-Kendall Sen) is a Python package for detecting trends in time series data using non-parametric methods. It's specifically designed for environmental monitoring, water quality analysis, and other fields where data is messy, irregular, or contains detection limits.

When to Use MannKS

Use this package when your data has:

  • Irregular sampling intervals (daily → monthly → quarterly)
  • Censored values (measurements like <5 or >100)
  • Seasonal patterns you need to account for
  • No normal distribution (non-parametric methods don't require it)
  • Small to large sample sizes (Automatic support for n > 10,000)

🆕 What's New

Upgrading from 0.5.x? Version 0.6 is a correctness release. Several statistical defects were fixed and some defaults changed, so results can differ from earlier versions. Re-run analyses before comparing old and new results. The changelog lists every change.

Version Highlights Learn more
0.6.3 Scientific audit & correctness release. Corrects direct confidence interval indexing per Gilbert (1987) Eq. 16.8, implements full interval ordering for robust right-censored Mann-Kendall, fixes plotting figure memory leaks, optimizes rolling trend search and pair-sampling memory, and aligns seasonal test result fields while preserving 100% LWP-TRENDS parity. Changelog
0.6.2 32-bit / Pyodide compatibility. Fixes int64 → int32 safe-casting error in _iter_triu_blocks and standardizes internal indexing arrays on pointer-sized np.intp across all estimators. Changelog
0.6.1 Large data sets, robustly. Censored data now scale like uncensored data (1,000,000 values in about a second); exact mode is 4–9× faster with a third to two-thirds of the memory; seasonal tests use every observation; a reliable fast-mode error estimate; fixes for missing dates and for bootstrap intervals on large data. Changelog · Large Dataset Guide
0.6.0 Correctness release, audited against the LWP-TRENDS R code and NADA. Var(S) includes the tie and censoring corrections; censored slopes default to the Akritas–Theil–Sen estimator; results no longer depend on row order; exact 'full' mode. Changelog · R parity suite
0.5.0 Large data sets: fast mode estimates the slope from a sample of pairs. Example 35 · Large Dataset Guide
0.4.0 Segmented trends: segmented_trend_test finds breakpoints, then estimates a robust slope for each segment. Example 32 · Segmented Guide
0.3.0 Rolling trends: rolling_trend_test over moving windows, and compare_periods to test before vs. after an intervention. Example 31 · Rolling Guide
0.2.0 Block bootstrap for autocorrelated data (autocorr_method='block_bootstrap'). Example 29 · Methodology

🚀 Quick Start

import pandas as pd
from MannKS import prepare_censored_data, trend_test

# 1. Prepare data with censored values
# Converts strings like '<5' into a structured format
values = [10, 12, '<5', 14, 15, 18, 20, '<5', 25, 30]
dates = pd.date_range(start='2020-01-01', periods=len(values), freq='ME')
data = prepare_censored_data(values)

# 2. Run trend test
# slope_scaling converts slope from "per second" to "per year"
result = trend_test(
    x=data,
    t=dates,
    slope_scaling='year',
    x_unit='mg/L',
    plot_path='trend.png'
)

# 3. Interpret results
print(f"Trend: {result.classification}")
print(f"Slope: {result.slope:.2f} {result.slope_units}")
print(f"Confidence: {result.C:.2%}")

Output:

Trend: Highly Likely Increasing
Slope: 24.57 mg/L per year
Confidence: 99.23%

Trend Analysis Plot


🎯 Key Features

Core Functionality

  • Mann-Kendall Trend Test: Detect monotonic trends with statistical significance
  • Sen's Slope Estimator: Calculate trend magnitude with confidence intervals
  • Seasonal Analysis: Separate seasonal signals from long-term trends
  • Regional Aggregation: Combine results across multiple monitoring sites

Data Handling

  • Censored Data Support: Native handling of detection limits (<5, >100)
    • Slope estimators: Akritas-Theil-Sen (default), LWP-TRENDS-compatible, and drop-ambiguous-pairs
    • Handles left-censored, right-censored, and mixed censoring
  • Unequal Spacing: Uses actual time differences (not just rank order)
  • Missing Data: Automatically handles NaN values and missing seasons
  • Temporal Aggregation: Multiple strategies for high-frequency data
  • Large Data Sets: Exact test and fast slope estimation, tested up to 1,000,000 observations

Statistical Features

  • Continuous Confidence: Reports likelihood ("Highly Likely Increasing") not just p-values
  • Data Quality Checks: Automatic warnings for tied values, long runs, insufficient data
  • Robust Methods: Akritas-Theil-Sen estimator for censored data (default)
  • Flexible Testing: Kendall's Tau-a or Tau-b, custom significance levels
  • Rolling Trends: Analyze how trends evolve over time with rolling_trend_test. See Example 31.
  • Segmented Trends: Automatically detect breakpoints and analyze trends in segments with segmented_trend_test. See Example 32.
  • Block Bootstrap: Robust trend testing for autocorrelated data with automatic ACF-based block size selection. See bootstrap.md for details and Example 29.

📊 Example Use Cases

Seasonal Water Quality Trend

from MannKS import seasonal_trend_test, check_seasonality

# Check if seasonality exists (period=12 is inferred from season_type='month')
seasonality = check_seasonality(x=data, t=dates, season_type='month')
print(f"Seasonal pattern detected: {seasonality.is_seasonal}")

# Run seasonal trend test
result = seasonal_trend_test(
    x=data,
    t=dates,
    season_type='month',         # Infers period=12 automatically
    agg_method='robust_median',  # Aggregates multiple samples per month
    slope_scaling='year'
)

Regional Analysis Across Sites

from MannKS import regional_test

# Run trend tests for each site
site_results = []
for site in ['Site_A', 'Site_B', 'Site_C']:
    result = trend_test(x=site_data[site], t=dates)
    site_results.append({
        'site': site,
        's': result.s,
        'C': result.C
    })

# Aggregate regional trend
regional = regional_test(
    trend_results=pd.DataFrame(site_results),
    time_series_data=all_site_data,
    site_col='site'
)
print(f"Regional trend: {regional.DT}, confidence: {regional.CT:.2%}")

📈 Large Data Sets

result = trend_test(x_large, t_large)          # automatic: exact up to n = 5,000, fast above
result = trend_test(x, t, large_dataset_mode='fast', max_pairs=500_000)   # more pairs, more accuracy
print(result.computation_mode, result.approximation_error)

The Mann-Kendall test (S, Var(S), p) is exact in every mode, including for censored data and seasonal tests. Above 5,000 observations, only the slope and its confidence interval are estimated from a random sample of pairs (reproducible by default). approximation_error gives the 95% half-width of that sampling error.

Single core, default settings:

Observations Mode Uncensored 30% censored Peak memory
5,000 Full (exact) 1.2 s 1.5 s < 1 GB
50,000 Fast 0.1 s 0.2 s 0.2 GB
1,000,000 Fast (with a warning recommending aggregation) 1.0 s 1.2 s 0.5 GB

In simulations, fast-mode confidence intervals contained the true slope 95–96% of the time.

  • Exact slopes (large_dataset_mode='full') need memory for all n(n-1)/2 pairs. They are practical up to about n = 20,000, and above n = 46,340 MannKS uses fast mode instead.
  • High-frequency data: aggregating first with agg_method/agg_period (e.g. daily or monthly medians) is usually better than analysing every value.
  • Slower options at large n: the block bootstrap (about 0.05 s per resample at n = 50,000) and the breakpoint search of segmented_trend_test (over a minute at n = 30,000).

See the Large Dataset Guide for details and accuracy measurements.


⚠️ Important Limitations

Statistical Assumptions

  • Independence: Data points must be serially independent
    • Autocorrelation violates this and causes spurious significance
    • Pre-test with ACF or use block bootstrap methods if autocorrelated
  • Monotonic trend: Cannot detect U-shaped or cyclical patterns
  • Homogeneous variance: Most powerful when variance is constant over time

📚 Documentation

Detailed Guides

Examples

The Examples folder contains step-by-step tutorials from basic to advanced usage.


🔬 Validation

Validated against:

  • LWP-TRENDS R script (v2502), run live via Rscript. With the LWP settings (alpha=0.1, mk_test_method='lwp', ci_method='lwp', sens_slope_method='lwp', tau_method='a', tie_break_method='lwp', agg_method='lwp'), S, Var(S), Z, p, C, Cd, tau, the Sen slope and its confidence interval, and sen_probability agree to 1e-6 (relative). This holds on synthetic edge cases and on the LWP example data sets. The documented exceptions are cases where LWP-TRENDS declines to compute, and fast-mode sampling. Frozen R reference values are tested in tests/test_lwp_parity.py.
  • NADA::cenken (Akritas-Theil-Sen): agreement within NADA's own bisection tolerance (median 0.3%).
  • Exact permutation distributions and Monte Carlo (Type-I error, CI coverage, slope bias).

See validation/r_parity for the scripts, and validation/ for the scenario studies.


🙏 Acknowledgments

This package is heavily inspired by the excellent work of LandWaterPeople (LWP). The robust censored data handling and regional aggregation methods are based on their R scripts and methodologies.


📖 References

  1. Helsel, D.R. (2012). Statistics for Censored Environmental Data Using Minitab and R (2nd ed.). Wiley.
  2. Gilbert, R.O. (1987). Statistical Methods for Environmental Pollution Monitoring. Wiley.
  3. Hirsch, R.M., Slack, J.R., & Smith, R.A. (1982). Techniques of trend analysis for monthly water quality data. Water Resources Research, 18(1), 107-121.
  4. Mann, H.B. (1945). Nonparametric tests against trend. Econometrica, 13(3), 245-259.
  5. Sen, P.K. (1968). Estimates of the regression coefficient based on a particular kind of rank correlation. Journal of the American Statistical Association, 63(324), 1379-1389.
  6. Fraser, C., & Whitehead, A. L. (2022). Continuous measures of confidence in direction of environmental trends at site and other spatial scales. Environmental Challenges, 9, 100601.
  7. Fraser, C., Snelder, T., & Matthews, A. (2018). State and trends of river water quality in the Manawatu-Whanganui region. Report for Horizons Regional Council.

Metadata

Release files for mannks 0.6.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mannks 0.6.3
File Size Uploaded Provenance Actions
mannks-0.6.3.tar.gz 78.9 kB With provenance Details for mannks-0.6.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mannks 0.6.3
File Interpreter ABI Platform Actions
mannks-0.6.3-py3-none-any.whl Python 3 none any Details for mannks-0.6.3-py3-none-any.whl

Total release size: 170.6 kB

Release files / mannks-0.6.3.tar.gz

Download URL mannks-0.6.3.tar.gz
Size 78.9 kB
Tags Source
SHA-256 checksum
How to use checksums
a836e793304fd0e6cb2d7f63f53d802e99918fb4d96ba71fc1d8e5842230df51
BLAKE2b-256 checksum
How to use checksums
4ff0d974efff4ed57193bfc69658372d4edff4e4ef35687d0f5f3926595ca219
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.

Transparency log

Release files / mannks-0.6.3-py3-none-any.whl

Download URL mannks-0.6.3-py3-none-any.whl
Size 91.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e47c2c988fc3fd17d29dcbc4ebcab195ef79acc1117314f56662cd13627b4dd6
BLAKE2b-256 checksum
How to use checksums
6c73f58f5650e1011e722bd3c9401f01eb1bde8450c007995bf1c7a4e89ea57b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.

Transparency log

Security for mannks 0.6.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for mannks-0.6.3.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 9, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page