-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
PERF: assert_frame_equal and assert_series_equal for frames/series with a MultiIndex #55949
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
mroeschke
merged 3 commits into
pandas-dev:main
from
lukemanley:perf-assert-frame-equal
Nov 14, 2023
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -283,22 +283,37 @@ def _get_ilevel_values(index, level): | |
right = cast(MultiIndex, right) | ||
|
||
for level in range(left.nlevels): | ||
# cannot use get_level_values here because it can change dtype | ||
llevel = _get_ilevel_values(left, level) | ||
rlevel = _get_ilevel_values(right, level) | ||
|
||
lobj = f"MultiIndex level [{level}]" | ||
assert_index_equal( | ||
llevel, | ||
rlevel, | ||
exact=exact, | ||
check_names=check_names, | ||
check_exact=check_exact, | ||
check_categorical=check_categorical, | ||
rtol=rtol, | ||
atol=atol, | ||
obj=lobj, | ||
) | ||
try: | ||
# try comparison on levels/codes to avoid densifying MultiIndex | ||
assert_index_equal( | ||
left.levels[level], | ||
right.levels[level], | ||
exact=exact, | ||
check_names=check_names, | ||
check_exact=check_exact, | ||
check_categorical=check_categorical, | ||
rtol=rtol, | ||
atol=atol, | ||
obj=lobj, | ||
) | ||
assert_numpy_array_equal(left.codes[level], right.codes[level]) | ||
except AssertionError: | ||
# cannot use get_level_values here because it can change dtype | ||
llevel = _get_ilevel_values(left, level) | ||
rlevel = _get_ilevel_values(right, level) | ||
|
||
assert_index_equal( | ||
llevel, | ||
rlevel, | ||
exact=exact, | ||
check_names=check_names, | ||
check_exact=check_exact, | ||
check_categorical=check_categorical, | ||
rtol=rtol, | ||
atol=atol, | ||
obj=lobj, | ||
) | ||
# get_level_values may change dtype | ||
_check_types(left.levels[level], right.levels[level], obj=obj) | ||
|
||
|
@@ -576,6 +591,9 @@ def raise_assert_detail( | |
|
||
{message}""" | ||
|
||
if isinstance(index_values, Index): | ||
index_values = np.array(index_values) | ||
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. array->asarray can avoid a copy 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. opened #55971 to update |
||
|
||
if isinstance(index_values, np.ndarray): | ||
msg += f"\n[index]: {pprint_thing(index_values)}" | ||
|
||
|
@@ -630,7 +648,7 @@ def assert_numpy_array_equal( | |
obj : str, default 'numpy array' | ||
Specify object name being compared, internally used to show appropriate | ||
assertion message. | ||
index_values : numpy.ndarray, default None | ||
index_values : Index | numpy.ndarray, default None | ||
optional index (shared by both left and right), used in output. | ||
""" | ||
__tracebackhide__ = True | ||
|
@@ -701,7 +719,7 @@ def assert_extension_array_equal( | |
The two arrays to compare. | ||
check_dtype : bool, default True | ||
Whether to check if the ExtensionArray dtypes are identical. | ||
index_values : numpy.ndarray, default None | ||
index_values : Index | numpy.ndarray, default None | ||
Optional index (shared by both left and right), used in output. | ||
check_exact : bool, default False | ||
Whether to compare number exactly. | ||
|
@@ -932,7 +950,7 @@ def assert_series_equal( | |
left_values, | ||
right_values, | ||
check_dtype=check_dtype, | ||
index_values=np.asarray(left.index), | ||
index_values=left.index, | ||
obj=str(obj), | ||
) | ||
else: | ||
|
@@ -941,7 +959,7 @@ def assert_series_equal( | |
right_values, | ||
check_dtype=check_dtype, | ||
obj=str(obj), | ||
index_values=np.asarray(left.index), | ||
index_values=left.index, | ||
) | ||
elif check_datetimelike_compat and ( | ||
needs_i8_conversion(left.dtype) or needs_i8_conversion(right.dtype) | ||
|
@@ -972,7 +990,7 @@ def assert_series_equal( | |
atol=atol, | ||
check_dtype=bool(check_dtype), | ||
obj=str(obj), | ||
index_values=np.asarray(left.index), | ||
index_values=left.index, | ||
) | ||
elif isinstance(left.dtype, ExtensionDtype) and isinstance( | ||
right.dtype, ExtensionDtype | ||
|
@@ -983,7 +1001,7 @@ def assert_series_equal( | |
rtol=rtol, | ||
atol=atol, | ||
check_dtype=check_dtype, | ||
index_values=np.asarray(left.index), | ||
index_values=left.index, | ||
obj=str(obj), | ||
) | ||
elif is_extension_array_dtype_and_needs_i8_conversion( | ||
|
@@ -993,7 +1011,7 @@ def assert_series_equal( | |
left._values, | ||
right._values, | ||
check_dtype=check_dtype, | ||
index_values=np.asarray(left.index), | ||
index_values=left.index, | ||
obj=str(obj), | ||
) | ||
elif needs_i8_conversion(left.dtype) and needs_i8_conversion(right.dtype): | ||
|
@@ -1002,7 +1020,7 @@ def assert_series_equal( | |
left._values, | ||
right._values, | ||
check_dtype=check_dtype, | ||
index_values=np.asarray(left.index), | ||
index_values=left.index, | ||
obj=str(obj), | ||
) | ||
else: | ||
|
@@ -1013,7 +1031,7 @@ def assert_series_equal( | |
atol=atol, | ||
check_dtype=bool(check_dtype), | ||
obj=str(obj), | ||
index_values=np.asarray(left.index), | ||
index_values=left.index, | ||
) | ||
|
||
# metadata comparison | ||
|
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
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.
how does it change dtype?
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.
thats an old comment and I'm not sure its valid anymore. opened #55971 to remove