1
- # -*- coding: utf8 -*-
2
- from __future__ import absolute_import , division , print_function
3
-
4
1
"""
5
2
Array class
6
3
"""
30
27
31
28
from collections import OrderedDict
32
29
from itertools import product , chain , groupby , islice , repeat
30
+ from collections .abc import Iterable , Sequence
31
+ import builtins
33
32
import os
34
33
import sys
35
34
import functools
59
58
float_error_handler_factory , light_product , common_type ,
60
59
renamed_to , deprecate_kwarg , LHDFStore , lazy_attribute , unique_multi , SequenceZip ,
61
60
Repeater , Product , ensure_no_numpy_type )
62
- from larray .util .compat import PY2 , basestring , Iterable , Sequence , builtins
63
61
from larray .util .options import _OPTIONS , DISPLAY_MAXLINES , DISPLAY_EDGEITEMS , DISPLAY_WIDTH , DISPLAY_PRECISION
64
62
65
63
@@ -305,42 +303,23 @@ def concat(arrays, axis=0, dtype=None):
305
303
return result
306
304
307
305
308
- if PY2 :
309
- class ArrayIterator (object ):
310
- __slots__ = ('next' ,)
311
-
312
- def __init__ (self , array ):
313
- data_iter = iter (array .data )
314
- next_data_func = data_iter .next
315
- res_axes = array .axes [1 :]
316
- # this case should not happen (handled by the fastpath in Array.__iter__)
317
- assert len (res_axes ) > 0
318
-
319
- def next_func ():
320
- return Array (next_data_func (), res_axes )
306
+ class ArrayIterator (object ):
307
+ __slots__ = ('__next__' ,)
321
308
322
- self .next = next_func
323
-
324
- def __iter__ (self ):
325
- return self
326
- else :
327
- class ArrayIterator (object ):
328
- __slots__ = ('__next__' ,)
329
-
330
- def __init__ (self , array ):
331
- data_iter = iter (array .data )
332
- next_data_func = data_iter .__next__
333
- res_axes = array .axes [1 :]
334
- # this case should not happen (handled by the fastpath in Array.__iter__)
335
- assert len (res_axes ) > 0
309
+ def __init__ (self , array ):
310
+ data_iter = iter (array .data )
311
+ next_data_func = data_iter .__next__
312
+ res_axes = array .axes [1 :]
313
+ # this case should not happen (handled by the fastpath in Array.__iter__)
314
+ assert len (res_axes ) > 0
336
315
337
- def next_func ():
338
- return Array (next_data_func (), res_axes )
316
+ def next_func ():
317
+ return Array (next_data_func (), res_axes )
339
318
340
- self .__next__ = next_func
319
+ self .__next__ = next_func
341
320
342
- def __iter__ (self ):
343
- return self
321
+ def __iter__ (self ):
322
+ return self
344
323
345
324
346
325
# TODO: rename to ArrayIndexIndexer or something like that
@@ -826,7 +805,7 @@ def title(self):
826
805
def title (self , title ):
827
806
import warnings
828
807
warnings .warn ("title attribute is deprecated. Please use meta.title instead" , FutureWarning , stacklevel = 2 )
829
- if not isinstance (title , basestring ):
808
+ if not isinstance (title , str ):
830
809
raise TypeError ("Expected string value, got {}" .format (type (title ).__name__ ))
831
810
self ._meta .title = title
832
811
@@ -1411,8 +1390,6 @@ def __array_wrap__(self, out_arr, context=None):
1411
1390
1412
1391
def __bool__ (self ):
1413
1392
return bool (self .data )
1414
- # Python 2
1415
- __nonzero__ = __bool__
1416
1393
1417
1394
# TODO: either support a list (of axes names) as first argument here (and set_labels)
1418
1395
# or don't support that in set_axes
@@ -1591,7 +1568,7 @@ def reindex(self, axes_to_reindex=None, new_axis=None, fill_value=nan, inplace=F
1591
1568
a1 c0 2 1
1592
1569
"""
1593
1570
# XXX: can't we move this to AxisCollection.replace?
1594
- if isinstance (axes_to_reindex , basestring ) and '=' in axes_to_reindex :
1571
+ if isinstance (axes_to_reindex , str ) and '=' in axes_to_reindex :
1595
1572
axes_to_reindex = Axis (axes_to_reindex )
1596
1573
if isinstance (axes_to_reindex , (Group , Axis )) and not isinstance (axes_to_reindex , AxisReference ):
1597
1574
new_axis = axes_to_reindex if isinstance (axes_to_reindex , Axis ) else Axis (axes_to_reindex )
@@ -2820,7 +2797,7 @@ def to_labelgroup(key, stack_depth=1):
2820
2797
if not all (g .axis .equals (axis ) for g in groups [1 :]):
2821
2798
raise ValueError ("group with different axes: %s" % str (key ))
2822
2799
return groups
2823
- if isinstance (key , (Group , int , basestring , list , slice )):
2800
+ if isinstance (key , (Group , int , str , list , slice )):
2824
2801
return self .axes ._guess_axis (key )
2825
2802
else :
2826
2803
raise NotImplementedError ("%s has invalid type (%s) for a group aggregate key"
@@ -7744,10 +7721,10 @@ def split_axes(self, axes=None, sep='_', names=None, regex=None, sort=False, fil
7744
7721
# * somehow factorize this code with AxisCollection.split_axes
7745
7722
if axes is None :
7746
7723
axes = {axis : None for axis in array .axes if axis .name is not None and sep in axis .name }
7747
- elif isinstance (axes , (int , basestring , Axis )):
7724
+ elif isinstance (axes , (int , str , Axis )):
7748
7725
axes = {axes : names }
7749
7726
elif isinstance (axes , (list , tuple )):
7750
- if all (isinstance (axis , (int , basestring , Axis )) for axis in axes ):
7727
+ if all (isinstance (axis , (int , str , Axis )) for axis in axes ):
7751
7728
axes = {axis : None for axis in axes }
7752
7729
else :
7753
7730
raise ValueError ("Expected tuple or list of int, string or Axis instances" )
@@ -9288,12 +9265,12 @@ def stack(elements=None, axes=None, title=None, meta=None, dtype=None, res_axes=
9288
9265
if elements is not None and kwargs :
9289
9266
raise TypeError ("stack() accepts either keyword arguments OR a collection of elements, not both" )
9290
9267
9291
- if isinstance (axes , basestring ) and '=' in axes :
9268
+ if isinstance (axes , str ) and '=' in axes :
9292
9269
axes = Axis (axes )
9293
9270
elif isinstance (axes , Group ):
9294
9271
axes = Axis (axes )
9295
9272
9296
- if axes is not None and not isinstance (axes , basestring ):
9273
+ if axes is not None and not isinstance (axes , str ):
9297
9274
axes = AxisCollection (axes )
9298
9275
9299
9276
if kwargs :
@@ -9321,7 +9298,7 @@ def stack(elements=None, axes=None, title=None, meta=None, dtype=None, res_axes=
9321
9298
9322
9299
if all (isinstance (e , tuple ) for e in elements ):
9323
9300
assert all (len (e ) == 2 for e in elements )
9324
- if axes is None or isinstance (axes , basestring ):
9301
+ if axes is None or isinstance (axes , str ):
9325
9302
keys = [k for k , v in elements ]
9326
9303
values = [v for k , v in elements ]
9327
9304
# assert that all keys are indexers
@@ -9338,7 +9315,7 @@ def translate_and_sort_key(key, axes):
9338
9315
dict_elements = {translate_and_sort_key (key , axes ): value for key , value in elements }
9339
9316
items = [(k , dict_elements [k ]) for k in axes .iter_labels ()]
9340
9317
else :
9341
- if axes is None or isinstance (axes , basestring ):
9318
+ if axes is None or isinstance (axes , str ):
9342
9319
axes = AxisCollection (Axis (len (elements ), axes ))
9343
9320
else :
9344
9321
# TODO: add support for more than one axis here
@@ -9568,7 +9545,7 @@ def values_with_expand(value, axes, readonly=True, ascending=True):
9568
9545
if not isinstance (axes , (tuple , list , AxisCollection )):
9569
9546
axes = (axes ,)
9570
9547
# transform string axes definitions to objects
9571
- axes = [Axis (axis ) if isinstance (axis , basestring ) and '=' in axis else axis
9548
+ axes = [Axis (axis ) if isinstance (axis , str ) and '=' in axis else axis
9572
9549
for axis in axes ]
9573
9550
# transform string axes references to objects
9574
9551
axes = AxisCollection ([axis if isinstance (axis , Axis ) else all_axes [axis ]
0 commit comments