@@ -3905,7 +3905,7 @@ def __init__(self,
39053905 raise ValueError ("length of axes %s does not match "
39063906 "data shape %s" % (axes .shape , data .shape ))
39073907
3908- # Because __getattr__ and __setattr__ have been rewritten
3908+ # Because __getattr__ and __setattr__ have been overridden
39093909 object .__setattr__ (self , 'data' , data )
39103910 object .__setattr__ (self , 'axes' , axes )
39113911 object .__setattr__ (self , 'title' , title )
@@ -3937,31 +3937,28 @@ def nonzero(self):
39373937 # can do a[a.nonzero()]
39383938 return self .data .nonzero ()
39393939
3940- def replace_axes (self , axes_to_replace = None , new_axis = None , ** kwargs ):
3940+ def set_axes (self , axes_to_replace = None , new_axis = None , inplace = False , ** kwargs ):
39413941 """
3942- Returns an array with one or several axes replaced .
3942+ Replace one, several or all axes of the array .
39433943
39443944 Parameters
39453945 ----------
3946- axes_to_replace : axis ref or dict {axis ref: axis} or
3947- list of tuple (axis ref, axis) or list of Axis or
3948- AxisCollection
3949- Axes to replace. If a single axis reference is given,
3950- the `new_axes` argument must be used. If a list of
3951- Axis or an AxisCollection is given, all axes will be
3952- replaced by the new ones. In that case, the number of
3953- new axes must match the number of the old ones.
3946+ axes_to_replace : axis ref or dict {axis ref: axis} or list of tuple (axis ref, axis)
3947+ or list of Axis or AxisCollection
3948+ Axes to replace. If a single axis reference is given, the `new_axiss` argument must be provided.
3949+ If a list of Axis or an AxisCollection is given, all axes will be replaced by the new ones.
3950+ In that case, the number of new axes must match the number of the old ones.
39543951 new_axis : Axis
3955- New axis if `axes_to_replace`
3956- contains a single axis reference.
3952+ New axis if `axes_to_replace` contains a single axis reference.
3953+ inplace : bool
3954+ Whether or not to modify the original object or return a new array and leave the original intact.
39573955 **kwargs : Axis
3958- New axis for each axis to replace given
3959- as a keyword argument.
3956+ New axis for each axis to replace given as a keyword argument.
39603957
39613958 Returns
39623959 -------
39633960 LArray
3964- Array with some axes replaced.
3961+ Array with axes replaced.
39653962
39663963 See Also
39673964 --------
@@ -3976,28 +3973,37 @@ def replace_axes(self, axes_to_replace=None, new_axis=None, **kwargs):
39763973 a1 | 3 | 4 | 5
39773974 >>> row = Axis('row', ['r0', 'r1'])
39783975 >>> column = Axis('column', ['c0', 'c1', 'c2'])
3979- >>> arr.replace_axes(x.a, row)
3976+
3977+ Replace one axis (second argument `new_axis` must be provided)
3978+
3979+ >>> arr.set_axes(x.a, row)
39803980 row\\ b | b0 | b1 | b2
39813981 r0 | 0 | 1 | 2
39823982 r1 | 3 | 4 | 5
3983- >>> arr.replace_axes([row, column])
3983+
3984+ Replace several axes (keywords, list of tuple or dictionary)
3985+
3986+ >>> arr.set_axes(a=row, b=column)
39843987 row\\ column | c0 | c1 | c2
39853988 r0 | 0 | 1 | 2
39863989 r1 | 3 | 4 | 5
3987- >>> arr.replace_axes(a= row, b= column)
3990+ >>> arr.set_axes([(x.a, row), (x.b, column)] )
39883991 row\\ column | c0 | c1 | c2
39893992 r0 | 0 | 1 | 2
39903993 r1 | 3 | 4 | 5
3991- >>> arr.replace_axes([( x.a, row), ( x.b, column)] )
3994+ >>> arr.set_axes({ x.a: row, x.b: column} )
39923995 row\\ column | c0 | c1 | c2
39933996 r0 | 0 | 1 | 2
39943997 r1 | 3 | 4 | 5
3995- >>> arr.replace_axes({x.a: row, x.b: column})
3998+
3999+ Replace all axes (list of axes or AxisCollection)
4000+
4001+ >>> arr.set_axes([row, column])
39964002 row\\ column | c0 | c1 | c2
39974003 r0 | 0 | 1 | 2
39984004 r1 | 3 | 4 | 5
39994005 >>> arr2 = ndrange([row, column])
4000- >>> arr.replace_axes (arr2.axes)
4006+ >>> arr.set_axes (arr2.axes)
40014007 row\\ column | c0 | c1 | c2
40024008 r0 | 0 | 1 | 2
40034009 r1 | 3 | 4 | 5
@@ -4021,13 +4027,17 @@ def replace_axes(self, axes_to_replace=None, new_axis=None, **kwargs):
40214027 items += kwargs .items ()
40224028 for old , new in items :
40234029 axes = axes .replace (old , new )
4024- return LArray (self .data , axes , title = self .title )
4030+ if inplace :
4031+ object .__setattr__ (self , 'axes' , axes )
4032+ return self
4033+ else :
4034+ return LArray (self .data , axes , title = self .title )
40254035
40264036 def with_axes (self , axes ):
40274037 warnings .warn ("LArray.with_axes is deprecated, "
40284038 "use LArray.replace_axes instead" ,
40294039 DeprecationWarning )
4030- return self .replace_axes (axes )
4040+ return self .set_axes (axes )
40314041
40324042 def __getattr__ (self , key ):
40334043 try :
@@ -4271,28 +4281,27 @@ def __bool__(self):
42714281 # Python 2
42724282 __nonzero__ = __bool__
42734283
4274- def rename (self , renames = None , to = None , ** kwargs ):
4275- """Renames some array axes .
4284+ def rename (self , renames = None , to = None , inplace = False , ** kwargs ):
4285+ """Renames axes of the array .
42764286
42774287 Parameters
42784288 ----------
42794289 renames : axis ref or dict {axis ref: str} or
42804290 list of tuple (axis ref, str)
4281- Renames to apply. If a single axis reference
4282- is given, the `to` argument must be used.
4283- to : str or Axis
4284- New name if `renames` contains a single axis reference
4285- **kwargs : str
4291+ Renames to apply. If a single axis reference is given, the `to` argument must be used.
4292+ to : string or Axis
4293+ New name if `renames` contains a single axis reference.
4294+ **kwargs :
42864295 New name for each axis given as a keyword argument.
42874296
42884297 Returns
42894298 -------
42904299 LArray
4291- Array with some axes renamed.
4300+ Array with axes renamed.
42924301
42934302 See Also
42944303 --------
4295- replace_axes : replace one or several axes
4304+ set_axes : replace one or several axes
42964305
42974306 Examples
42984307 --------
@@ -4332,7 +4341,11 @@ def rename(self, renames=None, to=None, **kwargs):
43324341 renames = {self .axes [k ]: v for k , v in items }
43334342 axes = [a .rename (renames [a ]) if a in renames else a
43344343 for a in self .axes ]
4335- return LArray (self .data , axes )
4344+ if inplace :
4345+ object .__setattr__ (self , 'axes' , AxisCollection (axes ))
4346+ return self
4347+ else :
4348+ return LArray (self .data , axes )
43364349
43374350 def sort_values (self , key ):
43384351 """Sorts values of the array.
@@ -8657,13 +8670,12 @@ def set_labels(self, axis, labels, inplace=False):
86578670
86588671 Parameters
86598672 ----------
8660- axis
8673+ axis : string or Axis
86618674 Axis for which we want to replace the labels.
8662- labels : list of axis labels
8663- New labels.
8675+ labels : int or iterable
8676+ Integer or list of values usable as the collection of labels for an Axis .
86648677 inplace : bool
8665- Whether or not to modify the original object or return a new
8666- array and leave the original intact.
8678+ Whether or not to modify the original object or return a new array and leave the original intact.
86678679
86688680 Returns
86698681 -------
@@ -8677,6 +8689,10 @@ def set_labels(self, axis, labels, inplace=False):
86778689 nat\\ sex | M | F
86788690 BE | 0 | 1
86798691 FO | 2 | 3
8692+ >>> a.set_labels(x.sex, 'Men,Women')
8693+ nat\\ sex | Men | Women
8694+ BE | 0 | 1
8695+ FO | 2 | 3
86808696 >>> a.set_labels(x.sex, ['Men', 'Women'])
86818697 nat\\ sex | Men | Women
86828698 BE | 0 | 1
@@ -8687,8 +8703,7 @@ def set_labels(self, axis, labels, inplace=False):
86878703 axis .labels = labels
86888704 return self
86898705 else :
8690- return LArray (self .data ,
8691- self .axes .replace (axis , Axis (axis .name , labels )))
8706+ return LArray (self .data , self .axes .replace (axis , Axis (axis .name , labels )))
86928707
86938708 def astype (self , dtype , order = 'K' , casting = 'unsafe' , subok = True , copy = True ):
86948709 return LArray (self .data .astype (dtype , order , casting , subok , copy ),
0 commit comments