Skip to content

Commit bb92e9f

Browse files
committed
fix(pandas-format): convert map keys
1 parent e98fa3c commit bb92e9f

File tree

1 file changed

+44
-15
lines changed

1 file changed

+44
-15
lines changed

ibis/formats/pandas.py

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import contextlib
4-
import json
54
import warnings
65

76
import numpy as np
@@ -240,6 +239,9 @@ def convert_Struct_element(cls, dtype):
240239
converters = tuple(map(cls.get_element_converter, dtype.types))
241240

242241
def convert(values, names=dtype.names, converters=converters):
242+
if values is None:
243+
return values
244+
243245
items = values.items() if isinstance(values, dict) else zip(names, values)
244246
return {
245247
k: converter(v) if v is not None else v
@@ -250,19 +252,24 @@ def convert(values, names=dtype.names, converters=converters):
250252

251253
@classmethod
252254
def convert_JSON_element(cls, _):
253-
def try_json(x):
254-
if x is None:
255-
return x
255+
import json
256+
257+
def convert(value):
258+
if value is None:
259+
return value
256260
try:
257-
return json.loads(x)
261+
return json.loads(value)
258262
except (TypeError, json.JSONDecodeError):
259-
return x
263+
return value
260264

261-
return try_json
265+
return convert
262266

263267
@classmethod
264268
def convert_Timestamp_element(cls, dtype):
265269
def converter(value, dtype=dtype):
270+
if value is None:
271+
return value
272+
266273
with contextlib.suppress(AttributeError):
267274
value = value.item()
268275

@@ -276,23 +283,45 @@ def converter(value, dtype=dtype):
276283
@classmethod
277284
def convert_Array_element(cls, dtype):
278285
convert_value = cls.get_element_converter(dtype.value_type)
279-
return lambda values: [
280-
convert_value(value) if value is not None else value for value in values
281-
]
286+
287+
def convert(values):
288+
if values is None:
289+
return values
290+
291+
return [
292+
convert_value(value) if value is not None else value for value in values
293+
]
294+
295+
return convert
282296

283297
@classmethod
284298
def convert_Map_element(cls, dtype):
299+
convert_key = cls.get_element_converter(dtype.key_type)
285300
convert_value = cls.get_element_converter(dtype.value_type)
286-
return lambda row: {
287-
key: convert_value(value) if value is not None else value
288-
for key, value in dict(row).items()
289-
}
301+
302+
def convert(raw_row):
303+
if raw_row is None:
304+
return raw_row
305+
306+
row = dict(raw_row)
307+
return dict(
308+
zip(map(convert_key, row.keys()), map(convert_value, row.values()))
309+
)
310+
311+
return convert
290312

291313
@classmethod
292314
def convert_UUID_element(cls, _):
293315
from uuid import UUID
294316

295-
return lambda v: v if isinstance(v, UUID) else UUID(v)
317+
def convert(value):
318+
if value is None:
319+
return value
320+
elif isinstance(value, UUID):
321+
return value
322+
return UUID(value)
323+
324+
return convert
296325

297326

298327
class DaskData(PandasData):

0 commit comments

Comments
 (0)