|
| 1 | +# ***************************************************************************** |
| 2 | +# Copyright (c) 2020, Intel Corporation All rights reserved. |
| 3 | +# |
| 4 | +# Redistribution and use in source and binary forms, with or without |
| 5 | +# modification, are permitted provided that the following conditions are met: |
| 6 | +# |
| 7 | +# Redistributions of source code must retain the above copyright notice, |
| 8 | +# this list of conditions and the following disclaimer. |
| 9 | +# |
| 10 | +# Redistributions in binary form must reproduce the above copyright notice, |
| 11 | +# this list of conditions and the following disclaimer in the documentation |
| 12 | +# and/or other materials provided with the distribution. |
| 13 | +# |
| 14 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 15 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 16 | +# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 | +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 18 | +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 19 | +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 | +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 21 | +# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 22 | +# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 23 | +# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 24 | +# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | +# ***************************************************************************** |
| 26 | + |
| 27 | +from numba.extending import box, unbox, NativeValue |
| 28 | +from numba.core import boxing |
| 29 | +from numba.core.imputils import lower_constant |
| 30 | +from numba.np import arrayobj |
| 31 | +from numba import types |
| 32 | + |
| 33 | +from . import pandas_support |
| 34 | +from .types import ( |
| 35 | + CategoricalDtypeType, |
| 36 | + Categorical, |
| 37 | +) |
| 38 | + |
| 39 | + |
| 40 | +@box(CategoricalDtypeType) |
| 41 | +def box_CategoricalDtype(typ, val, c): |
| 42 | + pd_dtype = pandas_support.as_dtype(typ) |
| 43 | + return c.pyapi.unserialize(c.pyapi.serialize_object(pd_dtype)) |
| 44 | + |
| 45 | + |
| 46 | +@unbox(CategoricalDtypeType) |
| 47 | +def unbox_CategoricalDtype(typ, val, c): |
| 48 | + return NativeValue(c.context.get_dummy_value()) |
| 49 | + |
| 50 | + |
| 51 | +@box(Categorical) |
| 52 | +def box_Categorical(typ, val, c): |
| 53 | + pandas_module_name = c.context.insert_const_string(c.builder.module, "pandas") |
| 54 | + pandas_module = c.pyapi.import_module_noblock(pandas_module_name) |
| 55 | + |
| 56 | + constructor = c.pyapi.object_getattr_string(pandas_module, "Categorical") |
| 57 | + |
| 58 | + empty_list = c.pyapi.list_new(c.context.get_constant(types.intp, 0)) |
| 59 | + args = c.pyapi.tuple_pack([empty_list]) |
| 60 | + categorical = c.pyapi.call(constructor, args) |
| 61 | + |
| 62 | + dtype = box_CategoricalDtype(typ.pd_dtype, val, c) |
| 63 | + c.pyapi.object_setattr_string(categorical, "_dtype", dtype) |
| 64 | + |
| 65 | + codes = boxing.box_array(typ.codes, val, c) |
| 66 | + c.pyapi.object_setattr_string(categorical, "_codes", codes) |
| 67 | + |
| 68 | + c.pyapi.decref(codes) |
| 69 | + c.pyapi.decref(dtype) |
| 70 | + c.pyapi.decref(args) |
| 71 | + c.pyapi.decref(empty_list) |
| 72 | + c.pyapi.decref(constructor) |
| 73 | + c.pyapi.decref(pandas_module) |
| 74 | + return categorical |
| 75 | + |
| 76 | + |
| 77 | +@unbox(Categorical) |
| 78 | +def unbox_Categorical(typ, val, c): |
| 79 | + codes = c.pyapi.object_getattr_string(val, "codes") |
| 80 | + native_value = boxing.unbox_array(typ.codes, codes, c) |
| 81 | + c.pyapi.decref(codes) |
| 82 | + return native_value |
| 83 | + |
| 84 | + |
| 85 | +@lower_constant(Categorical) |
| 86 | +def constant_Categorical(context, builder, ty, pyval): |
| 87 | + """ |
| 88 | + Create a constant Categorical. |
| 89 | + """ |
| 90 | + return arrayobj.constant_array(context, builder, ty.codes, pyval.codes) |
0 commit comments