@@ -4144,18 +4144,19 @@ def align(self, other, join='outer', axis=None, level=None, copy=True,
41444144 if isinstance (self , Series ):
41454145 # this means other is a DataFrame, and we need to broadcast
41464146 # self
4147- df = DataFrame (
4148- dict ((c , self ) for c in other .columns ),
4149- ** other ._construct_axes_dict ())
4147+ cons = self . _constructor_expanddim
4148+ df = cons ( dict ((c , self ) for c in other .columns ),
4149+ ** other ._construct_axes_dict ())
41504150 return df ._align_frame (other , join = join , axis = axis ,
41514151 level = level , copy = copy ,
41524152 fill_value = fill_value , method = method ,
41534153 limit = limit , fill_axis = fill_axis )
41544154 elif isinstance (other , Series ):
41554155 # this means self is a DataFrame, and we need to broadcast
41564156 # other
4157- df = DataFrame (dict ((c , other ) for c in self .columns ),
4158- ** self ._construct_axes_dict ())
4157+ cons = other ._constructor_expanddim
4158+ df = cons (dict ((c , other ) for c in self .columns ),
4159+ ** self ._construct_axes_dict ())
41594160 return self ._align_frame (df , join = join , axis = axis , level = level ,
41604161 copy = copy , fill_value = fill_value ,
41614162 method = method , limit = limit ,
@@ -4184,20 +4185,27 @@ def _align_frame(self, other, join='outer', axis=None, level=None,
41844185 ilidx , iridx = None , None
41854186 clidx , cridx = None , None
41864187
4188+ is_series = isinstance (self , ABCSeries )
4189+
41874190 if axis is None or axis == 0 :
41884191 if not self .index .equals (other .index ):
41894192 join_index , ilidx , iridx = self .index .join (
41904193 other .index , how = join , level = level , return_indexers = True )
41914194
41924195 if axis is None or axis == 1 :
4193- if not self .columns .equals (other .columns ):
4196+ if not is_series and not self .columns .equals (other .columns ):
41944197 join_columns , clidx , cridx = self .columns .join (
41954198 other .columns , how = join , level = level , return_indexers = True )
41964199
4197- left = self ._reindex_with_indexers ({0 : [join_index , ilidx ],
4198- 1 : [join_columns , clidx ]},
4199- copy = copy , fill_value = fill_value ,
4200+ if is_series :
4201+ reindexers = {0 : [join_index , ilidx ]}
4202+ else :
4203+ reindexers = {0 : [join_index , ilidx ], 1 : [join_columns , clidx ]}
4204+
4205+ left = self ._reindex_with_indexers (reindexers , copy = copy ,
4206+ fill_value = fill_value ,
42004207 allow_dups = True )
4208+ # other must be always DataFrame
42014209 right = other ._reindex_with_indexers ({0 : [join_index , iridx ],
42024210 1 : [join_columns , cridx ]},
42034211 copy = copy , fill_value = fill_value ,
@@ -4212,10 +4220,8 @@ def _align_frame(self, other, join='outer', axis=None, level=None,
42124220 def _align_series (self , other , join = 'outer' , axis = None , level = None ,
42134221 copy = True , fill_value = None , method = None , limit = None ,
42144222 fill_axis = 0 ):
4215- from pandas import DataFrame
4216-
4217- # series/series compat
4218- if isinstance (self , ABCSeries ) and isinstance (other , ABCSeries ):
4223+ # series/series compat, other must always be a Series
4224+ if isinstance (self , ABCSeries ):
42194225 if axis :
42204226 raise ValueError ('cannot align series to a series other than '
42214227 'axis 0' )
@@ -4261,7 +4267,7 @@ def _align_series(self, other, join='outer', axis=None, level=None,
42614267 if copy and fdata is self ._data :
42624268 fdata = fdata .copy ()
42634269
4264- left = DataFrame (fdata )
4270+ left = self . _constructor (fdata )
42654271
42664272 if ridx is None :
42674273 right = other
0 commit comments