-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Merge multi-index with a multi-index #15980
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
Changes from 1 commit
5fec67f
a5af632
dccb815
1971af7
f842257
cc0b296
b66ed30
4296343
06ec16d
a4cdfd2
142f7eb
eae67f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,3 +103,5 @@ doc/source/index.rst | |
doc/build/html/index.html | ||
# Windows specific leftover: | ||
doc/tmp.sv | ||
|
||
Untitled\.ipynb |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1211,11 +1211,7 @@ def test_join_multi_levels2(self): | |
.set_index(["household_id", "asset_id", "t"]) | ||
.reindex(columns=['share', 'log_return'])) | ||
|
||
def f(): | ||
household.join(log_return, how='inner') | ||
self.assertRaises(NotImplementedError, f) | ||
|
||
# this is the equivalency | ||
# this is equivalency the | ||
result = (merge(household.reset_index(), log_return.reset_index(), | ||
on=['asset_id'], how='inner') | ||
.set_index(['household_id', 'asset_id', 't'])) | ||
|
@@ -1224,7 +1220,7 @@ def f(): | |
expected = ( | ||
DataFrame(dict( | ||
household_id=[1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4], | ||
asset_id=["nl0000301109", "nl0000289783", "gb00b03mlx29", | ||
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. why would this change? 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. I think this is a typo in the original test. I am quite sure because the deleted ID (nl0000289783) does not appear in any dataframe of 'test_join_multi_levels2' function. |
||
asset_id=["nl0000301109", "nl0000301109", "gb00b03mlx29", | ||
"gb00b03mlx29", "gb00b03mlx29", | ||
"gb00b03mlx29", "gb00b03mlx29", "gb00b03mlx29", | ||
"lu0197800237", "lu0197800237", | ||
|
@@ -1237,8 +1233,51 @@ def f(): | |
.09604978, -.06524096, .03532373, | ||
.03025441, .036997, None, None] | ||
)) | ||
.set_index(["household_id", "asset_id", "t"])) | ||
.set_index(["household_id", "asset_id", "t"]) | ||
.reindex(columns=['share', 'log_return'])) | ||
|
||
def f(): | ||
household.join(log_return, how='outer') | ||
self.assertRaises(NotImplementedError, f) | ||
result = (merge(household.reset_index(), log_return.reset_index(), | ||
on=['asset_id'], how='outer') | ||
.set_index(['household_id', 'asset_id', 't'])) | ||
|
||
assert_frame_equal(result, expected) | ||
|
||
def test_join_multi_levels3(self): | ||
|
||
matrix = ( | ||
pd.DataFrame( | ||
dict(Origin=[1, 1, 2, 2, 3], | ||
Destination=[1, 2, 1, 3, 1], | ||
Period=['AM','PM','IP','AM','OP'], | ||
TripPurp=['hbw', 'nhb', 'hbo', 'nhb', 'hbw'], | ||
Trips=[1987, 3647, 2470, 4296, 4444]), | ||
columns=['Origin', 'Destination', 'Period', | ||
'TripPurp', 'Trips']) | ||
.set_index(['Origin', 'Destination', 'Period', 'TripPurp'])) | ||
|
||
distances = ( | ||
pd.DataFrame( | ||
dict(Origin= [1, 1, 2, 2, 3, 3, 5], | ||
Destination=[1, 2, 1, 2, 1, 2, 6], | ||
Period=['AM','PM','IP','AM','OP','IP', 'AM'], | ||
LinkType=['a', 'a', 'c', 'b', 'a', 'b', 'a'], | ||
Distance=[100, 80, 90, 80, 75, 35, 55]), | ||
columns=['Origin', 'Destination', 'Period', | ||
'LinkType', 'Distance']) | ||
.set_index(['Origin', 'Destination','Period', 'LinkType'])) | ||
|
||
expected = ( | ||
pd.DataFrame( | ||
dict(Origin= [1, 1, 2, 2, 3], | ||
Destination=[1, 2, 1, 3, 1], | ||
Period=['AM','PM','IP', 'AM', 'OP'], | ||
TripPurp=['hbw', 'nhb', 'hbo', 'nhb', 'hbw'], | ||
Trips=[1987, 3647, 2470, 4296, 4444], | ||
Distance=[100, 80, 90, np.nan, 75]), | ||
columns=['Origin', 'Destination', 'Period', 'TripPurp', | ||
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. so the very first test you should have is a self-join. 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. Do you mean result = matrix.join(matrix, how='inner', rsuffix='_joined') 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. yes 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. Hi @jreback , I've implemented the requested changes but I'm not sure they show because I force-pushed. 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. nothing seems to have been pushed. 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. Nevertheless, the current commit includes all the requested changes. You can review them whenever possible 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. if it was pushed it would show up 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. I think now it must be fine. My changes were pushed in a second commit but now I merged them. Really sorry about the confusion, still getting to know GIT.. |
||
'Trips', 'Distance']) | ||
.set_index(['Origin', 'Destination', 'Period', 'TripPurp'])) | ||
|
||
|
||
result = matrix.join(distances, how='left') | ||
assert_frame_equal(result, expected) |
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.
first thing is that you must have exactly the same levels that match, otherwise this is a raise. They must match in number and name (or None matches None).
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.
The common levels are specified by: overlap = list(set(self_names) & set(other_names)).
I think this way guarantees that they will much in number and name. Am I missing something?