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 62
Fix for supporting DF categoricals unboxing #923
Merged
AlexanderKalistratov
merged 1 commit into
IntelPython:master
from
kozlov-alexey:bugfix/add_df_boxing_categorical
Sep 8, 2020
Merged
Changes from all commits
Commits
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
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
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
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,124 @@ | ||||
| # ***************************************************************************** | ||||
| # 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. | ||||
| # ***************************************************************************** | ||||
|
|
||||
| from sdc.tests.test_base import TestCase | ||||
|
|
||||
| import numpy as np | ||||
| import pandas as pd | ||||
| import numba as nb | ||||
| from numba import types | ||||
|
|
||||
| from sdc.types import ( | ||||
| CategoricalDtypeType, | ||||
| Categorical, | ||||
| ) | ||||
|
|
||||
| from sdc.hiframes.pd_dataframe_type import DataFrameType | ||||
| from sdc.tests.test_utils import skip_numba_jit | ||||
|
|
||||
|
|
||||
| class DFCategoryTest(TestCase): | ||||
| """ | ||||
| Test for pandas DataFrames with CategoricalDtype. | ||||
| """ | ||||
|
|
||||
| def _pd_value(self): | ||||
| return pd.DataFrame({'A': pd.Categorical([1, 2, 3, 2, 1])}) | ||||
|
|
||||
| def test_typeof(self): | ||||
| pd_value = self._pd_value() | ||||
| nb_type = nb.typeof(pd_value) | ||||
|
|
||||
| assert(isinstance(nb_type, DataFrameType)) | ||||
| assert(nb_type.columns == ('A',)) | ||||
| assert(nb_type.index == types.none) | ||||
| assert(nb_type.data[0].pd_dtype == CategoricalDtypeType(categories=[1, 2, 3], ordered=False)) | ||||
| assert(nb_type.data[0] == Categorical(CategoricalDtypeType(categories=[1, 2, 3], ordered=False))) | ||||
|
|
||||
| def test_unboxing(self): | ||||
| @nb.njit | ||||
| def func(c): | ||||
| pass | ||||
|
|
||||
| pd_value = self._pd_value() | ||||
| func(pd_value) | ||||
|
|
||||
| def test_boxing(self): | ||||
| @nb.njit | ||||
| def func(c): | ||||
| return c | ||||
|
|
||||
| pd_value = self._pd_value() | ||||
| boxed = func(pd_value) | ||||
| assert(boxed.equals(pd_value)) | ||||
|
|
||||
| @skip_numba_jit("capturing DFs (not only categoricals) as freevar not working") | ||||
|
Contributor
Author
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. OK, here's what's going on: this doesn't test lowering, but test possibility of capture as freevar/const and it currently works only for categorical series since impl for them was added via sdc/sdc/datatypes/series/boxing.py Line 33 in 8c0914a
Probably the same can be done for DFs, non-categorical Series and other types, but it's outside the scope of this PR. |
||||
| def test_lowering(self): | ||||
| pd_value = self._pd_value() | ||||
|
|
||||
| @nb.njit | ||||
| def func(): | ||||
| return pd_value | ||||
|
|
||||
| boxed = func() | ||||
| assert(boxed.equals(pd_value)) | ||||
|
|
||||
| def test_constructor(self): | ||||
| @nb.njit | ||||
| def func(): | ||||
| return pd.DataFrame({'A': pd.Categorical([1, 2, 3, 2, 1])}) | ||||
|
|
||||
| boxed = func() | ||||
| assert(boxed.equals(self._pd_value())) | ||||
|
|
||||
| @skip_numba_jit("compiles, but category dtype not supported by df ctor") | ||||
| def test_constructor_list(self): | ||||
| @nb.njit | ||||
| def func(): | ||||
| return pd.DataFrame({'A': list("12321")}, dtype='category') | ||||
|
|
||||
| boxed = func() | ||||
| assert(boxed.equals(self._pd_value())) | ||||
|
|
||||
| @skip_numba_jit | ||||
| def test_constructor_CategoricalDtype(self): | ||||
| @nb.njit | ||||
| def func(): | ||||
| return pd.DataFrame(data={'A': np.array([1, 2, 3, 2, 1])}, | ||||
| dtype=pd.CategoricalDtype(categories=[1, 2, 3])) | ||||
|
|
||||
| boxed = func() | ||||
| assert(boxed.equals(self._pd_value())) | ||||
|
|
||||
| @skip_numba_jit | ||||
| def test_constructor_CategoricalDtype_list(self): | ||||
| @nb.njit | ||||
| def func(): | ||||
| return pd.DataFrame(data={'A': [1, 2, 3, 2, 1]}, | ||||
| dtype=pd.CategoricalDtype(categories=[1, 2, 3])) | ||||
|
|
||||
| boxed = func() | ||||
| assert(boxed.equals(self._pd_value())) | ||||
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.
Are there other methods which need the same fix?
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.
I think half of all DF and Series methods should have some special handling of Categoricals (see e.g. df.values). If we talk only about boxing/unboxing/fix_df_array/fix_df_index function, then probably no, since we don't support CategoricalIndexes yet. At least it looks so.