15
15
import numpy .ma as ma
16
16
17
17
from pandas .core .common import (isnull , notnull , _is_bool_indexer ,
18
- _default_index , _maybe_promote ,
18
+ _default_index , _maybe_promote , _maybe_upcast ,
19
19
_asarray_tuplesafe , is_integer_dtype ,
20
20
_infer_dtype_from_scalar )
21
21
from pandas .core .index import (Index , MultiIndex , InvalidIndexError ,
@@ -88,12 +88,15 @@ def wrapper(self, other):
88
88
89
89
# rhs is either a timedelta or a series/ndarray
90
90
if lib .is_timedelta_array (rvalues ):
91
- rvalues = pa .array ([ np .timedelta64 (v ) for v in rvalues ],dtype = 'timedelta64[ns]' )
91
+ rvalues = pa .array ([np .timedelta64 (v ) for v in rvalues ],
92
+ dtype = 'timedelta64[ns]' )
92
93
dtype = 'M8[ns]'
93
94
elif com .is_datetime64_dtype (rvalues ):
94
95
dtype = 'timedelta64[ns]'
95
96
else :
96
- raise ValueError ("cannot operate on a series with out a rhs of a series/ndarray of type datetime64[ns] or a timedelta" )
97
+ raise ValueError ('cannot operate on a series with out a rhs '
98
+ 'of a series/ndarray of type datetime64[ns] '
99
+ 'or a timedelta' )
97
100
98
101
lvalues = lvalues .view ('i8' )
99
102
rvalues = rvalues .view ('i8' )
@@ -430,32 +433,32 @@ def from_array(cls, arr, index=None, name=None, copy=False):
430
433
431
434
def __init__ (self , data = None , index = None , dtype = None , name = None ,
432
435
copy = False ):
433
- """One-dimensional ndarray with axis labels (including time
434
- series). Labels need not be unique but must be any hashable type. The object
435
- supports both integer- and label-based indexing and provides a host of methods
436
- for performing operations involving the index. Statistical methods from ndarray
437
- have been overridden to automatically exclude missing data (currently
438
- represented as NaN)
439
-
440
- Operations between Series (+, -, /, *, **) align values based on their
441
- associated index values-- they need not be the same length. The result
442
- index will be the sorted union of the two indexes.
443
-
444
- Parameters
445
- ----------
446
- data : array-like, dict, or scalar value
447
- Contains data stored in Series
448
- index : array-like or Index (1d)
436
+ """
437
+ One-dimensional ndarray with axis labels (including time series).
438
+ Labels need not be unique but must be any hashable type. The object
439
+ supports both integer- and label-based indexing and provides a host of
440
+ methods for performing operations involving the index. Statistical
441
+ methods from ndarray have been overridden to automatically exclude
442
+ missing data (currently represented as NaN)
449
443
450
- Values must be unique and hashable, same length as data. Index object
451
- (or other iterable of same length as data) Will default to
452
- np.arange(len(data)) if not provided. If both a dict and index sequence
453
- are used, the index will override the keys found in the dict.
444
+ Operations between Series (+, -, /, *, **) align values based on their
445
+ associated index values-- they need not be the same length. The result
446
+ index will be the sorted union of the two indexes.
454
447
455
- dtype : numpy.dtype or None
456
- If None, dtype will be inferred copy : boolean, default False Copy
457
- input data
458
- copy : boolean, default False
448
+ Parameters
449
+ ----------
450
+ data : array-like, dict, or scalar value
451
+ Contains data stored in Series
452
+ index : array-like or Index (1d)
453
+ Values must be unique and hashable, same length as data. Index
454
+ object (or other iterable of same length as data) Will default to
455
+ np.arange(len(data)) if not provided. If both a dict and index
456
+ sequence are used, the index will override the keys found in the
457
+ dict.
458
+ dtype : numpy.dtype or None
459
+ If None, dtype will be inferred copy : boolean, default False Copy
460
+ input data
461
+ copy : boolean, default False
459
462
"""
460
463
pass
461
464
@@ -769,7 +772,8 @@ def astype(self, dtype):
769
772
See numpy.ndarray.astype
770
773
"""
771
774
casted = com ._astype_nansafe (self .values , dtype )
772
- return self ._constructor (casted , index = self .index , name = self .name , dtype = casted .dtype )
775
+ return self ._constructor (casted , index = self .index , name = self .name ,
776
+ dtype = casted .dtype )
773
777
774
778
def convert_objects (self , convert_dates = True , convert_numeric = True ):
775
779
"""
@@ -778,8 +782,12 @@ def convert_objects(self, convert_dates=True, convert_numeric=True):
778
782
779
783
Parameters
780
784
----------
781
- convert_dates : if True, attempt to soft convert_dates, if 'coerce', force conversion (and non-convertibles get NaT)
782
- convert_numeric : if True attempt to coerce to numerbers (including strings), non-convertibles get NaN
785
+ convert_dates : boolean, default True
786
+ if True, attempt to soft convert_dates, if 'coerce', force
787
+ conversion (and non-convertibles get NaT)
788
+ convert_numeric : boolean, default True
789
+ if True attempt to coerce to numbers (including strings),
790
+ non-convertibles get NaN
783
791
784
792
Returns
785
793
-------
@@ -982,7 +990,8 @@ def __unicode__(self):
982
990
"""
983
991
Return a string representation for a particular DataFrame
984
992
985
- Invoked by unicode(df) in py2 only. Yields a Unicode String in both py2/py3.
993
+ Invoked by unicode(df) in py2 only. Yields a Unicode String in both
994
+ py2/py3.
986
995
"""
987
996
width , height = get_terminal_size ()
988
997
max_rows = (height if get_option ("display.max_rows" ) == 0
@@ -2416,7 +2425,7 @@ def reindex_axis(self, labels, axis=0, **kwargs):
2416
2425
raise ValueError ("cannot reindex series on non-zero axis!" )
2417
2426
return self .reindex (index = labels ,** kwargs )
2418
2427
2419
- def reindex_like (self , other , method = None , limit = None ):
2428
+ def reindex_like (self , other , method = None , limit = None , fill_value = pa . NA ):
2420
2429
"""
2421
2430
Reindex Series to match index of another Series, optionally with
2422
2431
filling logic
@@ -2437,7 +2446,8 @@ def reindex_like(self, other, method=None, limit=None):
2437
2446
-------
2438
2447
reindexed : Series
2439
2448
"""
2440
- return self .reindex (other .index , method = method , limit = limit )
2449
+ return self .reindex (other .index , method = method , limit = limit ,
2450
+ fill_value = fill_value )
2441
2451
2442
2452
def take (self , indices , axis = 0 ):
2443
2453
"""
@@ -3060,10 +3070,14 @@ def remove_na(arr):
3060
3070
3061
3071
def _sanitize_array (data , index , dtype = None , copy = False ,
3062
3072
raise_cast_failure = False ):
3073
+
3063
3074
if isinstance (data , ma .MaskedArray ):
3064
3075
mask = ma .getmaskarray (data )
3065
- data = ma .copy (data )
3066
- data [mask ] = pa .NA
3076
+ if mask .any ():
3077
+ data , fill_value = _maybe_upcast (data , copy = True )
3078
+ data [mask ] = fill_value
3079
+ else :
3080
+ data = data .copy ()
3067
3081
3068
3082
def _try_cast (arr ):
3069
3083
try :
@@ -3112,7 +3126,7 @@ def _try_cast(arr):
3112
3126
raise
3113
3127
subarr = pa .array (data , dtype = object , copy = copy )
3114
3128
subarr = lib .maybe_convert_objects (subarr )
3115
-
3129
+
3116
3130
else :
3117
3131
subarr = com ._possibly_convert_platform (data )
3118
3132
0 commit comments