-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Allow concat to take string axis names #14389
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
584ebd2
a6694b9
cf3f998
3f08b07
fdd5260
64702fb
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 |
---|---|---|
|
@@ -347,6 +347,20 @@ def test_concat_named_keys(self): | |
names=[None, None])) | ||
assert_frame_equal(concatted_unnamed, expected_unnamed) | ||
|
||
def test_concat_axis_parameter(self): | ||
# GH 14369 | ||
df1 = pd.DataFrame({'A': [0.1, 0.2]}, index=range(2)) | ||
df2 = pd.DataFrame({'A': [0.3, 0.4]}, index=range(2)) | ||
expected_row = pd.DataFrame( | ||
{'A': [0.1, 0.2, 0.3, 0.4]}, index=[0, 1, 0, 1]) | ||
concatted_row = pd.concat([df1, df2], axis='rows') | ||
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. Probably already tested elsewhere, but can you add for completeness in this test also the 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. What does this mean? I looked through the 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. Whoops, sorry, I meant 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. Okay, that makes sense! Tests added. |
||
assert_frame_equal(concatted_row, expected_row) | ||
|
||
expected_columns = pd.DataFrame( | ||
[[0.1, 0.3], [0.2, 0.4]], index=[0, 1], columns=['A', 'A']) | ||
concatted_columns = pd.concat([df1, df2], axis='columns') | ||
assert_frame_equal(concatted_columns, expected_columns) | ||
|
||
|
||
class TestDataFrameCombineFirst(tm.TestCase, TestData): | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1283,7 +1283,7 @@ def concat(objs, axis=0, join='outer', join_axes=None, ignore_index=False, | |
argument, unless it is passed, in which case the values will be | ||
selected (see below). Any None objects will be dropped silently unless | ||
they are all None in which case a ValueError will be raised | ||
axis : {0, 1, ...}, default 0 | ||
axis : {0, 1, 'rows', 'columns', ...}, default 0 | ||
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. Can you make this something like 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, that makes sense. Done. |
||
The axis to concatenate along | ||
join : {'inner', 'outer'}, default 'outer' | ||
How to handle indexes on other axis(es) | ||
|
@@ -1411,6 +1411,10 @@ def __init__(self, objs, axis=0, join='outer', join_axes=None, | |
sample = objs[0] | ||
self.objs = objs | ||
|
||
# Check for string axis parameter | ||
if isinstance(axis, str): | ||
axis = objs[0]._get_axis_number(axis) | ||
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 will not work in case of concatting different Serieses with Can you also add a test for such case? 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. @jreback Does there already exist a utility function similar to 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've added the test. Thanks for pointing that out! 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. What I wanted to say is that the code will not work in that case, but I think it should work (which means that we cannot use 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. iirc it's a class method so something like NDFrame._get_axis_number(axis) should work 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. That would have been indeed have been useful, but it is not a class method .. (the attributes like 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. Yeah, I've added a function in Does this work? It allows |
||
|
||
# Need to flip BlockManager axis in the DataFrame special case | ||
self._is_frame = isinstance(sample, DataFrame) | ||
if self._is_frame: | ||
|
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.
missing closing backticks after "columns"
Small other thing: I would also quote the parameters inside the backticks (like
'rows'
) to make clear it are string argumentsThere 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.
Good idea. Added!