1- # -*- coding: utf8 -*-
2- from __future__ import absolute_import , division , print_function
3-
41"""
52Array class
63"""
3027
3128from collections import OrderedDict
3229from itertools import product , chain , groupby , islice , repeat
30+ from collections .abc import Iterable , Sequence
31+ import builtins
3332import os
3433import sys
3534import functools
5958 float_error_handler_factory , light_product , common_type ,
6059 renamed_to , deprecate_kwarg , LHDFStore , lazy_attribute , unique_multi , SequenceZip ,
6160 Repeater , Product , ensure_no_numpy_type )
62- from larray .util .compat import PY2 , basestring , Iterable , Sequence , builtins
6361from larray .util .options import _OPTIONS , DISPLAY_MAXLINES , DISPLAY_EDGEITEMS , DISPLAY_WIDTH , DISPLAY_PRECISION
6462
6563
@@ -305,42 +303,23 @@ def concat(arrays, axis=0, dtype=None):
305303 return result
306304
307305
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__' ,)
321308
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
336315
337- def next_func ():
338- return Array (next_data_func (), res_axes )
316+ def next_func ():
317+ return Array (next_data_func (), res_axes )
339318
340- self .__next__ = next_func
319+ self .__next__ = next_func
341320
342- def __iter__ (self ):
343- return self
321+ def __iter__ (self ):
322+ return self
344323
345324
346325# TODO: rename to ArrayIndexIndexer or something like that
@@ -826,7 +805,7 @@ def title(self):
826805 def title (self , title ):
827806 import warnings
828807 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 ):
830809 raise TypeError ("Expected string value, got {}" .format (type (title ).__name__ ))
831810 self ._meta .title = title
832811
@@ -1411,8 +1390,6 @@ def __array_wrap__(self, out_arr, context=None):
14111390
14121391 def __bool__ (self ):
14131392 return bool (self .data )
1414- # Python 2
1415- __nonzero__ = __bool__
14161393
14171394 # TODO: either support a list (of axes names) as first argument here (and set_labels)
14181395 # 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
15911568 a1 c0 2 1
15921569 """
15931570 # 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 :
15951572 axes_to_reindex = Axis (axes_to_reindex )
15961573 if isinstance (axes_to_reindex , (Group , Axis )) and not isinstance (axes_to_reindex , AxisReference ):
15971574 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):
28202797 if not all (g .axis .equals (axis ) for g in groups [1 :]):
28212798 raise ValueError ("group with different axes: %s" % str (key ))
28222799 return groups
2823- if isinstance (key , (Group , int , basestring , list , slice )):
2800+ if isinstance (key , (Group , int , str , list , slice )):
28242801 return self .axes ._guess_axis (key )
28252802 else :
28262803 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
77447721 # * somehow factorize this code with AxisCollection.split_axes
77457722 if axes is None :
77467723 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 )):
77487725 axes = {axes : names }
77497726 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 ):
77517728 axes = {axis : None for axis in axes }
77527729 else :
77537730 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=
92889265 if elements is not None and kwargs :
92899266 raise TypeError ("stack() accepts either keyword arguments OR a collection of elements, not both" )
92909267
9291- if isinstance (axes , basestring ) and '=' in axes :
9268+ if isinstance (axes , str ) and '=' in axes :
92929269 axes = Axis (axes )
92939270 elif isinstance (axes , Group ):
92949271 axes = Axis (axes )
92959272
9296- if axes is not None and not isinstance (axes , basestring ):
9273+ if axes is not None and not isinstance (axes , str ):
92979274 axes = AxisCollection (axes )
92989275
92999276 if kwargs :
@@ -9321,7 +9298,7 @@ def stack(elements=None, axes=None, title=None, meta=None, dtype=None, res_axes=
93219298
93229299 if all (isinstance (e , tuple ) for e in elements ):
93239300 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 ):
93259302 keys = [k for k , v in elements ]
93269303 values = [v for k , v in elements ]
93279304 # assert that all keys are indexers
@@ -9338,7 +9315,7 @@ def translate_and_sort_key(key, axes):
93389315 dict_elements = {translate_and_sort_key (key , axes ): value for key , value in elements }
93399316 items = [(k , dict_elements [k ]) for k in axes .iter_labels ()]
93409317 else :
9341- if axes is None or isinstance (axes , basestring ):
9318+ if axes is None or isinstance (axes , str ):
93429319 axes = AxisCollection (Axis (len (elements ), axes ))
93439320 else :
93449321 # TODO: add support for more than one axis here
@@ -9568,7 +9545,7 @@ def values_with_expand(value, axes, readonly=True, ascending=True):
95689545 if not isinstance (axes , (tuple , list , AxisCollection )):
95699546 axes = (axes ,)
95709547 # 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
95729549 for axis in axes ]
95739550 # transform string axes references to objects
95749551 axes = AxisCollection ([axis if isinstance (axis , Axis ) else all_axes [axis ]
0 commit comments