# tf.data.experimental.group_by_window

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.13.1/tensorflow/python/data/experimental/ops/grouping.py#L58-L107) |

A transformation that groups windows of elements by key and reduces them. (deprecated)

#### View aliases


**Compat aliases for migration**

See
[Migration guide](https://www.tensorflow.org/guide/migrate) for
more details.

[`tf.compat.v1.data.experimental.group_by_window`](https://www.tensorflow.org/api_docs/python/tf/data/experimental/group_by_window)

<br />

    tf.data.experimental.group_by_window(
        key_func, reduce_func, window_size=None, window_size_func=None
    )

> [!WARNING]
> **Deprecated:** THIS FUNCTION IS DEPRECATED. It will be removed in a future version. Instructions for updating: Use [`tf.data.Dataset.group_by_window(...)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/data/Dataset#group_by_window).

This transformation maps each consecutive element in a dataset to a key
using `key_func` and groups the elements by key. It then applies
`reduce_func` to at most `window_size_func(key)` elements matching the same
key. All except the final window for each key will contain
`window_size_func(key)` elements; the final window may be smaller.

You may provide either a constant `window_size` or a window size determined by
the key through `window_size_func`.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `key_func` | A function mapping a nested structure of tensors (having shapes and types defined by `self.output_shapes` and `self.output_types`) to a scalar [`tf.int64`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf#int64) tensor. |
| `reduce_func` | A function mapping a key and a dataset of up to `window_size` consecutive elements matching that key to another dataset. |
| `window_size` | A [`tf.int64`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf#int64) scalar [`tf.Tensor`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/Tensor), representing the number of consecutive elements matching the same key to combine in a single batch, which will be passed to `reduce_func`. Mutually exclusive with `window_size_func`. |
| `window_size_func` | A function mapping a key to a [`tf.int64`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf#int64) scalar [`tf.Tensor`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/Tensor), representing the number of consecutive elements matching the same key to combine in a single batch, which will be passed to `reduce_func`. Mutually exclusive with `window_size`. |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| A `Dataset` transformation function, which can be passed to [`tf.data.Dataset.apply`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/data/Dataset#apply). ||

<br />

<br />

<br />

<br />

| ## Raises ||
|---|---|
| `ValueError` | if neither or both of {`window_size`, `window_size_func`} are passed. |

<br />