Skip to content

Commit

Permalink
Merge pull request #4800 from jreback/series_align
Browse files Browse the repository at this point in the history
CLN: move align out of series consolidating into core/generic.py
  • Loading branch information
jreback committed Sep 10, 2013
2 parents 66c7aec + be3630d commit db8f257
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 64 deletions.
64 changes: 40 additions & 24 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
39 changes: 0 additions & 39 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion pandas/sparse/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down

0 comments on commit db8f257

Please sign in to comment.