@@ -3905,7 +3905,7 @@ def __init__(self,
3905
3905
raise ValueError ("length of axes %s does not match "
3906
3906
"data shape %s" % (axes .shape , data .shape ))
3907
3907
3908
- # Because __getattr__ and __setattr__ have been rewritten
3908
+ # Because __getattr__ and __setattr__ have been overridden
3909
3909
object .__setattr__ (self , 'data' , data )
3910
3910
object .__setattr__ (self , 'axes' , axes )
3911
3911
object .__setattr__ (self , 'title' , title )
@@ -3937,31 +3937,28 @@ def nonzero(self):
3937
3937
# can do a[a.nonzero()]
3938
3938
return self .data .nonzero ()
3939
3939
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 ):
3941
3941
"""
3942
- Returns an array with one or several axes replaced .
3942
+ Replace one, several or all axes of the array .
3943
3943
3944
3944
Parameters
3945
3945
----------
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.
3954
3951
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.
3957
3955
**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.
3960
3957
3961
3958
Returns
3962
3959
-------
3963
3960
LArray
3964
- Array with some axes replaced.
3961
+ Array with axes replaced.
3965
3962
3966
3963
See Also
3967
3964
--------
@@ -3976,28 +3973,37 @@ def replace_axes(self, axes_to_replace=None, new_axis=None, **kwargs):
3976
3973
a1 | 3 | 4 | 5
3977
3974
>>> row = Axis('row', ['r0', 'r1'])
3978
3975
>>> 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)
3980
3980
row\\ b | b0 | b1 | b2
3981
3981
r0 | 0 | 1 | 2
3982
3982
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)
3984
3987
row\\ column | c0 | c1 | c2
3985
3988
r0 | 0 | 1 | 2
3986
3989
r1 | 3 | 4 | 5
3987
- >>> arr.replace_axes(a= row, b= column)
3990
+ >>> arr.set_axes([(x.a, row), (x.b, column)] )
3988
3991
row\\ column | c0 | c1 | c2
3989
3992
r0 | 0 | 1 | 2
3990
3993
r1 | 3 | 4 | 5
3991
- >>> arr.replace_axes([( x.a, row), ( x.b, column)] )
3994
+ >>> arr.set_axes({ x.a: row, x.b: column} )
3992
3995
row\\ column | c0 | c1 | c2
3993
3996
r0 | 0 | 1 | 2
3994
3997
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])
3996
4002
row\\ column | c0 | c1 | c2
3997
4003
r0 | 0 | 1 | 2
3998
4004
r1 | 3 | 4 | 5
3999
4005
>>> arr2 = ndrange([row, column])
4000
- >>> arr.replace_axes (arr2.axes)
4006
+ >>> arr.set_axes (arr2.axes)
4001
4007
row\\ column | c0 | c1 | c2
4002
4008
r0 | 0 | 1 | 2
4003
4009
r1 | 3 | 4 | 5
@@ -4021,13 +4027,17 @@ def replace_axes(self, axes_to_replace=None, new_axis=None, **kwargs):
4021
4027
items += kwargs .items ()
4022
4028
for old , new in items :
4023
4029
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 )
4025
4035
4026
4036
def with_axes (self , axes ):
4027
4037
warnings .warn ("LArray.with_axes is deprecated, "
4028
4038
"use LArray.replace_axes instead" ,
4029
4039
DeprecationWarning )
4030
- return self .replace_axes (axes )
4040
+ return self .set_axes (axes )
4031
4041
4032
4042
def __getattr__ (self , key ):
4033
4043
try :
@@ -4271,28 +4281,27 @@ def __bool__(self):
4271
4281
# Python 2
4272
4282
__nonzero__ = __bool__
4273
4283
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 .
4276
4286
4277
4287
Parameters
4278
4288
----------
4279
4289
renames : axis ref or dict {axis ref: str} or
4280
4290
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 :
4286
4295
New name for each axis given as a keyword argument.
4287
4296
4288
4297
Returns
4289
4298
-------
4290
4299
LArray
4291
- Array with some axes renamed.
4300
+ Array with axes renamed.
4292
4301
4293
4302
See Also
4294
4303
--------
4295
- replace_axes : replace one or several axes
4304
+ set_axes : replace one or several axes
4296
4305
4297
4306
Examples
4298
4307
--------
@@ -4332,7 +4341,11 @@ def rename(self, renames=None, to=None, **kwargs):
4332
4341
renames = {self .axes [k ]: v for k , v in items }
4333
4342
axes = [a .rename (renames [a ]) if a in renames else a
4334
4343
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 )
4336
4349
4337
4350
def sort_values (self , key ):
4338
4351
"""Sorts values of the array.
@@ -8657,13 +8670,12 @@ def set_labels(self, axis, labels, inplace=False):
8657
8670
8658
8671
Parameters
8659
8672
----------
8660
- axis
8673
+ axis : string or Axis
8661
8674
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 .
8664
8677
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.
8667
8679
8668
8680
Returns
8669
8681
-------
@@ -8677,6 +8689,10 @@ def set_labels(self, axis, labels, inplace=False):
8677
8689
nat\\ sex | M | F
8678
8690
BE | 0 | 1
8679
8691
FO | 2 | 3
8692
+ >>> a.set_labels(x.sex, 'Men,Women')
8693
+ nat\\ sex | Men | Women
8694
+ BE | 0 | 1
8695
+ FO | 2 | 3
8680
8696
>>> a.set_labels(x.sex, ['Men', 'Women'])
8681
8697
nat\\ sex | Men | Women
8682
8698
BE | 0 | 1
@@ -8687,8 +8703,7 @@ def set_labels(self, axis, labels, inplace=False):
8687
8703
axis .labels = labels
8688
8704
return self
8689
8705
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 )))
8692
8707
8693
8708
def astype (self , dtype , order = 'K' , casting = 'unsafe' , subok = True , copy = True ):
8694
8709
return LArray (self .data .astype (dtype , order , casting , subok , copy ),
0 commit comments