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

Commit ab559d9

Browse files
Rubtsowakozlov-alexey
authored andcommitted
Add setitem (#394)
* Impl operator setitem * change * Imp operator setitem for Series * comment old style * change * change * Add conditions in pd_series_ext for operator setitem * change * change * change * fix code style issues * change * change and add example for setitem (now not work) * change * change * change * change * change * change * change * change comment in examples * deleted brackets in message error for operator setitem * error message extension * solution PEP 8 issues * correctioned error message in test * add template for message error * correction examples, expansion tests * correction tests * correction check
1 parent 32e1af1 commit ab559d9

6 files changed

Lines changed: 468 additions & 51 deletions

File tree

examples/series_setitem_int.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# *****************************************************************************
2+
# Copyright (c) 2019, 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+
import numpy as np
28+
import pandas as pd
29+
30+
from numba import njit
31+
32+
33+
@njit
34+
def series_setitem():
35+
value = 0
36+
series = pd.Series(np.arange(5, 0, -1)) # Series of 5, 4, 3, 2, 1
37+
38+
series[0] = value
39+
40+
return series # result Series of 0, 4, 3, 2, 1
41+
42+
43+
print(series_setitem())

examples/series_setitem_series.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# *****************************************************************************
2+
# Copyright (c) 2019, 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+
import numpy as np
28+
import pandas as pd
29+
30+
from numba import njit
31+
32+
33+
@njit
34+
def series_setitem():
35+
value = 0
36+
series = pd.Series(np.arange(5, 0, -1)) # Series of 5, 4, 3, 2, 1
37+
38+
indices = pd.Series(np.asarray([1, 3]))
39+
series[indices] = value
40+
41+
return series # result Series of 5, 0, 3, 0, 1
42+
43+
44+
print(series_setitem())

examples/series_setitem_slice.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# *****************************************************************************
2+
# Copyright (c) 2019, 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+
import numpy as np
28+
import pandas as pd
29+
30+
from numba import njit
31+
32+
33+
@njit
34+
def series_setitem():
35+
value = 0
36+
series = pd.Series(np.arange(5, 0, -1)) # Series of 5, 4, 3, 2, 1
37+
38+
series[2:5] = value
39+
40+
return series # result Series of 5, 4, 0, 0, 0
41+
42+
43+
print(series_setitem())

sdc/datatypes/hpat_pandas_series_functions.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,104 @@ def hpat_pandas_series_getitem_idx_series_impl(self, idx):
141141
raise TypingError('{} The index must be an Integer, Slice or a pandas.series. Given: {}'.format(_func_name, idx))
142142

143143

144+
@sdc_overload(operator.setitem)
145+
def hpat_pandas_series_setitem(self, idx, value):
146+
"""
147+
Intel Scalable Dataframe Compiler User Guide
148+
********************************************
149+
Pandas API: pandas.Series.set
150+
151+
Examples
152+
--------
153+
.. literalinclude:: ../../../examples/series_setitem_int.py
154+
:language: python
155+
:lines: 27-
156+
:caption: Setting Pandas Series elements
157+
:name: ex_series_setitem
158+
159+
.. code-block:: console
160+
161+
> python ./series_setitem_int.py
162+
163+
0 0
164+
1 4
165+
2 3
166+
3 2
167+
4 1
168+
dtype: int64
169+
170+
> python ./series_setitem_slice.py
171+
172+
0 5
173+
1 4
174+
2 0
175+
3 0
176+
4 0
177+
dtype: int64
178+
179+
> python ./series_setitem_series.py
180+
181+
0 5
182+
1 0
183+
2 3
184+
3 0
185+
4 1
186+
dtype: int64
187+
188+
Intel Scalable Dataframe Compiler Developer Guide
189+
*************************************************
190+
Pandas Series operator :attr:`pandas.Series.set` implementation
191+
192+
Test: python -m sdc.runtests -k sdc.tests.test_series.TestSeries.test_series_setitem*
193+
194+
Parameters
195+
----------
196+
series: :obj:`pandas.Series`
197+
input series
198+
idx: :obj:`int`, :obj:`slice` or :obj:`pandas.Series`
199+
input index
200+
value: :object
201+
input value
202+
203+
Returns
204+
-------
205+
:class:`pandas.Series` or an element of the underneath type
206+
object of :class:`pandas.Series`
207+
"""
208+
209+
ty_checker = TypeChecker('Operator setitem.')
210+
ty_checker.check(self, SeriesType)
211+
212+
if not (isinstance(idx, (types.Integer, types.SliceType, SeriesType))):
213+
ty_checker.raise_exc(idx, 'int, Slice, Series', 'idx')
214+
215+
if not((isinstance(value, SeriesType) and isinstance(value.dtype, self.dtype)) or \
216+
isinstance(value, type(self.dtype))):
217+
ty_checker.raise_exc(value, self.dtype, 'value')
218+
219+
if isinstance(idx, types.Integer) or isinstance(idx, types.SliceType):
220+
def hpat_pandas_series_setitem_idx_integer_impl(self, idx, value):
221+
"""
222+
Test: python -m sdc.runtests sdc.tests.test_series.TestSeries.test_series_setitem_for_value
223+
Test: python -m sdc.runtests sdc.tests.test_series.TestSeries.test_series_setitem_for_slice
224+
"""
225+
self._data[idx] = value
226+
return self
227+
228+
return hpat_pandas_series_setitem_idx_integer_impl
229+
230+
if isinstance(idx, SeriesType):
231+
def hpat_pandas_series_setitem_idx_series_impl(self, idx, value):
232+
"""
233+
Test: python -m sdc.runtests sdc.tests.test_series.TestSeries.test_series_setitem_for_series
234+
"""
235+
super_index = idx._data
236+
self._data[super_index] = value
237+
return self
238+
239+
return hpat_pandas_series_setitem_idx_series_impl
240+
241+
144242
@sdc_overload_attribute(SeriesType, 'at')
145243
@sdc_overload_attribute(SeriesType, 'iat')
146244
@sdc_overload_attribute(SeriesType, 'iloc')

sdc/hiframes/pd_series_ext.py

Lines changed: 52 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -965,21 +965,22 @@ class CmpOpLESeries(SeriesCompEqual):
965965
class CmpOpLTSeries(SeriesCompEqual):
966966
key = '<'
967967

968-
# @infer_global(operator.getitem)
969-
# class GetItemBuffer(AbstractTemplate):
970-
# key = operator.getitem
971968

972-
# def generic(self, args, kws):
973-
# assert not kws
974-
# [ary, idx] = args
975-
# import pdb; pdb.set_trace()
976-
# if not isinstance(ary, SeriesType):
977-
# return
978-
# out = get_array_index_type(ary, idx)
979-
# # check result to be dt64 since it might be sliced array
980-
# # replace result with Timestamp
981-
# if out is not None and out.result == types.NPDatetime('ns'):
982-
# return signature(pandas_timestamp_type, ary, out.index)
969+
if sdc.config.config_pipeline_hpat_default:
970+
@infer_global(operator.getitem)
971+
class GetItemBuffer(AbstractTemplate):
972+
key = operator.getitem
973+
974+
def generic(self, args, kws):
975+
assert not kws
976+
[ary, idx] = args
977+
if not isinstance(ary, SeriesType):
978+
return
979+
out = get_array_index_type(ary, idx)
980+
# check result to be dt64 since it might be sliced array
981+
# replace result with Timestamp
982+
if out is not None and out.result == types.NPDatetime('ns'):
983+
return signature(pandas_timestamp_type, ary, out.index)
983984

984985

985986
def install_array_method(name, generic):
@@ -1105,44 +1106,44 @@ def generic(self, args, kws):
11051106
sig.return_type = pandas_timestamp_type
11061107
return sig
11071108

1109+
if sdc.config.config_pipeline_hpat_default:
1110+
@infer_global(operator.setitem)
1111+
class SetItemSeries(SetItemBuffer):
1112+
def generic(self, args, kws):
1113+
assert not kws
1114+
series, idx, val = args
1115+
if not isinstance(series, SeriesType):
1116+
return None
1117+
# TODO: handle any of args being Series independently
1118+
ary = series_to_array_type(series)
1119+
is_idx_series = False
1120+
if isinstance(idx, SeriesType):
1121+
idx = series_to_array_type(idx)
1122+
is_idx_series = True
1123+
is_val_series = False
1124+
if isinstance(val, SeriesType):
1125+
val = series_to_array_type(val)
1126+
is_val_series = True
1127+
# TODO: strings, dt_index
1128+
res = super(SetItemSeries, self).generic((ary, idx, val), kws)
1129+
if res is not None:
1130+
new_series = if_arr_to_series_type(res.args[0])
1131+
idx = res.args[1]
1132+
val = res.args[2]
1133+
if is_idx_series:
1134+
idx = if_arr_to_series_type(idx)
1135+
if is_val_series:
1136+
val = if_arr_to_series_type(val)
1137+
res.args = (new_series, idx, val)
1138+
return res
11081139

1109-
@infer_global(operator.setitem)
1110-
class SetItemSeries(SetItemBuffer):
1111-
def generic(self, args, kws):
1112-
assert not kws
1113-
series, idx, val = args
1114-
if not isinstance(series, SeriesType):
1115-
return None
1116-
# TODO: handle any of args being Series independently
1117-
ary = series_to_array_type(series)
1118-
is_idx_series = False
1119-
if isinstance(idx, SeriesType):
1120-
idx = series_to_array_type(idx)
1121-
is_idx_series = True
1122-
is_val_series = False
1123-
if isinstance(val, SeriesType):
1124-
val = series_to_array_type(val)
1125-
is_val_series = True
1126-
# TODO: strings, dt_index
1127-
res = super(SetItemSeries, self).generic((ary, idx, val), kws)
1128-
if res is not None:
1129-
new_series = if_arr_to_series_type(res.args[0])
1130-
idx = res.args[1]
1131-
val = res.args[2]
1132-
if is_idx_series:
1133-
idx = if_arr_to_series_type(idx)
1134-
if is_val_series:
1135-
val = if_arr_to_series_type(val)
1136-
res.args = (new_series, idx, val)
1137-
return res
1138-
1139-
1140-
@infer_global(operator.setitem)
1141-
class SetItemSeriesIat(SetItemSeries):
1142-
def generic(self, args, kws):
1143-
# iat[] is the same as regular setitem
1144-
if isinstance(args[0], SeriesIatType):
1145-
return SetItemSeries.generic(self, (args[0].stype, args[1], args[2]), kws)
1140+
if sdc.config.config_pipeline_hpat_default:
1141+
@infer_global(operator.setitem)
1142+
class SetItemSeriesIat(SetItemSeries):
1143+
def generic(self, args, kws):
1144+
# iat[] is the same as regular setitem
1145+
if isinstance(args[0], SeriesIatType):
1146+
return SetItemSeries.generic(self, (args[0].stype, args[1], args[2]), kws)
11461147

11471148

11481149
inplace_ops = [

0 commit comments

Comments
 (0)