Skip to content

Commit

Permalink
Using dataframe _get_axis_number instance method
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonmburroughs committed Oct 13, 2016
1 parent 64702fb commit 49442be
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
33 changes: 28 additions & 5 deletions pandas/tests/frame/test_combine_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,10 @@ def test_concat_axis_parameter(self):
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)

Expand All @@ -363,8 +365,10 @@ def test_concat_axis_parameter(self):
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)

Expand All @@ -374,14 +378,33 @@ def test_concat_axis_parameter(self):
series1 = pd.Series([0.1, 0.2])
series2 = pd.Series([0.3, 0.4])

expected_row_series = pd.Series(
# 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_row_series)
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)

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


class TestDataFrameCombineFirst(tm.TestCase, TestData):
Expand Down
5 changes: 2 additions & 3 deletions pandas/tools/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -1411,9 +1411,8 @@ 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)
# Standardize axis parameter to int
axis = DataFrame()._get_axis_number(axis)

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

0 comments on commit 49442be

Please sign in to comment.