Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9ad570b

Browse files
committed
Remove automatic sort order of categorical data if a non-ordinal type is specified
1 parent 54f5b0a commit 9ad570b

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

altair/utils/schemapi.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,14 @@ def to_dict(self, validate=True, ignore=None, context=None):
364364
# parsed_shorthand is removed from context if it exists so that it is
365365
# not passed to child to_dict function calls
366366
parsed_shorthand = context.pop("parsed_shorthand", {})
367+
# Prevent that pandas categorical data is automatically sorted
368+
# when a non-ordinal data type is specifed manually
369+
if "sort" in parsed_shorthand and kwds["type"] not in [
370+
"ordinal",
371+
Undefined,
372+
]:
373+
parsed_shorthand.pop("sort")
374+
367375
kwds.update(
368376
{
369377
k: v

tests/vegalite/v5/tests/test_api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,14 @@ def _check_encodings(chart):
166166
.encode(
167167
alt.X("x", type="nominal"),
168168
alt.Y("y", type="ordinal"),
169-
alt.Size("s", type="nominal", sort=None),
169+
alt.Size("s", type="nominal"),
170170
)
171171
)
172172
dct = chart.to_dict()
173173
assert dct["encoding"]["x"]["type"] == "nominal"
174174
assert dct["encoding"]["y"]["type"] == "ordinal"
175175
assert dct["encoding"]["size"]["type"] == "nominal"
176-
assert dct["encoding"]["size"]["sort"] is None
176+
assert "sort" not in dct["encoding"]["size"]
177177

178178

179179
@pytest.mark.parametrize(

tools/schemapi/schemapi.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,14 @@ def to_dict(self, validate=True, ignore=None, context=None):
362362
# parsed_shorthand is removed from context if it exists so that it is
363363
# not passed to child to_dict function calls
364364
parsed_shorthand = context.pop("parsed_shorthand", {})
365+
# Prevent that pandas categorical data is automatically sorted
366+
# when a non-ordinal data type is specifed manually
367+
if "sort" in parsed_shorthand and kwds["type"] not in [
368+
"ordinal",
369+
Undefined,
370+
]:
371+
parsed_shorthand.pop("sort")
372+
365373
kwds.update(
366374
{
367375
k: v

0 commit comments

Comments
 (0)