detect_trends
End-to-End Trend Detection
detect_trends
detect_trends(df, value_col, date_col=None, plot=True, method_params=None, debug=False)
This is the main function that runs trend detection end-to-end.
It runs the full PyTrendy pipeline in five stages: signal smoothing, segment extraction, boundary refinement, metric analysis, and optional visualization.
It returns a PyTrendyResults object containing ranked, classified, and trend segments, ready for filtering, plotting, or export.
Furthermore, it identifies patterns such as uptrends, downtrends, flat regions, and noise by applying rolling statistics, segmentation heuristics, and post-processing refinements.
It optionally visualizes the results and returns a structured object containing segment metadata.
The pipeline includes:
- Signal Processing: Applies Savitzky-Golay smoothing and computes flags for flat and noisy regions.
- Segmentation: Extracts contiguous segments based on signal classification.
- Refinement: Adjusts segment boundaries and classifies trends as gradual or abrupt.
- Analysis: Computes metrics like total change, percent change, and signal-to-noise ratio.
- Visualization (optional): Plots the original signal with annotated segments.
Parameters:
-
(dfDataFrame) –Input time series data containing at least the specified
date_colandvalue_col. Thedate_colmust contain datetime-like values (daily frequency recommended). -
(value_colstr) –Name of the column containing the primary signal to analyse for trend detection.
-
(date_colstr | None, default:None) –Historically, this represents the name of the column containing dates, but pytrendy now allows for indexes of any type to be used. In general, this column represents a human readable reference to the x-position of the sequence. Normally this would be a date or timestamp, but any unique set of values could be used. Default is 'None', in which case an integer sequence will be generated and used to identify segments.
-
(plotbool, default:True) –If
True, generates a matplotlib plot showing the detected trend segments over the original signal. Defaults toTrue. -
(method_paramsdict, default:None) –Optional parameters to customize detection heuristics. Supported keys:
- abrupt_padding (
int): Number of days to pad around abrupt transitions. Defaults to0. - avoid_noise (
bool): Whether to avoid noisy segments in trend detection. Defaults toTrue.
- abrupt_padding (
-
(debugbool, default:False) –If
Truewill run in debug mode, outputting various additional plots and print statements. Only recommended for developers of pytrendy. Defaults toFalse.
Returns:
-
PyTrendyResults(PyTrendyResults) –An object encapsulating the detected segments and associated metadata. Use this object to access segment statistics, rankings, and export utilities.
Source code in pytrendy/detect_trends.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | |