Skip to content
This repository was archived by the owner on Feb 2, 2024. It is now read-only.

Commit ac05952

Browse files
author
Ehsan Totoni
committed
support unboxing series with string index
1 parent 517cbb1 commit ac05952

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

hpat/hiframes/boxing.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ def typeof_pd_dataframe(val, c):
4646
# register series types for import
4747
@typeof_impl.register(pd.Series)
4848
def typeof_pd_str_series(val, c):
49-
return SeriesType(_infer_series_dtype(val))
49+
index_type = _infer_index_type(val.index)
50+
is_named = val.name is not None
51+
return SeriesType(
52+
_infer_series_dtype(val), index=index_type, is_named=is_named)
5053

5154

5255
@typeof_impl.register(pd.Index)
@@ -124,7 +127,6 @@ def _infer_series_dtype(S):
124127
raise ValueError("data type for column {} not supported".format(S.name))
125128

126129

127-
128130
def _infer_series_list_dtype(S):
129131
for i in range(len(S)):
130132
first_val = S.iloc[i]
@@ -142,6 +144,15 @@ def _infer_series_list_dtype(S):
142144
"data type for column {} not supported".format(S.name))
143145

144146

147+
def _infer_index_type(index):
148+
# TODO: support proper inference
149+
if index.dtype == np.dtype('O') and len(index) > 0:
150+
first_val = index[0]
151+
if isinstance(first_val, str):
152+
return string_array_type
153+
return types.none
154+
155+
145156
@box(DataFrameType)
146157
def box_dataframe(typ, val, c):
147158
context = c.context
@@ -251,6 +262,14 @@ def unbox_series(typ, val, c):
251262
arr_obj = c.pyapi.object_getattr_string(val, "values")
252263
series = cgutils.create_struct_proxy(typ)(c.context, c.builder)
253264
series.data = _unbox_series_data(typ.dtype, typ.data, arr_obj, c).value
265+
# TODO: other indices
266+
if typ.index == string_array_type:
267+
index_obj = c.pyapi.object_getattr_string(val, "index")
268+
series.index = unbox_str_series(string_array_type, index_obj, c).value
269+
if typ.is_named:
270+
name_obj = c.pyapi.object_getattr_string(val, "name")
271+
series.name = numba.unicode.unbox_unicode_str(
272+
string_type, name_obj, c).value
254273
# TODO: handle index and name
255274
c.pyapi.decref(arr_obj)
256275
return NativeValue(series._getvalue())

hpat/tests/test_series.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,14 @@ def test_impl(A):
149149
hpat_func = hpat.jit(test_impl)
150150
self.assertEqual(hpat_func(df.A), test_impl(df.A))
151151

152+
def test_pass_series_index1(self):
153+
def test_impl(A):
154+
return A
155+
156+
S = pd.Series([3, 5, 6], ['a', 'b', 'c'], name='A')
157+
hpat_func = hpat.jit(test_impl)
158+
pd.testing.assert_series_equal(hpat_func(S), test_impl(S))
159+
152160
def test_series_attr1(self):
153161
def test_impl(A):
154162
return A.size

0 commit comments

Comments
 (0)