Skip to content

Commit 619bdf1

Browse files
committed
feat: accept a scalar value in factor()
A scalar (int, float or str) passed to factor() within an aes() expression is now wrapped in a list before creating the Categorical.
1 parent fe15412 commit 619bdf1

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

doc/changelog.qmd

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ title: Changelog
9191

9292
### Enhancements
9393

94+
- The `factor` function available when evaluating expressions in
95+
[](:class:`~plotnine.aes`) now accepts a scalar value.
96+
97+
```python
98+
ggplot(mtcars, aes("wt", "mpg", color="factor(4)")) + geom_point()
99+
```
100+
94101
- Increased the default linespacing used by themes from `0.9` to `1.2`.
95102
This gives multiline titles, subtitles and captions a better balance between looking compact and looking crumpled.
96103

plotnine/mapping/_eval_environment.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424

2525
def factor(
26-
values: Sequence[Any],
26+
values: Sequence[Any] | float | str,
2727
categories: Sequence[Any] | None = None,
2828
ordered: bool | None = None,
2929
) -> pd.Categorical:
@@ -48,6 +48,9 @@ def factor(
4848
`categories` attribute (which in turn is the `categories` argument, if
4949
provided).
5050
"""
51+
if isinstance(values, (int, float, str)):
52+
values = [values]
53+
5154
return pd.Categorical(values, categories=categories, ordered=None) # pyright: ignore[reportReturnType]
5255

5356

0 commit comments

Comments
 (0)