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 6 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 the ``join`` argument. Earlier, upon supplying value of join argument as any of the four('outer', 'inner', 'left', 'right' ), align() was giving erraneous output. Align with broadcast_axis specified was using 'inner' join consistently irrespective of the value of 'join' provided when aligning dataframe and series on other axis. The problem was identified to be in pandas/core/generic.py and has been subsequently fixed. (: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.

Single line of comments pls. these are read by the users and don't need this kind of explanation.

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
50 changes: 50 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,53 @@ 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
# For 'outer' join
df = DataFrame(np.array([[1., 2.], [3., 4.]]), columns=list('AB'))
ts = Series([5., 6., 7.])
result = df.align(ts, join='outer', axis=0, broadcast_axis=1)
result1 = DataFrame(result[0])
result2 = DataFrame(result[1])
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(result1, expected1)
assert_frame_equal(result2, expected2)

# For 'inner' join
result = df.align(ts, join='inner', axis=0, broadcast_axis=1)
result1 = DataFrame(result[0])
Copy link
Member

@sinhrks sinhrks May 19, 2016

Choose a reason for hiding this comment

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

Not needed. Test returned obj (result[0] and [1]) as it is.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sinhrks I'm not sure that I understand. Can you please elaborate?

Copy link
Member

Choose a reason for hiding this comment

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

Pls remove DataFrame(result..) like below.

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

result2 = DataFrame(result[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.]]),
columns=list('AB'))
assert_frame_equal(result1, expected1)
assert_frame_equal(result2, expected2)

# For 'left' join
result = df.align(ts, join='left', axis=0, broadcast_axis=1)
result1 = DataFrame(result[0])
result2 = DataFrame(result[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(result1, expected1)
assert_frame_equal(result2, expected2)

# For 'right' join
result = df.align(ts, join='right', axis=0, broadcast_axis=1)
result1 = DataFrame(result[0])
result2 = DataFrame(result[1])
expected1 = DataFrame(np.array([[1., 2.], [3., 4.],
[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(result1, expected1)
assert_frame_equal(result2, 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)