@@ -41,19 +41,15 @@ def wrapper(self, other):
41
41
if self .index .equals (other .index ):
42
42
return Series (op (self .values , other .values ), index = self .index )
43
43
44
- newIndex = self .index + other .index
45
-
46
- try :
47
- this_reindexed = self .reindex (newIndex )
48
- other_reindexed = other .reindex (newIndex )
49
- arr = op (this_reindexed .values , other_reindexed .values )
50
- except Exception :
51
- arr = Series .combine (self , other , getattr (type (self [0 ]), name ))
52
- result = Series (arr , index = newIndex )
53
- return result
44
+ new_index = self .index + other .index
45
+ this_reindexed = self .reindex (new_index )
46
+ other_reindexed = other .reindex (new_index )
47
+ arr = op (this_reindexed .values , other_reindexed .values )
48
+ return Series (arr , index = new_index )
54
49
elif isinstance (other , DataFrame ):
55
50
return NotImplemented
56
51
else :
52
+ # scalars
57
53
return Series (op (self .values , other ), index = self .index )
58
54
return wrapper
59
55
@@ -84,7 +80,7 @@ def f(self, other, fill_value=None):
84
80
85
81
class Series (np .ndarray , PandasObject ):
86
82
"""
87
- Generic indexed (labeled) vector ( time series or cross-section)
83
+ Generic indexed (labeled) vector, including time series
88
84
89
85
Contains values in a numpy-ndarray with an optional bound index
90
86
(also an array of dates, strings, or whatever you want the 'row
@@ -124,7 +120,7 @@ class Series(np.ndarray, PandasObject):
124
120
125
121
_AXIS_NAMES = dict ((v , k ) for k , v in _AXIS_NUMBERS .iteritems ())
126
122
127
- def __new__ (cls , data , index = None , dtype = None , copy = False ):
123
+ def __new__ (cls , data , index = None , dtype = None , name = None , copy = False ):
128
124
if isinstance (data , Series ):
129
125
if index is None :
130
126
index = data .index
@@ -178,6 +174,7 @@ def __new__(cls, data, index=None, dtype=None, copy=False):
178
174
# Change the class of the array to be the subclass type.
179
175
subarr = subarr .view (cls )
180
176
subarr .index = index
177
+ subarr .name = name
181
178
182
179
if subarr .index .is_all_dates ():
183
180
subarr = subarr .view (TimeSeries )
@@ -271,12 +268,7 @@ def __getitem__(self, key):
271
268
except TypeError :
272
269
pass
273
270
274
- # boolean indexing, need to check that the data are aligned, otherwise
275
- # disallowed
276
- if isinstance (key , Series ) and key .dtype == np .bool_ :
277
- if not key .index .equals (self .index ):
278
- raise Exception ('can only boolean index with like-indexed '
279
- 'Series or raw ndarrays' )
271
+ self ._check_bool_indexer (key )
280
272
281
273
def _index_with (indexer ):
282
274
return Series (self .values [indexer ],
@@ -338,7 +330,7 @@ def get(self, key, default=None):
338
330
y : scalar
339
331
"""
340
332
if key in self .index :
341
- return self ._get_val_at (self .index .indexMap [ key ] )
333
+ return self ._get_val_at (self .index .get_loc ( key ) )
342
334
else :
343
335
return default
344
336
@@ -361,7 +353,7 @@ def __getslice__(self, i, j):
361
353
def __setitem__ (self , key , value ):
362
354
values = self .values
363
355
try :
364
- loc = self .index .indexMap [ key ]
356
+ loc = self .index .get_loc ( key )
365
357
values [loc ] = value
366
358
return
367
359
except KeyError :
@@ -373,12 +365,7 @@ def __setitem__(self, key, value):
373
365
# Could not hash item
374
366
pass
375
367
376
- # boolean indexing, need to check that the data are aligned, otherwise
377
- # disallowed
378
- if isinstance (key , Series ) and key .dtype == np .bool_ :
379
- if not key .index .equals (self .index ):
380
- raise Exception ('can only boolean index with like-indexed '
381
- 'Series or raw ndarrays' )
368
+ self ._check_bool_indexer (key )
382
369
383
370
# special handling of boolean data with NAs stored in object
384
371
# arrays. Sort of an elaborate hack since we can't represent boolean
@@ -396,6 +383,14 @@ def __setitem__(self, key, value):
396
383
397
384
values [key ] = value
398
385
386
+ def _check_bool_indexer (self , key ):
387
+ # boolean indexing, need to check that the data are aligned, otherwise
388
+ # disallowed
389
+ if isinstance (key , Series ) and key .dtype == np .bool_ :
390
+ if not key .index .equals (self .index ):
391
+ raise Exception ('can only boolean index with like-indexed '
392
+ 'Series or raw ndarrays' )
393
+
399
394
def __setslice__ (self , i , j , value ):
400
395
"""Set slice equal to given value(s)"""
401
396
ndarray .__setslice__ (self , i , j , value )
@@ -743,12 +738,12 @@ def append(self, other):
743
738
-------
744
739
y : Series
745
740
"""
746
- newIndex = np .concatenate ((self .index , other .index ))
747
- newIndex = Index (newIndex )
748
- newIndex ._verify_integrity ()
741
+ new_index = np .concatenate ((self .index , other .index ))
742
+ new_index = Index (new_index )
743
+ new_index ._verify_integrity ()
749
744
750
745
new_values = np .concatenate ((self , other ))
751
- return Series (new_values , index = newIndex )
746
+ return Series (new_values , index = new_index )
752
747
753
748
def _binop (self , other , func , fill_value = None ):
754
749
"""
@@ -811,17 +806,17 @@ def combine(self, other, func, fill_value=nan):
811
806
result : Series
812
807
"""
813
808
if isinstance (other , Series ):
814
- newIndex = self .index + other .index
809
+ new_index = self .index + other .index
815
810
816
- new_values = np .empty (len (newIndex ), dtype = self .dtype )
817
- for i , idx in enumerate (newIndex ):
811
+ new_values = np .empty (len (new_index ), dtype = self .dtype )
812
+ for i , idx in enumerate (new_index ):
818
813
new_values [i ] = func (self .get (idx , fill_value ),
819
814
other .get (idx , fill_value ))
820
815
else :
821
- newIndex = self .index
816
+ new_index = self .index
822
817
new_values = func (self .values , other )
823
818
824
- return Series (new_values , index = newIndex )
819
+ return Series (new_values , index = new_index )
825
820
826
821
def combineFirst (self , other ):
827
822
"""
@@ -837,16 +832,16 @@ def combineFirst(self, other):
837
832
formed as union of two Series
838
833
"""
839
834
if self .index .equals (other .index ):
840
- newIndex = self .index
835
+ new_index = self .index
841
836
# save ourselves the copying in this case
842
837
this = self
843
838
else :
844
- newIndex = self .index + other .index
839
+ new_index = self .index + other .index
845
840
846
- this = self .reindex (newIndex )
847
- other = other .reindex (newIndex )
841
+ this = self .reindex (new_index )
842
+ other = other .reindex (new_index )
848
843
849
- result = Series (np .where (isnull (this ), other , this ), index = newIndex )
844
+ result = Series (np .where (isnull (this ), other , this ), index = new_index )
850
845
return result
851
846
852
847
#----------------------------------------------------------------------
@@ -896,7 +891,9 @@ def _try_mergesort(arr):
896
891
# stable sort not available for object dtype
897
892
return arr .argsort ()
898
893
899
- if 'missingAtEnd' in kwds :
894
+ if 'missingAtEnd' in kwds : # pragma: no cover
895
+ warnings .warn ("missingAtEnd is deprecated, use na_last" ,
896
+ FutureWarning )
900
897
na_last = kwds ['missingAtEnd' ]
901
898
902
899
arr = self .values
0 commit comments