Teradata Package for Python Function Reference | 20.00 - mdiff - 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 - 20.00
- Deployment
- VantageCloud
- VantageCore
- Edition
- Enterprise
- IntelliFlex
- VMware
- Product
- Teradata Package for Python
- Release Number
- 20.00.00.03
- Published
- December 2024
- ft:locale
- en-US
- ft:lastEdition
- 2024-12-19
- dita:id
- TeradataPython_FxRef_Enterprise_2000
- lifecycle
- latest
- Product Category
- Teradata Vantage
- teradataml.dataframe.sql.DataFrameColumn.mdiff = mdiff(width, sort_columns)
- DESCRIPTION:
Computes the moving difference for the current row and the preceding
"width"-1 rows in a partition, by sorting the rows according to
"sort_columns".
Note:
mdiff does not support below type of columns.
* BLOB
* BYTE
* CHAR
* CLOB
* DATE
* PERIOD_DATE
* PERIOD_TIME
* PERIOD_TIMESTAMP
* TIME
* TIMESTAMP
* VARBYTE
* VARCHAR
PARAMETERS:
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
RAISES:
TeradataMlException, TypeError
RETURNS:
ColumnExpression, also known as, teradataml DataFrameColumn.
NOTES:
* One must use DataFrame.assign() when using the aggregate functions on
ColumnExpression, also known as, teradataml DataFrameColumn.
* ColumnExpression specified in "sort_columns" should be from the same
teradataml DataFrame as that of the ColumnExpression invoking the function.
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
>>>
# Example 1: Calculate the moving sum for column 'Feb' by sorting the data
# on column accounts in ascending order, with window of size 2.
>>> df.assign(msum_feb=df.Feb.msum(2, df.accounts))
Feb Jan Mar Apr datetime msum_feb
accounts
Jones LLC 200.0 150.0 140.0 180.0 04/01/2017 290.0
Red Inc 200.0 150.0 140.0 NaN 04/01/2017 410.0
Yellow Inc 90.0 NaN NaN NaN 04/01/2017 290.0
Orange Inc 210.0 NaN NaN 250.0 04/01/2017 410.0
Blue Inc 90.0 50.0 95.0 101.0 04/01/2017 300.0
Alpha Co 210.0 200.0 215.0 250.0 04/01/2017 210.0
>>>
# Example 2: Calculate the moving sum for column 'Feb' by sorting the data
# on column 'Mar' in descending order and column 'accounts' in ascending order,
# with window of size 2.
>>> df.assign(msum_feb=df.Feb.msum(2, sort_columns=[df.Mar.desc(), df.accounts]))
Feb Jan Mar Apr datetime msum_feb
accounts
Red Inc 200.0 150.0 140.0 NaN 04/01/2017 400.0
Orange Inc 210.0 NaN NaN 250.0 04/01/2017 300.0
Yellow Inc 90.0 NaN NaN NaN 04/01/2017 300.0
Blue Inc 90.0 50.0 95.0 101.0 04/01/2017 290.0
Jones LLC 200.0 150.0 140.0 180.0 04/01/2017 410.0
Alpha Co 210.0 200.0 215.0 250.0 04/01/2017 210.0