Skip to content

Commit 426a058

Browse files
authored
fix #338 : implemented option 'exact' in align()
1 parent 95e138f commit 426a058

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

doc/source/changes/version_0_30.rst.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ Miscellaneous improvements
135135
people['Bruce Wayne', 'Bruce Willis', 'Arthur Dent']
136136

137137

138+
* added option ``exact`` to ``join`` argument of :py:obj:`Axis.align()` and :py:obj:`LArray.align()` methods.
139+
Instead of aligning, passing ``join='exact'`` to the ``align`` method will raise an error when axes are not equal.
140+
Closes :issue:`338`.
141+
142+
138143
Fixes
139144
-----
140145

larray/core/array.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,13 +1481,14 @@ def align(self, other, join='outer', fill_value=nan, axes=None):
14811481
Parameters
14821482
----------
14831483
other : LArray-like
1484-
join : {'outer', 'inner', 'left', 'right'}, optional
1484+
join : {'outer', 'inner', 'left', 'right', 'exact'}, optional
14851485
Join method. For each axis common to both arrays:
14861486
- outer: will use a label if it is in either arrays axis (ordered like the first array).
14871487
This is the default as it results in no information loss.
1488-
- inner: will use a label if it is in both arrays axis (ordered like the first array)
1489-
- left: will use the first array axis labels
1488+
- inner: will use a label if it is in both arrays axis (ordered like the first array).
1489+
- left: will use the first array axis labels.
14901490
- right: will use the other array axis labels.
1491+
- exact: instead of aligning, raise an error when axes to be aligned are not equal.
14911492
fill_value : scalar or LArray, optional
14921493
Value used to fill cells corresponding to label combinations which are not common to both arrays.
14931494
Defaults to NaN.
@@ -1638,12 +1639,23 @@ def align(self, other, join='outer', fill_value=nan, axes=None):
16381639
a0 0.0 -1.0 nan
16391640
a1 -2.0 -3.0 nan
16401641
a2 -4.0 -5.0 nan
1642+
1643+
Test if two arrays are aligned
1644+
1645+
>>> arr1.align(arr2, join='exact') # doctest: +NORMALIZE_WHITESPACE
1646+
Traceback (most recent call last):
1647+
...
1648+
ValueError: Both arrays are not aligned because align method with join='exact'
1649+
expected Axis(['a0', 'a1'], 'a') to be equal to Axis(['a0', 'a1', 'a2'], 'a')
16411650
"""
16421651
other = aslarray(other)
16431652
# reindex does not currently support anonymous axes
16441653
if any(name is None for name in self.axes.names) or any(name is None for name in other.axes.names):
16451654
raise ValueError("arrays with anonymous axes are currently not supported by LArray.align")
1646-
left_axes, right_axes = self.axes.align(other.axes, join=join, axes=axes)
1655+
try:
1656+
left_axes, right_axes = self.axes.align(other.axes, join=join, axes=axes)
1657+
except ValueError as e:
1658+
raise ValueError("Both arrays are not aligned because {}".format(e))
16471659
return self.reindex(left_axes, fill_value=fill_value), other.reindex(right_axes, fill_value=fill_value)
16481660

16491661
@deprecate_kwarg('reverse', 'ascending', {True: False, False: True})

larray/core/axis.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,7 +1201,7 @@ def align(self, other, join='outer'):
12011201
Parameters
12021202
----------
12031203
other : Axis or label sequence
1204-
join : {'outer', 'inner', 'left', 'right'}, optional
1204+
join : {'outer', 'inner', 'left', 'right', 'exact'}, optional
12051205
Defaults to 'outer'.
12061206
12071207
Returns
@@ -1225,8 +1225,13 @@ def align(self, other, join='outer'):
12251225
Axis(['a0', 'a1', 'a2'], 'a')
12261226
>>> axis1.align(axis2, join='right')
12271227
Axis(['a1', 'a2', 'a3'], 'a')
1228+
>>> axis1.align(axis2, join='exact') # doctest: +NORMALIZE_WHITESPACE
1229+
Traceback (most recent call last):
1230+
...
1231+
ValueError: align method with join='exact' expected
1232+
Axis(['a0', 'a1', 'a2'], 'a') to be equal to Axis(['a1', 'a2', 'a3'], 'a')
12281233
"""
1229-
assert join in {'outer', 'inner', 'left', 'right'}
1234+
assert join in {'outer', 'inner', 'left', 'right', 'exact'}
12301235
if join == 'outer':
12311236
return self.union(other)
12321237
elif join == 'inner':
@@ -1237,6 +1242,12 @@ def align(self, other, join='outer'):
12371242
if not isinstance(other, Axis):
12381243
other = Axis(other)
12391244
return other
1245+
elif join == 'exact':
1246+
if not self.equals(other):
1247+
raise ValueError("align method with join='exact' "
1248+
"expected {!r} to be equal to {!r}".format(self, other))
1249+
else:
1250+
return self
12401251

12411252
def to_hdf(self, filepath, key=None):
12421253
"""
@@ -2976,7 +2987,7 @@ def align(self, other, join='outer', axes=None):
29762987
Parameters
29772988
----------
29782989
other : AxisCollection
2979-
join : {'outer', 'inner', 'left', 'right'}, optional
2990+
join : {'outer', 'inner', 'left', 'right', 'exact'}, optional
29802991
Defaults to 'outer'.
29812992
axes : AxisReference or sequence of them, optional
29822993
Axes to align. Need to be valid in both arrays. Defaults to None (all common axes). This must be specified
@@ -3047,8 +3058,8 @@ def align(self, other, join='outer', axes=None):
30473058
Axis(['c0'], None)
30483059
])
30493060
"""
3050-
if join not in {'outer', 'inner', 'left', 'right'}:
3051-
raise ValueError("join should be one of 'outer', 'inner', 'left' or 'right'")
3061+
if join not in {'outer', 'inner', 'left', 'right', 'exact'}:
3062+
raise ValueError("join should be one of 'outer', 'inner', 'left', 'right' or 'exact'")
30523063
other = other if isinstance(other, AxisCollection) else AxisCollection(other)
30533064

30543065
# if axes not specified

0 commit comments

Comments
 (0)