Skip to content

BUG: align with broadcast_axis, #13194 #13198

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

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.18.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,4 @@ Bug Fixes
- Bug in ``NaT`` - ``Period`` raises ``AttributeError`` (:issue:`13071`)
- Bug in ``Period`` addition raises ``TypeError`` if ``Period`` is on right hand side (:issue:`13069`)
- Bug in ``pd.set_eng_float_format()`` that would prevent NaN's from formatting (:issue:`11981`)
- Fixed the bug in ``DataFrame.align()`` which was giving wrong output when supplied with broadcast_axis. (:issue:`13194`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug in

use double-backticks around broadcast_axis.

say incorrect output (not wrong output)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

say that this was not taking into account the how parameter when broadcast_axis was passed

11 changes: 7 additions & 4 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4147,13 +4147,17 @@ def align(self, other, join='outer', axis=None, level=None, copy=True,
from pandas import DataFrame, Series
method = missing.clean_fill_method(method)

if axis is not None:
axis = self._get_axis_number(axis)

if broadcast_axis == 1 and self.ndim != other.ndim:
if isinstance(self, Series):
# this means other is a DataFrame, and we need to broadcast
# self
cons = self._constructor_expanddim
df = cons(dict((c, self) for c in other.columns),
**other._construct_axes_dict())
**self._construct_axes_dict(
**other._construct_axes_dict(axes=['columns'])))
return df._align_frame(other, join=join, axis=axis,
level=level, copy=copy,
fill_value=fill_value, method=method,
Expand All @@ -4163,14 +4167,13 @@ def align(self, other, join='outer', axis=None, level=None, copy=True,
# other
cons = other._constructor_expanddim
df = cons(dict((c, other) for c in self.columns),
**self._construct_axes_dict())
**other._construct_axes_dict(
**self._construct_axes_dict(axes=['columns'])))
return self._align_frame(df, join=join, axis=axis, level=level,
copy=copy, fill_value=fill_value,
method=method, limit=limit,
fill_axis=fill_axis)

if axis is not None:
axis = self._get_axis_number(axis)
if isinstance(other, DataFrame):
return self._align_frame(other, join=join, axis=axis, level=level,
copy=copy, fill_value=fill_value,
Expand Down
44 changes: 44 additions & 0 deletions pandas/tests/frame/test_axis_select_reindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -880,3 +880,47 @@ def test_reindex_multi(self):
expected = df.reindex([0, 1]).reindex(columns=['a', 'b'])

assert_frame_equal(result, expected)

def test_align_broadcast_axis(self):
# GH 13194
# First four tests for DataFrame.align(Index)
# For 'right' join
df = DataFrame(np.array([[1., 2.], [3., 4.]]), columns=list('AB'))
ts = Series([5., 6., 7.])

result = df.align(ts, join='right', axis=0, broadcast_axis=1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try one of these with broadcast_axis='columns' and one with broadcast_axis='index' as well

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left, right = df.align(....) and same below

expected1 = DataFrame(np.array([[1., 2.], [3., 4.],
Copy link
Contributor

@jreback jreback May 20, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

call these expected_left and expected_right (and same below)

[pd.np.nan, pd.np.nan]]),
columns=list('AB'))
expected2 = DataFrame(np.array([[5., 5.], [6., 6.], [7., 7.]]),
columns=list('AB'))
assert_frame_equal(result[0], expected1)
assert_frame_equal(result[1], expected2)

# For 'right' join on different index
result = df.align(ts, join='right', axis=1, broadcast_axis=1)
expected1 = DataFrame(np.array([[1., 2.], [3., 4.]]),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need to pass these as numpy arrays, just list-of-lists is fine

columns=list('AB'))
expected2 = DataFrame(np.array([[5., 5.], [6., 6.],
[7., 7.]]),
columns=list('AB'))
assert_frame_equal(result[0], expected1)
assert_frame_equal(result[1], expected2)

# For 'left' join
result = df.align(ts, join='left', axis=0, broadcast_axis=1)
expected1 = DataFrame(np.array([[1., 2.], [3., 4.]]),
columns=list('AB'))
expected2 = DataFrame(np.array([[5., 5.], [6., 6.]]),
columns=list('AB'))
assert_frame_equal(result[0], expected1)
assert_frame_equal(result[1], expected2)

# For 'left' join on different axis
result = df.align(ts, join='left', axis=1, broadcast_axis=1)
expected1 = DataFrame(np.array([[1., 2.], [3., 4.]]),
columns=list('AB'))
expected2 = DataFrame(np.array([[5., 5.], [6., 6.], [7., 7.]]),
columns=list('AB'))
assert_frame_equal(result[0], expected1)
assert_frame_equal(result[1], expected2)
23 changes: 23 additions & 0 deletions pandas/tests/series/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1838,3 +1838,26 @@ def test_multilevel_preserve_name(self):
result2 = s.ix['foo']
self.assertEqual(result.name, s.name)
self.assertEqual(result2.name, s.name)

def test_align_broadcast_axis_series(self):
# GH 13194
# Series.align(DataFrame) tests, 'outer' join
df = DataFrame(np.array([[1., 2.], [3., 4.]]), columns=list('AB'))
ts = Series([5., 6., 7.])
result = ts.align(df, join='outer', axis=0, broadcast_axis=1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same name scheme here

expected1 = DataFrame(np.array([[5., 5.], [6., 6.], [7., 7.]]),
columns=list('AB'))
expected2 = DataFrame(np.array([[1., 2.], [3., 4.],
[pd.np.nan, pd.np.nan]]),
columns=list('AB'))
self.assertTrue(result[0].equals(expected1))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tm.assert_frame_equal

self.assertTrue(result[1].equals(expected2))

# Series.align(DataFrame) tests, 'inner' join
result = ts.align(df, join='inner', axis=0, broadcast_axis=1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can make all tests more comprehensive as "inner" and "outer" outputs the same, and "left" and "right" outputs inversed results.

result = df.align(series, ...)
self.assert_frame_equal(result[0], expected1)
self.assert_frame_equal(result[1], expected2)
result = series.align(df, ...)
self.assert_frame_equal(result[0], expected2)
self.assert_frame_equal(result[1], expected1)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missed @jreback suggestion for variable names:

left, right = df.align(series, ...)
self.assert_frame_equal(left, expected_left)
self.assert_frame_equal(right, expected_right)
left, right = series.align(df, ...)
self.assert_frame_equal(right, expected_left)
self.assert_frame_equal(left, expected_right)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jreback Can you please look into the issue I have reported above.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a single Ping is enough then you need to wait for comments

expected1 = DataFrame(np.array([[5., 5.], [6., 6.]]),
columns=list('AB'))
expected2 = DataFrame(np.array([[1., 2.], [3., 4.]]),
columns=list('AB'))
self.assertTrue(result[0].equals(expected1))
self.assertTrue(result[1].equals(expected2))
1 change: 1 addition & 0 deletions pandas/tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1874,6 +1874,7 @@ def test_pipe_panel(self):
with tm.assertRaises(ValueError):
result = wp.pipe((f, 'y'), x=1, y=1)


if __name__ == '__main__':
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
exit=False)