Skip to content

CLN: move align out of series consolidating into core/generic.py #4800

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

Merged
merged 1 commit into from
Sep 10, 2013
Merged
Show file tree
Hide file tree
Changes from all 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
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