Skip to content

Commit 2ffebfd

Browse files
authored
fix: Allow encoding of pl.Enum with smaller physicals (#23829)
1 parent b2b6248 commit 2ffebfd

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

crates/polars-json/src/json/write/serialize.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,20 @@ pub(crate) fn new_serializer<'a>(
491491
list_serializer::<i64>(array.as_any().downcast_ref().unwrap(), offset, take)
492492
},
493493
ArrowDataType::Dictionary(k, v, _) => match (k, &**v) {
494+
(IntegerType::UInt8, ArrowDataType::Utf8View) => {
495+
let array = array
496+
.as_any()
497+
.downcast_ref::<DictionaryArray<u8>>()
498+
.unwrap();
499+
dictionary_utf8view_serializer::<u8>(array, offset, take)
500+
},
501+
(IntegerType::UInt16, ArrowDataType::Utf8View) => {
502+
let array = array
503+
.as_any()
504+
.downcast_ref::<DictionaryArray<u16>>()
505+
.unwrap();
506+
dictionary_utf8view_serializer::<u16>(array, offset, take)
507+
},
494508
(IntegerType::UInt32, ArrowDataType::Utf8View) => {
495509
let array = array
496510
.as_any()

py-polars/tests/unit/io/test_json.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import polars as pl
2222
from polars.exceptions import ComputeError
23-
from polars.testing import assert_frame_equal
23+
from polars.testing import assert_frame_equal, assert_series_equal
2424

2525

2626
def test_write_json() -> None:
@@ -632,3 +632,11 @@ def test_ndjson_22229() -> None:
632632
]
633633

634634
assert pl.read_ndjson(io.StringIO("\n".join(li))).to_dict(as_series=False)
635+
636+
637+
def test_json_encode_enum_23826() -> None:
638+
s = pl.Series("a", ["b"], dtype=pl.Enum(["b"]))
639+
assert_series_equal(
640+
s.to_frame().select(c=pl.struct("a").struct.json_encode()).to_series(),
641+
pl.Series("c", ['{"a":"0"}'], pl.String),
642+
)

0 commit comments

Comments
 (0)