1515# and by Perry Greenfield 2000-4-1 for numarray
1616# and by Travis Oliphant 2005-8-22 for numpy
1717
18+
19+ # Note: Both scalartypes.c.src and arrayprint.py implement strs for numpy
20+ # scalars but for different purposes. scalartypes.c.src has str/reprs for when
21+ # the scalar is printed on its own, while arrayprint.py has strs for when
22+ # scalars are printed inside an ndarray. Only the latter strs are currently
23+ # user-customizable.
24+
1825import sys
1926import functools
2027if sys .version_info [0 ] >= 3 :
2835 except ImportError :
2936 from dummy_thread import get_ident
3037
38+ import numpy as np
3139from . import numerictypes as _nt
3240from .umath import maximum , minimum , absolute , not_equal , isnan , isinf
3341from .multiarray import (array , format_longfloat , datetime_as_string ,
3442 datetime_data , dtype )
3543from .fromnumeric import ravel
3644from .numeric import asarray
45+ import warnings
3746
3847if sys .version_info [0 ] >= 3 :
3948 _MAXINT = sys .maxsize
@@ -399,7 +408,7 @@ def wrapper(self, *args, **kwargs):
399408@_recursive_guard ()
400409def array2string (a , max_line_width = None , precision = None ,
401410 suppress_small = None , separator = ' ' , prefix = "" ,
402- style = repr , formatter = None ):
411+ style = np . _NoValue , formatter = None ):
403412 """
404413 Return a string representation of an array.
405414
@@ -425,9 +434,10 @@ def array2string(a, max_line_width=None, precision=None,
425434
426435 The length of the prefix string is used to align the
427436 output correctly.
428- style : function, optional
429- A function that accepts an ndarray and returns a string. Used only
430- when the shape of `a` is equal to ``()``, i.e. for 0-D arrays.
437+ style : _NoValue, optional
438+ Has no effect, do not use.
439+
440+ .. deprecated:: 1.14.0
431441 formatter : dict of callables, optional
432442 If not None, the keys should indicate the type(s) that the respective
433443 formatting function applies to. Callables should return a string.
@@ -494,6 +504,11 @@ def array2string(a, max_line_width=None, precision=None,
494504
495505 """
496506
507+ # Deprecation 05-16-2017 v1.14
508+ if style is not np ._NoValue :
509+ warnings .warn ("'style' argument is deprecated and no longer functional" ,
510+ DeprecationWarning , stacklevel = 3 )
511+
497512 if max_line_width is None :
498513 max_line_width = _line_width
499514
@@ -506,16 +521,7 @@ def array2string(a, max_line_width=None, precision=None,
506521 if formatter is None :
507522 formatter = _formatter
508523
509- if a .shape == ():
510- x = a .item ()
511- if a .dtype .fields is not None :
512- arr = array ([x ], dtype = a .dtype )
513- format_function = _get_format_function (
514- arr , precision , suppress_small , formatter )
515- lst = format_function (arr [0 ])
516- else :
517- lst = style (x )
518- elif functools .reduce (product , a .shape ) == 0 :
524+ if a .size == 0 :
519525 # treat as a null array if any of shape elements == 0
520526 lst = "[]"
521527 else :
@@ -542,7 +548,7 @@ def _formatArray(a, format_function, rank, max_line_len,
542548
543549 """
544550 if rank == 0 :
545- raise ValueError ( "rank shouldn't be zero." )
551+ return format_function ( a [()]) + ' \n '
546552
547553 if summary_insert and 2 * edge_items < len (a ):
548554 leading_items = edge_items
@@ -809,22 +815,21 @@ def __call__(self, x):
809815
810816class TimedeltaFormat (object ):
811817 def __init__ (self , data ):
812- if data .dtype .kind == 'm' :
813- nat_value = array (['NaT' ], dtype = data .dtype )[0 ]
814- int_dtype = dtype (data .dtype .byteorder + 'i8' )
815- int_view = data .view (int_dtype )
816- v = int_view [not_equal (int_view , nat_value .view (int_dtype ))]
817- if len (v ) > 0 :
818- # Max str length of non-NaT elements
819- max_str_len = max (len (str (maximum .reduce (v ))),
820- len (str (minimum .reduce (v ))))
821- else :
822- max_str_len = 0
823- if len (v ) < len (data ):
824- # data contains a NaT
825- max_str_len = max (max_str_len , 5 )
826- self .format = '%' + str (max_str_len ) + 'd'
827- self ._nat = "'NaT'" .rjust (max_str_len )
818+ nat_value = array (['NaT' ], dtype = data .dtype )[0 ]
819+ int_dtype = dtype (data .dtype .byteorder + 'i8' )
820+ int_view = data .view (int_dtype )
821+ v = int_view [not_equal (int_view , nat_value .view (int_dtype ))]
822+ if len (v ) > 0 :
823+ # Max str length of non-NaT elements
824+ max_str_len = max (len (str (maximum .reduce (v ))),
825+ len (str (minimum .reduce (v ))))
826+ else :
827+ max_str_len = 0
828+ if len (v ) < len (data ):
829+ # data contains a NaT
830+ max_str_len = max (max_str_len , 5 )
831+ self .format = '%' + str (max_str_len ) + 'd'
832+ self ._nat = "'NaT'" .rjust (max_str_len )
828833
829834 def __call__ (self , x ):
830835 # TODO: After NAT == NAT deprecation should be simplified:
0 commit comments