Skip to content

Commit

Permalink
BUG: Concat with axis rows
Browse files Browse the repository at this point in the history
closes pandas-dev#14369

Author: Brandon M. Burroughs <[email protected]>

Closes pandas-dev#14416 from brandonmburroughs/concat_with_axis_rows and squashes the following commits:

9aa260d [Brandon M. Burroughs] Changing axis handling to depend on object passed
49442be [Brandon M. Burroughs] Using dataframe _get_axis_number instance method
64702fb [Brandon M. Burroughs] Updating documentation for concat
fdd5260 [Brandon M. Burroughs] Removing duplicate expected dfs
3f08b07 [Brandon M. Burroughs] Adding concat tests for axis 0, 1, and 'index'
cf3f998 [Brandon M. Burroughs] Adding ValueError test for concat Series axis 'columns'
a6694b9 [Brandon M. Burroughs] Updating documentation
584ebd2 [Brandon M. Burroughs] BUG: Allow concat to take string axis names
  • Loading branch information
brandonmburroughs authored and Piotr Chromiec committed Oct 21, 2016
1 parent a970dd3 commit 5454a5b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.19.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Bug Fixes


- Bug in ``pd.concat`` where names of the ``keys`` were not propagated to the resulting ``MultiIndex`` (:issue:`14252`)
- Bug in ``pd.concat`` where ``axis`` cannot take string parameters ``'rows'`` or ``'columns'`` (:issue:`14369`)
- Bug in ``MultiIndex.set_levels`` where illegal level values were still set after raising an error (:issue:`13754`)
- Bug in ``DataFrame.to_json`` where ``lines=True`` and a value contained a ``}`` character (:issue:`14391`)
- Bug in ``df.groupby`` causing an ``AttributeError`` when grouping a single index frame by a column and the index level (:issue`14327`)
59 changes: 59 additions & 0 deletions pandas/tests/frame/test_combine_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,65 @@ 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))

# Index/row/0 DataFrame
expected_index = pd.DataFrame(
{'A': [0.1, 0.2, 0.3, 0.4]}, index=[0, 1, 0, 1])

concatted_index = pd.concat([df1, df2], axis='index')
assert_frame_equal(concatted_index, expected_index)

concatted_row = pd.concat([df1, df2], axis='rows')
assert_frame_equal(concatted_row, expected_index)

concatted_0 = pd.concat([df1, df2], axis=0)
assert_frame_equal(concatted_0, expected_index)

# Columns/1 DataFrame
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)

concatted_1 = pd.concat([df1, df2], axis=1)
assert_frame_equal(concatted_1, expected_columns)

series1 = pd.Series([0.1, 0.2])
series2 = pd.Series([0.3, 0.4])

# Index/row/0 Series
expected_index_series = pd.Series(
[0.1, 0.2, 0.3, 0.4], index=[0, 1, 0, 1])

concatted_index_series = pd.concat([series1, series2], axis='index')
assert_series_equal(concatted_index_series, expected_index_series)

concatted_row_series = pd.concat([series1, series2], axis='rows')
assert_series_equal(concatted_row_series, expected_index_series)

concatted_0_series = pd.concat([series1, series2], axis=0)
assert_series_equal(concatted_0_series, expected_index_series)

# Columns/1 Series
expected_columns_series = pd.DataFrame(
[[0.1, 0.3], [0.2, 0.4]], index=[0, 1], columns=[0, 1])

concatted_columns_series = pd.concat(
[series1, series2], axis='columns')
assert_frame_equal(concatted_columns_series, expected_columns_series)

concatted_1_series = pd.concat([series1, series2], axis=1)
assert_frame_equal(concatted_1_series, expected_columns_series)

# Testing ValueError
with assertRaisesRegexp(ValueError, 'No axis named'):
pd.concat([series1, series2], axis='something')


class TestDataFrameCombineFirst(tm.TestCase, TestData):

Expand Down
8 changes: 7 additions & 1 deletion pandas/tools/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/'index', 1/'columns'}, default 0
The axis to concatenate along
join : {'inner', 'outer'}, default 'outer'
How to handle indexes on other axis(es)
Expand Down Expand Up @@ -1411,6 +1411,12 @@ def __init__(self, objs, axis=0, join='outer', join_axes=None,
sample = objs[0]
self.objs = objs

# Standardize axis parameter to int
if isinstance(sample, Series):
axis = DataFrame()._get_axis_number(axis)
else:
axis = sample._get_axis_number(axis)

# Need to flip BlockManager axis in the DataFrame special case
self._is_frame = isinstance(sample, DataFrame)
if self._is_frame:
Expand Down

0 comments on commit 5454a5b

Please sign in to comment.