@@ -186,6 +186,10 @@ def __init__(self, data, index=None, dtype=None, name=None, copy=False):
186
186
"""
187
187
pass
188
188
189
+ @property
190
+ def _constructor (self ):
191
+ return Series
192
+
189
193
def __hash__ (self ):
190
194
raise TypeError ('unhashable type' )
191
195
@@ -304,7 +308,8 @@ def _multilevel_index(self, key):
304
308
_get_val_at = ndarray .__getitem__
305
309
306
310
def __getslice__ (self , i , j ):
307
- return Series (self .values [i :j ], index = self .index [i :j ], name = self .name )
311
+ return self ._constructor (self .values [i :j ], index = self .index [i :j ],
312
+ name = self .name )
308
313
309
314
def __setitem__ (self , key , value ):
310
315
values = self .values
@@ -354,24 +359,52 @@ def __setslice__(self, i, j, value):
354
359
def __repr__ (self ):
355
360
"""Clean string representation of a Series"""
356
361
if len (self .index ) > 500 :
357
- return self ._make_repr ( 50 )
362
+ return self ._tidy_repr ( 30 )
358
363
elif len (self .index ) > 0 :
359
- return _seriesRepr ( self .index , self . values )
364
+ return self ._get_repr ( name = True )
360
365
else :
361
366
return '%s' % ndarray .__repr__ (self )
362
367
363
- def _make_repr (self , max_vals = 50 ):
368
+ def _tidy_repr (self , max_vals = 20 ):
369
+ num = max_vals // 2
370
+ head = self [:num ]._get_repr (name = False )
371
+ tail = self [- (max_vals - num ):]._get_repr (name = False )
372
+ result = head + '\n ...\n ' + tail
373
+ result = '%s\n Name: %s, Length: %d' % (result , self .name , len (self ))
374
+ return result
375
+
376
+ def to_string (self , buffer = sys .stdout , nanRep = 'NaN' ):
377
+ print >> buffer , self ._get_repr (nanRep = nanRep )
378
+
379
+ def _get_repr (self , name = False , nanRep = 'NaN' ):
364
380
vals = self .values
365
381
index = self .index
366
382
367
- num = max_vals // 2
368
- head = _seriesRepr (index [:num ], vals [:num ])
369
- tail = _seriesRepr (index [- (max_vals - num ):], vals [- (max_vals - num ):])
370
- return head + '\n ...\n ' + tail + '\n length: %d' % len (vals )
383
+ string_index = index .format ()
384
+ maxlen = max (len (x ) for x in string_index )
385
+ padSpace = min (maxlen , 60 )
371
386
372
- def toString (self , buffer = sys .stdout , nanRep = 'NaN' ):
373
- print >> buffer , _seriesRepr (self .index , self .values ,
374
- nanRep = nanRep )
387
+ def _format_float (k , v ):
388
+ if np .isnan (v ):
389
+ v = nanRep
390
+ else :
391
+ v = str (v )
392
+ return '%s %s' % (str (k ).ljust (padSpace ), v )
393
+
394
+ def _format_nonfloat (k , v ):
395
+ return '%s %s' % (str (k ).ljust (padSpace ), v )
396
+
397
+ if vals .dtype == np .float_ :
398
+ _format = _format_float
399
+ else :
400
+ _format = _format_nonfloat
401
+
402
+ it = itertools .starmap (_format ,
403
+ itertools .izip (string_index , vals ))
404
+ it = list (it )
405
+ if name :
406
+ it .append ('Name: %s, Length: %d' % (str (self .name ), len (self )))
407
+ return '\n ' .join (it )
375
408
376
409
def __str__ (self ):
377
410
return repr (self )
@@ -462,7 +495,8 @@ def to_sparse(self, kind='block', fill_value=None):
462
495
sp : SparseSeries
463
496
"""
464
497
from pandas .core .sparse import SparseSeries
465
- return SparseSeries (self , kind = kind , fill_value = fill_value )
498
+ return SparseSeries (self , kind = kind , fill_value = fill_value ,
499
+ name = self .name )
466
500
467
501
def get (self , key , default = None ):
468
502
"""
@@ -946,9 +980,9 @@ def append(self, other):
946
980
new_index = self .index .append (other .index )
947
981
new_index ._verify_integrity ()
948
982
949
- new_values = np .concatenate ((self , other ))
983
+ new_values = np .concatenate ((self . values , other . values ))
950
984
name = _maybe_match_name (self , other )
951
- return Series (new_values , index = new_index , name = name )
985
+ return self . _constructor (new_values , index = new_index , name = name )
952
986
953
987
def _binop (self , other , func , fill_value = None ):
954
988
"""
@@ -1804,6 +1838,7 @@ def fromValue(cls, value=nan, index=None, dtype=None): # pragma: no cover
1804
1838
1805
1839
asOf = deprecate ('asOf' , asof )
1806
1840
toDict = deprecate ('toDict' , to_dict )
1841
+ toString = deprecate ('toString' , to_string )
1807
1842
merge = deprecate ('merge' , map )
1808
1843
applymap = deprecate ('applymap' , apply )
1809
1844
combineFirst = deprecate ('combineFirst' , combine_first )
@@ -1822,28 +1857,3 @@ def remove_na(arr):
1822
1857
Return array containing only true/non-NaN values, possibly empty.
1823
1858
"""
1824
1859
return arr [notnull (arr )]
1825
-
1826
- def _seriesRepr (index , vals , nanRep = 'NaN' ):
1827
- string_index = index .format ()
1828
- maxlen = max (len (x ) for x in string_index )
1829
- padSpace = min (maxlen , 60 )
1830
-
1831
- if vals .dtype == np .object_ :
1832
- def _format (k , v ):
1833
- return '%s %s' % (str (k ).ljust (padSpace ), v )
1834
- elif vals .dtype == np .float_ :
1835
- def _format (k , v ):
1836
- if np .isnan (v ):
1837
- v = nanRep
1838
- else :
1839
- v = str (v )
1840
-
1841
- return '%s %s' % (str (k ).ljust (padSpace ), v )
1842
- else :
1843
- def _format (k , v ):
1844
- return '%s %s' % (str (k ).ljust (padSpace ), v )
1845
-
1846
- it = itertools .starmap (_format ,
1847
- itertools .izip (string_index , vals ))
1848
-
1849
- return '\n ' .join (it )
0 commit comments