@@ -104,49 +104,15 @@ def __new__(cls, data, index=None, dtype=None, name=None, copy=False):
104
104
index = Index (sorted (data .keys ()))
105
105
data = [data .get (idx , np .nan ) for idx in index ]
106
106
107
- try :
108
- subarr = np .array (data , dtype = dtype , copy = copy )
109
- except ValueError :
110
- if dtype :
111
- raise
112
- else : # pragma: no cover
113
- subarr = np .array (data , dtype = object )
114
-
115
- if subarr .ndim == 0 :
116
- if isinstance (data , list ): # pragma: no cover
117
- subarr = np .array (data , dtype = object )
118
- elif index is not None :
119
- value = data
120
-
121
- # If we create an empty array using a string to infer
122
- # the dtype, NumPy will only allocate one character per entry
123
- # so this is kind of bad. Alternately we could use np.repeat
124
- # instead of np.empty (but then you still don't want things
125
- # coming out as np.str_!
126
- if isinstance (value , basestring ) and dtype is None :
127
- dtype = np .object_
128
-
129
- if dtype is None :
130
- subarr = np .empty (len (index ), dtype = type (value ))
131
- else :
132
- subarr = np .empty (len (index ), dtype = dtype )
133
- subarr .fill (value )
134
- else :
135
- return subarr .item ()
136
- elif subarr .ndim > 1 :
137
- if isinstance (data , np .ndarray ):
138
- raise Exception ('Data must be 1-dimensional' )
139
- else :
140
- subarr = _asarray_tuplesafe (data , dtype = dtype )
107
+ subarr = _sanitize_array (data , index , dtype , copy ,
108
+ raise_cast_failure = True )
109
+
110
+ if not isinstance (subarr , np .ndarray ):
111
+ return subarr
141
112
142
113
if index is None :
143
114
index = _default_index (len (subarr ))
144
115
145
- # This is to prevent mixed-type Series getting all casted to
146
- # NumPy string type, e.g. NaN --> '-1#IND'.
147
- if issubclass (subarr .dtype .type , basestring ):
148
- subarr = np .array (data , dtype = object , copy = copy )
149
-
150
116
# Change the class of the array to be the subclass type.
151
117
subarr = subarr .view (cls )
152
118
subarr .index = index
@@ -2001,6 +1967,50 @@ def remove_na(arr):
2001
1967
return arr [notnull (arr )]
2002
1968
2003
1969
1970
+ def _sanitize_array (data , index , dtype = None , copy = False ,
1971
+ raise_cast_failure = False ):
1972
+ try :
1973
+ subarr = np .array (data , dtype = dtype , copy = copy )
1974
+ except (ValueError , TypeError ):
1975
+ if dtype and raise_cast_failure :
1976
+ raise
1977
+ else : # pragma: no cover
1978
+ subarr = np .array (data , dtype = object )
1979
+
1980
+ if subarr .ndim == 0 :
1981
+ if isinstance (data , list ): # pragma: no cover
1982
+ subarr = np .array (data , dtype = object )
1983
+ elif index is not None :
1984
+ value = data
1985
+
1986
+ # If we create an empty array using a string to infer
1987
+ # the dtype, NumPy will only allocate one character per entry
1988
+ # so this is kind of bad. Alternately we could use np.repeat
1989
+ # instead of np.empty (but then you still don't want things
1990
+ # coming out as np.str_!
1991
+ if isinstance (value , basestring ) and dtype is None :
1992
+ dtype = np .object_
1993
+
1994
+ if dtype is None :
1995
+ subarr = np .empty (len (index ), dtype = type (value ))
1996
+ else :
1997
+ subarr = np .empty (len (index ), dtype = dtype )
1998
+ subarr .fill (value )
1999
+ else :
2000
+ return subarr .item ()
2001
+ elif subarr .ndim > 1 :
2002
+ if isinstance (data , np .ndarray ):
2003
+ raise Exception ('Data must be 1-dimensional' )
2004
+ else :
2005
+ subarr = _asarray_tuplesafe (data , dtype = dtype )
2006
+
2007
+ # This is to prevent mixed-type Series getting all casted to
2008
+ # NumPy string type, e.g. NaN --> '-1#IND'.
2009
+ if issubclass (subarr .dtype .type , basestring ):
2010
+ subarr = np .array (data , dtype = object , copy = copy )
2011
+
2012
+ return subarr
2013
+
2004
2014
def _get_rename_function (mapper ):
2005
2015
if isinstance (mapper , (dict , Series )):
2006
2016
def f (x ):
0 commit comments