-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
TST: refactored test_factorize #32311
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
Merged
WillAyd
merged 6 commits into
pandas-dev:master
from
SaturnFromTitan:fixturize-test_factorize
Mar 7, 2020
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e419141
refactored test_factorize
SaturnFromTitan cdca771
fixing ci
SaturnFromTitan 1b4825b
switched to using intp instead of workaround to make CI happy
SaturnFromTitan 9c22a50
review comments
SaturnFromTitan d26d0fa
Merge branch 'master' into fixturize-test_factorize
SaturnFromTitan 76d73cc
Merge branch 'master' into fixturize-test_factorize
SaturnFromTitan 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 |
---|---|---|
|
@@ -557,66 +557,28 @@ def test_value_counts_datetime64(self, index_or_series): | |
result2 = td2.value_counts() | ||
tm.assert_series_equal(result2, expected_s) | ||
|
||
def test_factorize(self): | ||
for orig in self.objs: | ||
o = orig.copy() | ||
|
||
if isinstance(o, Index) and o.is_boolean(): | ||
exp_arr = np.array([0, 1] + [0] * 8, dtype=np.intp) | ||
exp_uniques = o | ||
exp_uniques = Index([False, True]) | ||
else: | ||
exp_arr = np.array(range(len(o)), dtype=np.intp) | ||
exp_uniques = o | ||
codes, uniques = o.factorize() | ||
|
||
tm.assert_numpy_array_equal(codes, exp_arr) | ||
if isinstance(o, Series): | ||
tm.assert_index_equal(uniques, Index(orig), check_names=False) | ||
else: | ||
# factorize explicitly resets name | ||
tm.assert_index_equal(uniques, exp_uniques, check_names=False) | ||
|
||
def test_factorize_repeated(self): | ||
for orig in self.objs: | ||
o = orig.copy() | ||
@pytest.mark.parametrize("sort", [True, False]) | ||
def test_factorize(self, index_or_series_obj, sort): | ||
obj = index_or_series_obj | ||
result_codes, result_uniques = obj.factorize(sort=sort) | ||
|
||
# don't test boolean | ||
if isinstance(o, Index) and o.is_boolean(): | ||
continue | ||
constructor = pd.Index | ||
if isinstance(obj, pd.MultiIndex): | ||
constructor = pd.MultiIndex.from_tuples | ||
expected_uniques = constructor(obj.unique()) | ||
|
||
# sort by value, and create duplicates | ||
if isinstance(o, Series): | ||
o = o.sort_values() | ||
n = o.iloc[5:].append(o) | ||
else: | ||
indexer = o.argsort() | ||
o = o.take(indexer) | ||
n = o[5:].append(o) | ||
|
||
exp_arr = np.array( | ||
[5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=np.intp | ||
) | ||
codes, uniques = n.factorize(sort=True) | ||
if sort: | ||
expected_uniques = expected_uniques.sort_values() | ||
|
||
tm.assert_numpy_array_equal(codes, exp_arr) | ||
if isinstance(o, Series): | ||
tm.assert_index_equal( | ||
uniques, Index(orig).sort_values(), check_names=False | ||
) | ||
else: | ||
tm.assert_index_equal(uniques, o, check_names=False) | ||
expected_uniques_list = list(expected_uniques) | ||
expected_codes = [expected_uniques_list.index(val) for val in obj] | ||
|
||
exp_arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4], np.intp) | ||
codes, uniques = n.factorize(sort=False) | ||
tm.assert_numpy_array_equal(codes, exp_arr) | ||
# CI: on linux 32bit the dtype is int32, otherwise int64 | ||
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. This seems like a bug itself; is there an open issue for it? If not can you open one? 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. Could be related to #31856 |
||
assert result_codes.dtype in [np.int32, np.int64] | ||
SaturnFromTitan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
expected_codes = np.asarray(expected_codes, dtype=result_codes.dtype) | ||
|
||
if isinstance(o, Series): | ||
expected = Index(o.iloc[5:10].append(o.iloc[:5])) | ||
tm.assert_index_equal(uniques, expected, check_names=False) | ||
else: | ||
expected = o[5:10].append(o[:5]) | ||
tm.assert_index_equal(uniques, expected, check_names=False) | ||
tm.assert_numpy_array_equal(result_codes, expected_codes) | ||
tm.assert_index_equal(result_uniques, expected_uniques) | ||
|
||
def test_duplicated_drop_duplicates_index(self): | ||
# GH 4060 | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.