# xarray.CFTimeIndex.factorize

#### CFTimeIndex.factorize(sort=False, use_na_sentinel=True)[[source]](https://github.com/pydata/xarray/blob/main/xarray/../.pixi/envs/doc/lib/python3.14/site-packages/pandas/core/base.py#L1329-L1476)

Encode the object as an enumerated type or categorical variable.

This method is useful for obtaining a numeric representation of an
array when all that matters is identifying distinct values. factorize
is available as both a top-level function [`pandas.factorize()`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.factorize.html#pandas.factorize),
and as a method `Series.factorize()` and `Index.factorize()`.

* **Parameters:**
  * **sort** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool), *default* [`False`](https://docs.python.org/3/builtins/constants.html#False)) – Sort uniques and shuffle codes to maintain the
    relationship.
  * **use_na_sentinel** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool), *default* [`True`](https://docs.python.org/3/builtins/constants.html#True)) – If True, the sentinel -1 will be used for NaN values. If False,
    NaN values will be encoded as non-negative integers and will not drop the
    NaN from the uniques of the values.
* **Returns:**
  * **codes** ([`ndarray`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray)) – An integer ndarray that’s an indexer into uniques.
    `uniques.take(codes)` will have the same values as values.
  * **uniques** ([`ndarray`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray), [`Index`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Index.html#pandas.Index), or [`Categorical`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Categorical.html#pandas.Categorical)) – The unique valid values. When values is Categorical, uniques
    is a Categorical. When values is some other pandas object, an
    Index is returned. Otherwise, a 1-D ndarray is returned.

    #### NOTE
    Even if there’s a missing value in values, uniques will
    *not* contain an entry for it.

#### SEE ALSO
`cut`
: Discretize continuous-valued array.

[`unique`](xarray.CFTimeIndex.unique.html.md#xarray.CFTimeIndex.unique)
: Find the unique value in an array.

### Notes

Reference [the user guide](https://pandas.pydata.org/pandas-docs/stable/user_guide/reshaping.html#reshaping-factorize) for more examples.

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('6a9d6bd9-e703-4308-b90d-30da88d14301','c69a8042-5354-4304-8256-000f6adc3c2c','4d6fc0f4-e17a-45fb-8862-268089f08448','../lite/tree/../notebooks/index.html?path=ca86be53_4a13_4afa_8b16_c8d45fef1a85.ipynb','None')">Try it in your browser!</button></div>

These examples all show factorize as a top-level method like
`pd.factorize(values)`. The results are identical for methods like
`Series.factorize()`.

```pycon
>>> codes, uniques = pd.factorize(
...     np.array(["b", "b", "a", "c", "b"], dtype="O")
... )
>>> codes
array([0, 0, 1, 2, 0])
>>> uniques
array(['b', 'a', 'c'], dtype=object)
```

With `sort=True`, the uniques will be sorted, and codes will be
shuffled so that the relationship is the maintained.

```pycon
>>> codes, uniques = pd.factorize(
...     np.array(["b", "b", "a", "c", "b"], dtype="O"), sort=True
... )
>>> codes
array([1, 1, 0, 2, 1])
>>> uniques
array(['a', 'b', 'c'], dtype=object)
```

When `use_na_sentinel=True` (the default), missing values are indicated in
the codes with the sentinel value `-1` and missing values are not
included in uniques.

```pycon
>>> codes, uniques = pd.factorize(
...     np.array(["b", None, "a", "c", "b"], dtype="O")
... )
>>> codes
array([ 0, -1,  1,  2,  0])
>>> uniques
array(['b', 'a', 'c'], dtype=object)
```

Thus far, we’ve only factorized lists (which are internally coerced to
NumPy arrays). When factorizing pandas objects, the type of uniques
will differ. For Categoricals, a Categorical is returned.

```pycon
>>> cat = pd.Categorical(["a", "a", "c"], categories=["a", "b", "c"])
>>> codes, uniques = pd.factorize(cat)
>>> codes
array([0, 0, 1])
>>> uniques
['a', 'c']
Categories (3, str): ['a', 'b', 'c']
```

Notice that `'b'` is in `uniques.categories`, despite not being
present in `cat.values`.

For all other pandas objects, an Index of the appropriate type is
returned.

```pycon
>>> cat = pd.Series(["a", "a", "c"])
>>> codes, uniques = pd.factorize(cat)
>>> codes
array([0, 0, 1])
>>> uniques
Index(['a', 'c'], dtype='str')
```

If NaN is in the values, and we want to include NaN in the uniques of the
values, it can be achieved by setting `use_na_sentinel=False`.

```pycon
>>> values = np.array([1, 2, 1, np.nan])
>>> codes, uniques = pd.factorize(values)  # default: use_na_sentinel=True
>>> codes
array([ 0,  1,  0, -1])
>>> uniques
array([1., 2.])
```

```pycon
>>> codes, uniques = pd.factorize(values, use_na_sentinel=False)
>>> codes
array([0, 1, 0, 2])
>>> uniques
array([ 1.,  2., nan])
```

<div id="4d6fc0f4-e17a-45fb-8862-268089f08448" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('6a9d6bd9-e703-4308-b90d-30da88d14301','4d6fc0f4-e17a-45fb-8862-268089f08448')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('6a9d6bd9-e703-4308-b90d-30da88d14301','4d6fc0f4-e17a-45fb-8862-268089f08448')">Open In Tab</button></div><div id="c69a8042-5354-4304-8256-000f6adc3c2c" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>
