Skip to content

Commit baf385f

Browse files
GuessWhoSamFoostangirala
authored andcommitted
DOC: Added examples for union_categoricals (pandas-dev#16397)
closes pandas-dev#16390
1 parent 99855c0 commit baf385f

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

pandas/core/dtypes/concat.py

+68
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,74 @@ def union_categoricals(to_union, sort_categories=False, ignore_order=False):
242242
- sort_categories=True and Categoricals are ordered
243243
ValueError
244244
Empty list of categoricals passed
245+
246+
Notes
247+
-----
248+
249+
To learn more about categories, see `link
250+
<http://pandas.pydata.org/pandas-docs/stable/categorical.html#unioning>`__
251+
252+
Examples
253+
--------
254+
255+
>>> from pandas.api.types import union_categoricals
256+
257+
If you want to combine categoricals that do not necessarily have
258+
the same categories, `union_categoricals` will combine a list-like
259+
of categoricals. The new categories will be the union of the
260+
categories being combined.
261+
262+
>>> a = pd.Categorical(["b", "c"])
263+
>>> b = pd.Categorical(["a", "b"])
264+
>>> union_categoricals([a, b])
265+
[b, c, a, b]
266+
Categories (3, object): [b, c, a]
267+
268+
By default, the resulting categories will be ordered as they appear
269+
in the `categories` of the data. If you want the categories to be
270+
lexsorted, use `sort_categories=True` argument.
271+
272+
>>> union_categoricals([a, b], sort_categories=True)
273+
[b, c, a, b]
274+
Categories (3, object): [a, b, c]
275+
276+
`union_categoricals` also works with the case of combining two
277+
categoricals of the same categories and order information (e.g. what
278+
you could also `append` for).
279+
280+
>>> a = pd.Categorical(["a", "b"], ordered=True)
281+
>>> b = pd.Categorical(["a", "b", "a"], ordered=True)
282+
>>> union_categoricals([a, b])
283+
[a, b, a, b, a]
284+
Categories (2, object): [a < b]
285+
286+
Raises `TypeError` because the categories are ordered and not identical.
287+
288+
>>> a = pd.Categorical(["a", "b"], ordered=True)
289+
>>> b = pd.Categorical(["a", "b", "c"], ordered=True)
290+
>>> union_categoricals([a, b])
291+
TypeError: to union ordered Categoricals, all categories must be the same
292+
293+
New in version 0.20.0
294+
295+
Ordered categoricals with different categories or orderings can be
296+
combined by using the `ignore_ordered=True` argument.
297+
298+
>>> a = pd.Categorical(["a", "b", "c"], ordered=True)
299+
>>> b = pd.Categorical(["c", "b", "a"], ordered=True)
300+
>>> union_categoricals([a, b], ignore_order=True)
301+
[a, b, c, c, b, a]
302+
Categories (3, object): [a, b, c]
303+
304+
`union_categoricals` also works with a `CategoricalIndex`, or `Series`
305+
containing categorical data, but note that the resulting array will
306+
always be a plain `Categorical`
307+
308+
>>> a = pd.Series(["b", "c"], dtype='category')
309+
>>> b = pd.Series(["a", "b"], dtype='category')
310+
>>> union_categoricals([a, b])
311+
[b, c, a, b]
312+
Categories (3, object): [b, c, a]
245313
"""
246314
from pandas import Index, Categorical, CategoricalIndex, Series
247315

0 commit comments

Comments
 (0)