-
Notifications
You must be signed in to change notification settings - Fork 4k
ARROW-7168: [Python] Respect the specified dictionary type for pd.Categorical conversion #5866
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
jorisvandenbossche
wants to merge
5
commits into
apache:master
from
jorisvandenbossche:ARROW-7168-categorical-specified-type
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3535a56
ARROW-7168: [Python] Respect the specified dictionary type when conve…
jorisvandenbossche bfb8237
additional tests
jorisvandenbossche 003e653
for now use deprecation warnings instead of error
jorisvandenbossche e4dbb2c
try fix python 2
jorisvandenbossche 39ff8e8
more python 2
jorisvandenbossche 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3212,6 +3212,80 @@ def test_variable_dictionary_to_pandas(): | |
| tm.assert_series_equal(result_dense, expected_dense) | ||
|
|
||
|
|
||
| def test_dictionary_from_pandas(): | ||
| cat = pd.Categorical([u'a', u'b', u'a']) | ||
| expected_type = pa.dictionary(pa.int8(), pa.string()) | ||
|
|
||
| result = pa.array(cat) | ||
| assert result.to_pylist() == ['a', 'b', 'a'] | ||
| assert result.type.equals(expected_type) | ||
|
|
||
| # with missing values in categorical | ||
| cat = pd.Categorical([u'a', u'b', None, u'a']) | ||
|
|
||
| result = pa.array(cat) | ||
| assert result.to_pylist() == ['a', 'b', None, 'a'] | ||
| assert result.type.equals(expected_type) | ||
|
|
||
| # with additional mask | ||
| result = pa.array(cat, mask=np.array([False, False, False, True])) | ||
| assert result.to_pylist() == ['a', 'b', None, None] | ||
| assert result.type.equals(expected_type) | ||
|
|
||
|
|
||
| def test_dictionary_from_pandas_specified_type(): | ||
| # ARROW-7168 - ensure specified type is always respected | ||
|
|
||
| # the same as cat = pd.Categorical(['a', 'b']) but explicit about dtypes | ||
| cat = pd.Categorical.from_codes( | ||
| np.array([0, 1], dtype='int8'), np.array(['a', 'b'], dtype=object)) | ||
|
|
||
| # different index type -> allow this | ||
| # (the type of the 'codes' in pandas is not part of the data type) | ||
| typ = pa.dictionary(index_type=pa.int16(), value_type=pa.string()) | ||
| result = pa.array(cat, type=typ) | ||
| assert result.type.equals(typ) | ||
| assert result.to_pylist() == ['a', 'b'] | ||
|
|
||
|
Member
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. Also, should you perhaps check passing a |
||
| # mismatching values type -> raise error (for now a deprecation warning) | ||
| typ = pa.dictionary(index_type=pa.int8(), value_type=pa.int64()) | ||
| with pytest.warns(FutureWarning): | ||
| result = pa.array(cat, type=typ) | ||
| assert result.to_pylist() == ['a', 'b'] | ||
|
|
||
| # mismatching order -> raise error (for now a deprecation warning) | ||
| typ = pa.dictionary( | ||
| index_type=pa.int8(), value_type=pa.string(), ordered=True) | ||
| with pytest.warns(FutureWarning, match="The 'ordered' flag of the passed"): | ||
| result = pa.array(cat, type=typ) | ||
| assert result.to_pylist() == ['a', 'b'] | ||
|
|
||
| # with mask | ||
| typ = pa.dictionary(index_type=pa.int16(), value_type=pa.string()) | ||
| result = pa.array(cat, type=typ, mask=np.array([False, True])) | ||
| assert result.type.equals(typ) | ||
| assert result.to_pylist() == ['a', None] | ||
|
|
||
| # empty categorical -> be flexible in values type to allow | ||
| cat = pd.Categorical([]) | ||
|
|
||
| typ = pa.dictionary(index_type=pa.int8(), value_type=pa.string()) | ||
| result = pa.array(cat, type=typ) | ||
| assert result.type.equals(typ) | ||
| assert result.to_pylist() == [] | ||
| typ = pa.dictionary(index_type=pa.int8(), value_type=pa.int64()) | ||
| result = pa.array(cat, type=typ) | ||
| assert result.type.equals(typ) | ||
| assert result.to_pylist() == [] | ||
|
|
||
| # passing non-dictionary type | ||
| cat = pd.Categorical(['a', 'b']) | ||
| result = pa.array(cat, type=pa.string()) | ||
| expected = pa.array(['a', 'b'], type=pa.string()) | ||
| assert result.equals(expected) | ||
| assert result.to_pylist() == ['a', 'b'] | ||
|
|
||
|
|
||
| # ---------------------------------------------------------------------- | ||
| # Array protocol in pandas conversions tests | ||
|
|
||
|
|
||
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.
Shouldn't you check also the array value? (for example using
result.to_pylist())