Teradata Package for Python Function Reference on VantageCloud Lake | 17.20 - mavg - Teradata Package for Python - Look here for syntax, methods and examples for the functions included in the Teradata Package for Python.

Teradata® Package for Python Function Reference on VantageCloud Lake

Deployment
VantageCloud
Edition
Lake
Product
Teradata Package for Python
Release Number
17.20
Publication Date
November 2022
Content Type
Programming Reference
Publication ID
B700-4500-112K
ft:locale
en-US
ft:lastEdition
2024-02-21
ft:sourceId
FTML
dita:id
TeradataPython_FxRef_Lake_1720
Product Category
Teradata Vantage
teradataml.dataframe.dataframe.DataFrame.mavg = mavg(self, width, sort_columns, drop_columns=False)
DESCRIPTION:
    Computes the moving average for the current row and the preceding
    "width"-1 rows in a partition, by sorting the rows according to
    "sort_columns".
    Note:
        mavg does not support below type of columns.
            * BLOB
            * BYTE
            * CHAR
            * CLOB
            * DATE
            * PERIOD_DATE
            * PERIOD_TIME
            * PERIOD_TIMESTAMP
            * TIME
            * TIMESTAMP
            * VARBYTE
            * VARCHAR            
 
PARAMETERS:
    width:
        Required Argument.
        Specifies the width of the partition. "width" must be
        greater than 0 and less than or equal to 4096.
        Types: int
 
    sort_columns:
        Required Argument.
        Specifies the columns to use for sorting.
        Note:
            "sort_columns" does not support CLOB and BLOB type of
            columns.
        Types: str (or) ColumnExpression (or) List of strings(str)
               or ColumnExpressions
 
    drop_columns:
        Optional Argument.
        Specifies whether to retain all the input DataFrame columns
        in the output or not. When set to False, columns from input
        DataFrame are retained, dropped otherwise.
        Default Value: False
        Types: bool
 
RAISES:
    TeradataMlException, TypeError
 
RETURNS:
    teradataml DataFrame.
 
EXAMPLES:
    # Load the data to run the example.
    >>> from teradataml import load_example_data
    >>> load_example_data("dataframe","sales")
 
    # Create teradataml dataframe.
    >>> df = DataFrame.from_table('sales')
    >>> print(df)
                  Feb    Jan    Mar    Apr    datetime
    accounts
    Blue Inc     90.0   50.0   95.0  101.0  04/01/2017
    Orange Inc  210.0    NaN    NaN  250.0  04/01/2017
    Red Inc     200.0  150.0  140.0    NaN  04/01/2017
    Yellow Inc   90.0    NaN    NaN    NaN  04/01/2017
    Jones LLC   200.0  150.0  140.0  180.0  04/01/2017
    Alpha Co    210.0  200.0  215.0  250.0  04/01/2017
    >>>
 
    # Sorts the Data on column accounts in ascending order and
    # calculates moving avergae on the window of size 2.
    >>> df.mavg(width=2, sort_columns=df.accounts)
                  Feb    Jan    Mar    Apr    datetime  mavg_Feb  mavg_Jan  mavg_Mar  mavg_Apr mavg_datetime
    accounts
    Jones LLC   200.0  150.0  140.0  180.0  04/01/2017     145.0     100.0     117.5     140.5    04/01/2017
    Red Inc     200.0  150.0  140.0    NaN  04/01/2017     205.0     150.0     140.0     250.0    04/01/2017
    Yellow Inc   90.0    NaN    NaN    NaN  04/01/2017     145.0     150.0     140.0       NaN    04/01/2017
    Orange Inc  210.0    NaN    NaN  250.0  04/01/2017     205.0     150.0     140.0     215.0    04/01/2017
    Blue Inc     90.0   50.0   95.0  101.0  04/01/2017     150.0     125.0     155.0     175.5    04/01/2017
    Alpha Co    210.0  200.0  215.0  250.0  04/01/2017     210.0     200.0     215.0     250.0    04/01/2017
    >>>
 
    # Sorts the Data on column accounts in ascending order and column
    # Feb in descending order, then calculates moving average by dropping
    # the input DataFrame columns on the window of size 2.
    >>> df.mavg(width=2, sort_columns=[df.accounts, df.Feb.desc()], drop_columns=True)
       mavg_Feb  mavg_Jan  mavg_Mar  mavg_Apr mavg_datetime
    0     145.0     100.0     117.5     140.5    04/01/2017
    1     205.0     150.0     140.0     250.0    04/01/2017
    2     145.0     150.0     140.0       NaN    04/01/2017
    3     205.0     150.0     140.0     215.0    04/01/2017
    4     150.0     125.0     155.0     175.5    04/01/2017
    5     210.0     200.0     215.0     250.0    04/01/2017
    >>>