-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
[WIP] Quick fix to provide complex data type support for hashmap based algorithms #27599
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
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0fbd5d9
Cast complex data types to object type when hashing is needed. Coerce…
sk-tests e3933ef
Merge remote-tracking branch 'upstream/master' into complex-value-counts
sk-tests 665e7a6
This commit fixes #17927
sk-tests a1421fe
This commit fixes #17927
sk-tests 7a16823
Use pandas testing utilities for testing numpy array equality. Comple…
sk-tests 0bf66a9
specify 64 bit integer data type in the complex groupby test
sk-tests 85edadb
Simple test cases for mode
sk-tests 0a1e59a
change bit specific integer types to platform dependent integers in t…
sk-tests 13afa7e
Merge remote-tracking branch 'upstream/master' into quick-complex-sup…
sk-tests a8fba28
update tests for basic complex number support. Use assert_produces_wa…
sk-tests 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 |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
import pandas as pd | ||
from pandas import DataFrame, Index, Series | ||
import pandas.core.algorithms as algos | ||
import pandas.util.testing as tm | ||
|
||
|
||
class TestBasicComplexSupport: | ||
@pytest.mark.parametrize( | ||
"array,expected", | ||
[ | ||
( | ||
[1 + 1j, 0, 1, 1j, 1 + 2j], | ||
Series([1, 1, 1, 1, 1], index=[1 + 2j, 1 + 1j, 1j, 1, 0]), | ||
), | ||
( | ||
[1 + 2j, 0, 1j, 1, 1j, 1 + 1j], | ||
# index is sorted by value counts in descending order by default | ||
Series([2, 1, 1, 1, 1], index=[1j, 1 + 2j, 1 + 1j, 1, 0]), | ||
), | ||
], | ||
) | ||
def test_value_counts(self, array, expected): | ||
result = algos.value_counts(array) | ||
tm.assert_series_equal(result, expected) | ||
|
||
@pytest.mark.parametrize( | ||
"array,expected", | ||
[ | ||
( | ||
[1 + 1j, 0, 1, 1j, 1 + 2j, 1 + 2j], | ||
np.array([(1 + 1j), 0j, (1 + 0j), 1j, (1 + 2j)], dtype=object), | ||
) | ||
], | ||
) | ||
def test_unique(self, array, expected): | ||
result = algos.unique(array) | ||
tm.assert_numpy_array_equal(result, expected) | ||
|
||
@pytest.mark.parametrize( | ||
"array,expected", | ||
[ | ||
( | ||
[0, 1j, 1j, 1, 1 + 1j, 1 + 2j, 1 + 1j], | ||
Series([False, False, True, False, False, False, True], dtype=bool), | ||
) | ||
], | ||
) | ||
def test_duplicated(self, array, expected): | ||
result = Series(array).duplicated() | ||
tm.assert_series_equal(result, expected) | ||
|
||
@pytest.mark.parametrize( | ||
"array,expected", | ||
[ | ||
( | ||
[0, 1j, 1j, 1, 1 + 1j, 1 + 2j, 1 + 1j], | ||
Series([False, True, True, False, True, True, True], dtype=bool), | ||
) | ||
], | ||
) | ||
def test_isin(self, array, expected): | ||
result = Series(array).isin([1j, 1 + 1j, 1 + 2j]) | ||
tm.assert_series_equal(result, expected) | ||
|
||
def test_factorize(self): | ||
array = [1, 2, 2 + 1j] | ||
labels, uniques = pd.factorize(array) | ||
expected = np.array([0, 1, 2], dtype=np.intp) | ||
tm.assert_numpy_array_equal(labels, expected) | ||
expected = np.array([(1 + 0j), (2 + 0j), (2 + 1j)], dtype=object) | ||
tm.assert_numpy_array_equal(uniques, expected) | ||
|
||
@pytest.mark.parametrize( | ||
"frame,expected", | ||
[ | ||
( | ||
DataFrame([dict(a=1, b=1 + 1j), dict(a=1, b=1 + 2j)]), | ||
DataFrame( | ||
np.array([1, 1], dtype=np.int64), | ||
index=Index([(1 + 1j), (1 + 2j)], dtype="object", name="b"), | ||
columns=Index(["a"], dtype="object"), | ||
), | ||
) | ||
], | ||
) | ||
def test_groupby(self, frame, expected): | ||
result = frame.groupby("b", sort=False).count() | ||
tm.assert_frame_equal(result, expected) | ||
|
||
# sorting of the index should fail since complex numbers are unordered | ||
with pytest.raises(TypeError): | ||
frame.groupby("b", sort=True).count() | ||
|
||
@pytest.mark.parametrize( | ||
"array,expected", | ||
[ | ||
([0, 1j, 1, 1, 1 + 1j, 1 + 2j], Series([1], dtype=np.complex128)), | ||
([1 + 1j, 2j, 1 + 1j], Series([1 + 1j], dtype=np.complex128)), | ||
], | ||
) | ||
def test_unimode(self, array, expected): | ||
result = Series(array).mode() | ||
tm.assert_series_equal(result, expected) | ||
|
||
# mode tries to sort multimodal series. | ||
# A warning will be raised since complex numbers | ||
# are not ordered. | ||
@pytest.mark.parametrize( | ||
"array,expected", | ||
[ | ||
( | ||
# no modes | ||
[0, 1j, 1, 1 + 1j, 1 + 2j], | ||
Series([0, 1, 1j, 1 + 1j, 1 + 2j], dtype=np.complex128), | ||
), | ||
([1 + 1j, 2j, 1 + 1j, 2j, 3], Series([1 + 1j, 2j], dtype=np.complex128)), | ||
], | ||
) | ||
def test_multimode(self, array, expected): | ||
with tm.assert_produces_warning(UserWarning): | ||
result = Series(array).mode() | ||
tm.assert_series_equal(result, expected) |
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.
better to do the object-coersion here than fallback to the except branch. clearer