diff --git a/pandas/core/generic.py b/pandas/core/generic.py index d5c265dcf93a0..d6daa467752a9 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -2202,7 +2202,7 @@ def last(self, offset): return self.ix[start:] def align(self, other, join='outer', axis=None, level=None, copy=True, - fill_value=np.nan, method=None, limit=None, fill_axis=0): + fill_value=None, method=None, limit=None, fill_axis=0): """ Align two object on their axes with the specified join method for each axis Index @@ -2288,35 +2288,51 @@ def _align_series(self, other, join='outer', axis=None, level=None, fill_axis=0): from pandas import DataFrame - fdata = self._data - if axis == 0: - join_index = self.index - lidx, ridx = None, None - if not self.index.equals(other.index): - join_index, lidx, ridx = self.index.join(other.index, how=join, - return_indexers=True) + # series/series compat + if isinstance(self, ABCSeries) and isinstance(other, ABCSeries): + if axis: + raise ValueError('cannot align series to a series other than axis 0') - if lidx is not None: - fdata = fdata.reindex_indexer(join_index, lidx, axis=1) - elif axis == 1: - join_index = self.columns - lidx, ridx = None, None - if not self.columns.equals(other.index): - join_index, lidx, ridx = \ - self.columns.join(other.index, how=join, - return_indexers=True) + join_index, lidx, ridx = self.index.join(other.index, how=join, + level=level, + return_indexers=True) + + left_result = self._reindex_indexer(join_index, lidx, copy) + right_result = other._reindex_indexer(join_index, ridx, copy) - if lidx is not None: - fdata = fdata.reindex_indexer(join_index, lidx, axis=0) else: - raise ValueError('Must specify axis=0 or 1') - if copy and fdata is self._data: - fdata = fdata.copy() + # one has > 1 ndim + fdata = self._data + if axis == 0: + join_index = self.index + lidx, ridx = None, None + if not self.index.equals(other.index): + join_index, lidx, ridx = self.index.join(other.index, how=join, + return_indexers=True) + + if lidx is not None: + fdata = fdata.reindex_indexer(join_index, lidx, axis=1) + elif axis == 1: + join_index = self.columns + lidx, ridx = None, None + if not self.columns.equals(other.index): + join_index, lidx, ridx = \ + self.columns.join(other.index, how=join, + return_indexers=True) + + if lidx is not None: + fdata = fdata.reindex_indexer(join_index, lidx, axis=0) + else: + raise ValueError('Must specify axis=0 or 1') + + if copy and fdata is self._data: + fdata = fdata.copy() - left_result = DataFrame(fdata) - right_result = other if ridx is None else other.reindex(join_index) + left_result = DataFrame(fdata) + right_result = other if ridx is None else other.reindex(join_index) + # fill fill_na = notnull(fill_value) or (method is not None) if fill_na: return (left_result.fillna(fill_value, method=method, limit=limit, diff --git a/pandas/core/series.py b/pandas/core/series.py index 3f93f210ad4cf..32f6765b5d84d 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -2667,45 +2667,6 @@ def apply(self, func, convert_dtype=True, args=(), **kwds): else: return self._constructor(mapped, index=self.index, name=self.name) - def align(self, other, join='outer', axis=None, level=None, copy=True, - fill_value=None, method=None, limit=None): - """ - Align two Series object with the specified join method - - Parameters - ---------- - other : Series - join : {'outer', 'inner', 'left', 'right'}, default 'outer' - axis : None, alignment axis (is 0 for Series) - level : int or name - Broadcast across a level, matching Index values on the - passed MultiIndex level - copy : boolean, default True - Always return new objects. If copy=False and no reindexing is - required, the same object will be returned (for better performance) - fill_value : object, default None - method : str, default 'pad' - limit : int, default None - fill_value, method, inplace, limit are passed to fillna - - Returns - ------- - (left, right) : (Series, Series) - Aligned Series - """ - join_index, lidx, ridx = self.index.join(other.index, how=join, - level=level, - return_indexers=True) - - left = self._reindex_indexer(join_index, lidx, copy) - right = other._reindex_indexer(join_index, ridx, copy) - fill_na = (fill_value is not None) or (method is not None) - if fill_na: - return (left.fillna(fill_value, method=method, limit=limit), - right.fillna(fill_value, method=method, limit=limit)) - else: - return left, right - def _reindex_indexer(self, new_index, indexer, copy): if indexer is None: if copy: diff --git a/pandas/sparse/frame.py b/pandas/sparse/frame.py index d8f6d531a6983..7d2571e6c3c74 100644 --- a/pandas/sparse/frame.py +++ b/pandas/sparse/frame.py @@ -573,11 +573,14 @@ def _reindex_columns(self, columns, copy, level, fill_value, limit=None, return SparseDataFrame(sdict, index=self.index, columns=columns, default_fill_value=self._default_fill_value) - def _reindex_with_indexers(self, reindexers, method=None, fill_value=np.nan, limit=None, copy=False): + def _reindex_with_indexers(self, reindexers, method=None, fill_value=None, limit=None, copy=False): if method is not None or limit is not None: raise NotImplementedError("cannot reindex with a method or limit with sparse") + if fill_value is None: + fill_value = np.nan + index, row_indexer = reindexers.get(0, (None, None)) columns, col_indexer = reindexers.get(1, (None, None))