# pysparky.functions.math_

*module*

[View source](https://github.com/PySparky/pysparky-pyspark-helper/blob/327582f8b776fe7638082ce4b5be8153d3ce63d3/pysparky/functions/math_.py#L1-L1)

## pysparky.functions.math_.cumsum

*function*

```python
def cumsum(column_or_name: ColumnOrName, partition_by: list[Column] | None = None, order_by_column: Column | None = None, is_normalized: bool = False, is_descending: bool = False, alias: str = 'cumsum') -> Column
```

Calculate the cumulative sum of a column, optionally partitioned by other columns.

**Parameters**

- `column_or_name` (`Column`) — The column for which to calculate the cumulative sum.
- `partition_by` (`list[Column]`) (default: `None`) — A list of columns to partition by. Defaults to an empty list.
- `order_by_column` (`Column | None`) (default: `None`) — The Column for order by, null for using the same column.
- `is_normalized` (`bool`) (default: `False`) — Whether to normalize the cumulative sum. Defaults to False.
- `is_descending` (`bool`) (default: `False`) — Whether to order the cumulative sum in descending order. Defaults to False.
- `alias` (`str`) (default: `'cumsum'`) — Alias for the resulting column. Defaults to "cumsum".

**Returns**

- `Column` (`Column`) — A column representing the cumulative sum.

> **Example**
>
> ```python
> >>> df = spark.createDataFrame([(1, "A", 10), (2, "A", 20), (3, "B", 30)], ["id", "category", "value"])
> >>> result_df = df.select("id", "category", "value", cumsum(F.col("value"), partition_by=[F.col("category")], is_descending=True))
> >>> result_df.show()
> +---+--------+-----+------+
> | id|category|value|cumsum|
> +---+--------+-----+------+
> |  1|       A|   10|    30|
> |  2|       A|   20|    20|
> |  3|       B|   30|    30|
> +---+--------+-----+------+
> ```

[View source](https://github.com/PySparky/pysparky-pyspark-helper/blob/327582f8b776fe7638082ce4b5be8153d3ce63d3/pysparky/functions/math_.py#L61-L118)

## pysparky.functions.math_.haversine_distance

*function*

```python
def haversine_distance(lat1: ColumnOrName, long1: ColumnOrName, lat2: ColumnOrName, long2: ColumnOrName) -> Column
```

Calculates the Haversine distance between two sets of latitude and longitude coordinates.

**Parameters**

- `lat1` (`ColumnOrName`) — The column containing the latitude of the first coordinate.
- `long1` (`ColumnOrName`) — The column containing the longitude of the first coordinate.
- `lat2` (`ColumnOrName`) — The column containing the latitude of the second coordinate.
- `long2` (`ColumnOrName`) — The column containing the longitude of the second coordinate.

**Returns**

- `Column` (`Column`) — The column containing the calculated Haversine distance.

> **Example**
>
> ```python
> >>> df = spark.createDataFrame([(52.1552, 5.3876, 59.9111, 10.7503)], ["lat1", "long1", "lat2", "long2"])
> >>> df.select(haversine_distance("lat1", "long1", "lat2", "long2")).first()[0]
> 923.8038
> ```

[View source](https://github.com/PySparky/pysparky-pyspark-helper/blob/327582f8b776fe7638082ce4b5be8153d3ce63d3/pysparky/functions/math_.py#L11-L58)

## pysparky.functions.math_.sumif

*function*

```python
def sumif(condition: Column, value: Union[Column, int, float] = 1, otherwise_value: Union[Column, int, float] = 0) -> Column
```

Return a conditional sum using Spark expressions.

**Parameters**

- `condition` (`Column`) — Boolean Spark expression to filter rows.
- `value` (`Union[Column, int, float]`) (default: `1`) — Column or scalar to sum when condition is True.
- `otherwise_value` (`Union[Column, int, float]`) (default: `0`) — Column or scalar to use when condition is False.

**Returns**

- `Column` (`Column`) — Spark aggregation expression that sums `value` when true,
- (`Column`) — otherwise `otherwise_value`.

> **Example**
>
> ```python
> >>> df = spark.createDataFrame([("A", 10), ("B", 20), ("A", 30)], ["category", "value"])
> >>> df.select(sumif(F.col("category") == "A").alias("count_a")).show()
> +-------+
> |count_a|
> +-------+
> |      2|
> +-------+
> 
> >>> df.select(sumif(F.col("category") == "A", F.col("value")).alias("sum_a")).show()
> +-----+
> |sum_a|
> +-----+
> |   40|
> +-----+
> ```

[View source](https://github.com/PySparky/pysparky-pyspark-helper/blob/327582f8b776fe7638082ce4b5be8153d3ce63d3/pysparky/functions/math_.py#L121-L155)
