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 4 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. (: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.

be more descriptive of what you fixed here. remember a user is reading this.

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
46 changes: 46 additions & 0 deletions pandas/tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1874,6 +1874,52 @@ def test_pipe_panel(self):
with tm.assertRaises(ValueError):
result = wp.pipe((f, 'y'), x=1, y=1)

def test_align_joins(self):
df = DataFrame(np.array([[1., 2.], [3., 4.]]), columns=list('AB'))
Copy link
Contributor

Choose a reason for hiding this comment

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

add the issue number as a comment here

rename to test_align_broadcast_axis

ts = Series([5., 6., 7.])
check = df.align(ts, join='outer', axis=0, broadcast_axis=1)
df1 = DataFrame(check[0])
Copy link
Contributor

Choose a reason for hiding this comment

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

all of these tests need to go in tests/frame/test_axis_select_reinde and/or tests/series/test_indexing. right after the other align tests.

Copy link
Contributor

Choose a reason for hiding this comment

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

model them after the existing tests.

df2 = DataFrame(check[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(df1, expected1)
assert_frame_equal(df2, expected2)

check = df.align(ts, join='inner', axis=0, broadcast_axis=1)
df1 = DataFrame(check[0])
df2 = DataFrame(check[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(df1, expected1)
assert_frame_equal(df2, expected2)

check = df.align(ts, join='left', axis=0, broadcast_axis=1)
df1 = DataFrame(check[0])
df2 = DataFrame(check[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(df1, expected1)
assert_frame_equal(df2, expected2)

check = df.align(ts, join='right', axis=0, broadcast_axis=1)
df1 = DataFrame(check[0])
df2 = DataFrame(check[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(df1, expected1)
assert_frame_equal(df2, expected2)


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