Back to Influxdb

movingAverage() function

content/flux/v0/stdlib/universe/movingaverage.md

latest4.8 KB
Original Source
<!------------------------------------------------------------------------------ IMPORTANT: This page was generated from comments in the Flux source code. Any edits made directly to this page will be overwritten the next time the documentation is generated. To make updates to this documentation, update the function comments above the function definition in the Flux source code: https://github.com/influxdata/flux/blob/master/stdlib/universe/universe.flux#L1891-L1896 Contributing to Flux: https://github.com/influxdata/flux#contributing Fluxdoc syntax: https://github.com/influxdata/flux/blob/master/docs/fluxdoc.md ------------------------------------------------------------------------------->

movingAverage() calculates the mean of non-null values using the current value and n - 1 previous values in the _values column.

Moving average rules

  • The average over a period populated by n values is equal to their algebraic mean.
  • The average over a period populated by only null values is null.
  • Moving averages skip null values.
  • If n is less than the number of records in a table, movingAverage() returns the average of the available values.
Function type signature
js
(<-tables: stream[{A with _value: B}], n: int) => stream[{A with _value: float}] where B: Numeric

{{% caption %}} For more information, see Function type signatures. {{% /caption %}}

Parameters

n

({{< req >}}) Number of values to average.

tables

Input data. Default is piped-forward data (<-).

Examples

Calculate a three point moving average

js
import "sampledata"

sampledata.int()
    |> movingAverage(n: 3)

{{< expand-wrapper >}} {{% expand "View example input and output" %}}

Input data

_time_value*tag
2021-01-01T00:00:00Z-2t1
2021-01-01T00:00:10Z10t1
2021-01-01T00:00:20Z7t1
2021-01-01T00:00:30Z17t1
2021-01-01T00:00:40Z15t1
2021-01-01T00:00:50Z4t1
_time_value*tag
2021-01-01T00:00:00Z19t2
2021-01-01T00:00:10Z4t2
2021-01-01T00:00:20Z-3t2
2021-01-01T00:00:30Z19t2
2021-01-01T00:00:40Z13t2
2021-01-01T00:00:50Z1t2

Output data

_time_value*tag
2021-01-01T00:00:20Z5t1
2021-01-01T00:00:30Z11.333333333333334t1
2021-01-01T00:00:40Z13t1
2021-01-01T00:00:50Z12t1
_time_value*tag
2021-01-01T00:00:20Z6.666666666666667t2
2021-01-01T00:00:30Z6.666666666666667t2
2021-01-01T00:00:40Z9.666666666666666t2
2021-01-01T00:00:50Z11t2

{{% /expand %}} {{< /expand-wrapper >}}

Calculate a three point moving average with null values

js
import "sampledata"

sampledata.int(includeNull: true)
    |> movingAverage(n: 3)

{{< expand-wrapper >}} {{% expand "View example input and output" %}}

Input data

_time_value*tag
2021-01-01T00:00:00Z-2t1
2021-01-01T00:00:10Zt1
2021-01-01T00:00:20Z7t1
2021-01-01T00:00:30Zt1
2021-01-01T00:00:40Zt1
2021-01-01T00:00:50Z4t1
_time_value*tag
2021-01-01T00:00:00Zt2
2021-01-01T00:00:10Z4t2
2021-01-01T00:00:20Z-3t2
2021-01-01T00:00:30Z19t2
2021-01-01T00:00:40Zt2
2021-01-01T00:00:50Z1t2

Output data

_time_value*tag
2021-01-01T00:00:20Z2.5t1
2021-01-01T00:00:30Z7t1
2021-01-01T00:00:40Z7t1
2021-01-01T00:00:50Z4t1
_time_value*tag
2021-01-01T00:00:20Z0.5t2
2021-01-01T00:00:30Z6.666666666666667t2
2021-01-01T00:00:40Z8t2
2021-01-01T00:00:50Z10t2

{{% /expand %}} {{< /expand-wrapper >}}