This repository was archived by the owner on Feb 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
Df.loc impl #788
Merged
Merged
Df.loc impl #788
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ce783ae
Df.loc impl
5715fc0
small fixes
e6b3ab1
fix
05de3d2
fix
2e3e8da
pep
9648125
add attr
721f060
add case of return dataframe
d861e3f
unify return values
1623b99
Merge branch 'master' into dfloc
1e-to 543d4ed
fix with sdc_take
28d6b38
add find idx with chunks
723b6ed
Add support list of lists in sdc_take
5477a98
pep
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| # ***************************************************************************** | ||
| # Copyright (c) 2020, Intel Corporation All rights reserved. | ||
| # | ||
| # Redistribution and use in source and binary forms, with or without | ||
| # modification, are permitted provided that the following conditions are met: | ||
| # | ||
| # Redistributions of source code must retain the above copyright notice, | ||
| # this list of conditions and the following disclaimer. | ||
| # | ||
| # Redistributions in binary form must reproduce the above copyright notice, | ||
| # this list of conditions and the following disclaimer in the documentation | ||
| # and/or other materials provided with the distribution. | ||
| # | ||
| # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | ||
| # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR | ||
| # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
| # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
| # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | ||
| # OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
| # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | ||
| # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, | ||
| # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| # ***************************************************************************** | ||
|
|
||
|
|
||
| """ | ||
| Expected result: | ||
| A 3.0 | ||
| B 6.0 | ||
| C 2.0 | ||
| Name: 2, dtype: float64 | ||
| """ | ||
|
|
||
| import pandas as pd | ||
| from numba import njit | ||
|
|
||
|
|
||
| @njit | ||
| def dataframe_loc(): | ||
| df = pd.DataFrame({'A': [1.0, 2.0, 3.0, 1.0], 'B': [4, 5, 6, 7], 'C': [4, 5, 2, 1]}) | ||
|
|
||
| return df.loc[2] | ||
|
|
||
|
|
||
| print(dataframe_loc()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1867,13 +1867,93 @@ def _df_getitem_unicode_idx_impl(self, idx): | |
| ty_checker.raise_exc(idx, expected_types, 'idx') | ||
|
|
||
|
|
||
| def df_getitem_single_label_loc_codegen(self, idx): | ||
| """ | ||
| Example of generated implementation: | ||
| def _df_getitem_single_label_loc_impl(self, idx): | ||
| idx_list = [] | ||
| for i in range(len(self._dataframe.index)): | ||
| if self._dataframe._index[i] == idx: | ||
| idx_list.append(i) | ||
| data_0 = [] | ||
| for i in numba.prange(len(idx_list)): | ||
| index_in_list_0 = idx_list[i] | ||
| data_0.append(self._dataframe._data[0][index_in_list_0]) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can't do |
||
| res_data_0 = pandas.Series(data_0) | ||
| data_1 = [] | ||
| for i in numba.prange(len(idx_list)): | ||
| index_in_list_1 = idx_list[i] | ||
| data_1.append(self._dataframe._data[1][index_in_list_1]) | ||
| res_data_1 = pandas.Series(data_1) | ||
| if len(idx_list) < 1: | ||
| raise IndexingError('Index is out of bounds for axis') | ||
| new_index = [] | ||
| for i in numba.prange(len(idx_list)): | ||
| new_index.append(self._dataframe._index[idx_list[i]]) | ||
| return pandas.DataFrame({"A": res_data_0, "B": res_data_1}, index=numpy.array(new_index)) | ||
| """ | ||
| if isinstance(self.index, types.NoneType): | ||
| fill_list = [' idx_list.append(idx)'] | ||
| new_index = [' new_index.append(idx)'] | ||
|
|
||
| else: | ||
| fill_list = [' for i in range(len(self._dataframe.index)):', | ||
| ' if self._dataframe._index[i] == idx:', | ||
| ' idx_list.append(i)'] | ||
| new_index = [' for i in numba.prange(len(idx_list)):', | ||
| ' new_index.append(self._dataframe._index[idx_list[i]])'] | ||
|
|
||
| fill_list_text = '\n'.join(fill_list) | ||
| new_index_text = '\n'.join(new_index) | ||
| func_lines = ['def _df_getitem_single_label_loc_impl(self, idx):', | ||
| ' idx_list = []', | ||
| f'{fill_list_text}'] | ||
| results = [] | ||
| for i, c in enumerate(self.columns): | ||
| data = f'data_{i}' | ||
| index_in_list = f'index_in_list_{i}' | ||
| res_data = f'res_data_{i}' | ||
| func_lines += [f' {data} = []', | ||
| ' for i in numba.prange(len(idx_list)):', | ||
| f' {index_in_list} = idx_list[i]', | ||
| f' {data}.append(self._dataframe._data[{i}][{index_in_list}])', | ||
| f' {res_data} = pandas.Series({data})'] | ||
| results.append((c, res_data)) | ||
|
|
||
| func_lines += [' if len(idx_list) < 1:', | ||
| " raise IndexingError('Index is out of bounds for axis')"] | ||
|
|
||
| data = ', '.join(f'"{col}": {data}' for col, data in results) | ||
| func_lines += [' new_index = []', | ||
| f'{new_index_text}', | ||
| f' return pandas.DataFrame({{{data}}}, index=numpy.array(new_index))'] | ||
|
|
||
| func_text = '\n'.join(func_lines) | ||
| global_vars = {'pandas': pandas, 'numpy': numpy, | ||
| 'numba': numba, | ||
| 'IndexingError': IndexingError} | ||
|
|
||
| return func_text, global_vars | ||
|
|
||
|
|
||
| gen_df_getitem_loc_single_label_impl = gen_impl_generator( | ||
| df_getitem_single_label_loc_codegen, '_df_getitem_single_label_loc_impl') | ||
|
|
||
|
|
||
| @sdc_overload(operator.getitem) | ||
| def sdc_pandas_dataframe_accessor_getitem(self, idx): | ||
| if not isinstance(self, DataFrameGetitemAccessorType): | ||
| return None | ||
|
|
||
| accessor = self.accessor.literal_value | ||
|
|
||
| if accessor == 'loc': | ||
| if isinstance(idx, types.Integer): | ||
| return gen_df_getitem_loc_single_label_impl(self.dataframe, idx) | ||
|
|
||
| ty_checker = TypeChecker('Attribute loc().') | ||
| ty_checker.raise_exc(idx, 'int', 'idx') | ||
|
|
||
| if accessor == 'iat': | ||
| if isinstance(idx, types.Tuple) and isinstance(idx[1], types.Literal): | ||
| col = idx[1].literal_value | ||
|
|
@@ -1944,6 +2024,57 @@ def sdc_pandas_dataframe_iat_impl(self): | |
| return sdc_pandas_dataframe_iat_impl | ||
|
|
||
|
|
||
| @sdc_overload_attribute(DataFrameType, 'loc') | ||
| def sdc_pandas_dataframe_loc(self): | ||
| """ | ||
| Intel Scalable Dataframe Compiler User Guide | ||
| ******************************************** | ||
|
|
||
| Pandas API: pandas.DataFrame.loc | ||
|
|
||
| Limitations | ||
| ----------- | ||
| - Loc always returns Dataframe. | ||
| - Parameter ``idx`` is supported only to be a single value, e.g. :obj:`df.loc['A']`. | ||
|
|
||
| Examples | ||
| -------- | ||
| .. literalinclude:: ../../../examples/dataframe/dataframe_loc.py | ||
| :language: python | ||
| :lines: 36- | ||
| :caption: Access a group of rows and columns by label(s) or a boolean array. | ||
| :name: ex_dataframe_loc | ||
|
|
||
| .. command-output:: python ./dataframe/dataframe_loc.py | ||
| :cwd: ../../../examples | ||
|
|
||
| .. seealso:: | ||
| :ref:`DataFrame.at <pandas.DataFrame.at>` | ||
| Access a single value for a row/column label pair. | ||
| :ref:`DataFrame.iloc <pandas.DataFrame.iloc>` | ||
| Access group of rows and columns by integer position(s). | ||
| :ref:`DataFrame.xs <pandas.DataFrame.xs>` | ||
| Returns a cross-section (row(s) or column(s)) from the Series/DataFrame. | ||
| :ref:`Series.loc <pandas.Series.loc>` | ||
| Access group of values using labels. | ||
|
|
||
| Intel Scalable Dataframe Compiler Developer Guide | ||
| ************************************************* | ||
| Pandas DataFrame method :meth:`pandas.DataFrame.loc` implementation. | ||
|
|
||
| .. only:: developer | ||
| Test: python -m sdc.runtests -k sdc.tests.test_dataframe.TestDataFrame.test_df_loc* | ||
| """ | ||
|
|
||
| ty_checker = TypeChecker('Attribute loc().') | ||
| ty_checker.check(self, DataFrameType) | ||
|
|
||
| def sdc_pandas_dataframe_loc_impl(self): | ||
| return sdc.datatypes.hpat_pandas_dataframe_getitem_types.dataframe_getitem_accessor_init(self, 'loc') | ||
|
|
||
| return sdc_pandas_dataframe_loc_impl | ||
|
|
||
|
|
||
| @sdc_overload_method(DataFrameType, 'pct_change') | ||
| def pct_change_overload(df, periods=1, fill_method='pad', limit=None, freq=None): | ||
| """ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1100,6 +1100,28 @@ def test_impl(df): | |
| msg = 'Index is out of bounds for axis' | ||
| self.assertIn(msg, str(raises.exception)) | ||
|
|
||
| def test_df_loc(self): | ||
| def test_impl(df): | ||
| return df.loc[4] | ||
|
|
||
| sdc_func = sdc.jit(test_impl) | ||
| idx = [3, 4, 1, 4, 0] | ||
| df = pd.DataFrame({"A": [3.2, 4.4, 7.0, 3.3, 1.0], | ||
| "B": [3, 4, 1, 0, 222], | ||
| "C": [3.1, 8.4, 7.1, 3.2, 1]}, index=idx) | ||
| pd.testing.assert_frame_equal(sdc_func(df), test_impl(df)) | ||
|
|
||
| @unittest.skip("SDC Dataframe.loc[] always return Dataframe") | ||
| def test_df_loc_no_idx(self): | ||
| def test_impl(df): | ||
| return df.loc[2] | ||
|
|
||
| sdc_func = sdc.jit(test_impl) | ||
| df = pd.DataFrame({"A": [3.2, 4.4, 7.0, 3.3, 1.0], | ||
| "B": [3, 4, 1, 0, 222], | ||
| "C": [3.1, 8.4, 7.1, 3.2, 1]}) | ||
| pd.testing.assert_frame_equal(sdc_func(df), test_impl(df)) | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add test with index not contained in indices DF. |
||
| def test_df_head(self): | ||
| def get_func(n): | ||
| def impl(a): | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would happen if
_indexisNone?Also it is better to do it in parallel. Split it into chunks, create list per chunk and then merge them
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.