Skip to content

Commit db8f257

Browse files
committed
Merge pull request #4800 from jreback/series_align
CLN: move align out of series consolidating into core/generic.py
2 parents 66c7aec + be3630d commit db8f257

File tree

3 files changed

+44
-64
lines changed

3 files changed

+44
-64
lines changed

pandas/core/generic.py

+40-24
Original file line numberDiff line numberDiff line change
@@ -2202,7 +2202,7 @@ def last(self, offset):
22022202
return self.ix[start:]
22032203

22042204
def align(self, other, join='outer', axis=None, level=None, copy=True,
2205-
fill_value=np.nan, method=None, limit=None, fill_axis=0):
2205+
fill_value=None, method=None, limit=None, fill_axis=0):
22062206
"""
22072207
Align two object on their axes with the
22082208
specified join method for each axis Index
@@ -2288,35 +2288,51 @@ def _align_series(self, other, join='outer', axis=None, level=None,
22882288
fill_axis=0):
22892289
from pandas import DataFrame
22902290

2291-
fdata = self._data
2292-
if axis == 0:
2293-
join_index = self.index
2294-
lidx, ridx = None, None
2295-
if not self.index.equals(other.index):
2296-
join_index, lidx, ridx = self.index.join(other.index, how=join,
2297-
return_indexers=True)
2291+
# series/series compat
2292+
if isinstance(self, ABCSeries) and isinstance(other, ABCSeries):
2293+
if axis:
2294+
raise ValueError('cannot align series to a series other than axis 0')
22982295

2299-
if lidx is not None:
2300-
fdata = fdata.reindex_indexer(join_index, lidx, axis=1)
2301-
elif axis == 1:
2302-
join_index = self.columns
2303-
lidx, ridx = None, None
2304-
if not self.columns.equals(other.index):
2305-
join_index, lidx, ridx = \
2306-
self.columns.join(other.index, how=join,
2307-
return_indexers=True)
2296+
join_index, lidx, ridx = self.index.join(other.index, how=join,
2297+
level=level,
2298+
return_indexers=True)
2299+
2300+
left_result = self._reindex_indexer(join_index, lidx, copy)
2301+
right_result = other._reindex_indexer(join_index, ridx, copy)
23082302

2309-
if lidx is not None:
2310-
fdata = fdata.reindex_indexer(join_index, lidx, axis=0)
23112303
else:
2312-
raise ValueError('Must specify axis=0 or 1')
23132304

2314-
if copy and fdata is self._data:
2315-
fdata = fdata.copy()
2305+
# one has > 1 ndim
2306+
fdata = self._data
2307+
if axis == 0:
2308+
join_index = self.index
2309+
lidx, ridx = None, None
2310+
if not self.index.equals(other.index):
2311+
join_index, lidx, ridx = self.index.join(other.index, how=join,
2312+
return_indexers=True)
2313+
2314+
if lidx is not None:
2315+
fdata = fdata.reindex_indexer(join_index, lidx, axis=1)
2316+
elif axis == 1:
2317+
join_index = self.columns
2318+
lidx, ridx = None, None
2319+
if not self.columns.equals(other.index):
2320+
join_index, lidx, ridx = \
2321+
self.columns.join(other.index, how=join,
2322+
return_indexers=True)
2323+
2324+
if lidx is not None:
2325+
fdata = fdata.reindex_indexer(join_index, lidx, axis=0)
2326+
else:
2327+
raise ValueError('Must specify axis=0 or 1')
2328+
2329+
if copy and fdata is self._data:
2330+
fdata = fdata.copy()
23162331

2317-
left_result = DataFrame(fdata)
2318-
right_result = other if ridx is None else other.reindex(join_index)
2332+
left_result = DataFrame(fdata)
2333+
right_result = other if ridx is None else other.reindex(join_index)
23192334

2335+
# fill
23202336
fill_na = notnull(fill_value) or (method is not None)
23212337
if fill_na:
23222338
return (left_result.fillna(fill_value, method=method, limit=limit,

pandas/core/series.py

-39
Original file line numberDiff line numberDiff line change
@@ -2667,45 +2667,6 @@ def apply(self, func, convert_dtype=True, args=(), **kwds):
26672667
else:
26682668
return self._constructor(mapped, index=self.index, name=self.name)
26692669

2670-
def align(self, other, join='outer', axis=None, level=None, copy=True,
2671-
fill_value=None, method=None, limit=None):
2672-
"""
2673-
Align two Series object with the specified join method
2674-
2675-
Parameters
2676-
----------
2677-
other : Series
2678-
join : {'outer', 'inner', 'left', 'right'}, default 'outer'
2679-
axis : None, alignment axis (is 0 for Series)
2680-
level : int or name
2681-
Broadcast across a level, matching Index values on the
2682-
passed MultiIndex level
2683-
copy : boolean, default True
2684-
Always return new objects. If copy=False and no reindexing is
2685-
required, the same object will be returned (for better performance)
2686-
fill_value : object, default None
2687-
method : str, default 'pad'
2688-
limit : int, default None
2689-
fill_value, method, inplace, limit are passed to fillna
2690-
2691-
Returns
2692-
-------
2693-
(left, right) : (Series, Series)
2694-
Aligned Series
2695-
"""
2696-
join_index, lidx, ridx = self.index.join(other.index, how=join,
2697-
level=level,
2698-
return_indexers=True)
2699-
2700-
left = self._reindex_indexer(join_index, lidx, copy)
2701-
right = other._reindex_indexer(join_index, ridx, copy)
2702-
fill_na = (fill_value is not None) or (method is not None)
2703-
if fill_na:
2704-
return (left.fillna(fill_value, method=method, limit=limit),
2705-
right.fillna(fill_value, method=method, limit=limit))
2706-
else:
2707-
return left, right
2708-
27092670
def _reindex_indexer(self, new_index, indexer, copy):
27102671
if indexer is None:
27112672
if copy:

pandas/sparse/frame.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -573,11 +573,14 @@ def _reindex_columns(self, columns, copy, level, fill_value, limit=None,
573573
return SparseDataFrame(sdict, index=self.index, columns=columns,
574574
default_fill_value=self._default_fill_value)
575575

576-
def _reindex_with_indexers(self, reindexers, method=None, fill_value=np.nan, limit=None, copy=False):
576+
def _reindex_with_indexers(self, reindexers, method=None, fill_value=None, limit=None, copy=False):
577577

578578
if method is not None or limit is not None:
579579
raise NotImplementedError("cannot reindex with a method or limit with sparse")
580580

581+
if fill_value is None:
582+
fill_value = np.nan
583+
581584
index, row_indexer = reindexers.get(0, (None, None))
582585
columns, col_indexer = reindexers.get(1, (None, None))
583586

0 commit comments

Comments
 (0)