Skip to content

Commit b269fa4

Browse files
committed
DOC: union_categoricals docstring examples (pandas-dev#16390)
Add examples and link to online documentation for the function union_categoricals.
1 parent a6fcec6 commit b269fa4

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

pandas/core/dtypes/concat.py

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

0 commit comments

Comments
 (0)