@@ -76,7 +76,7 @@ def construct_from_string(cls, string):
76
76
"'{}'" .format (cls , string ))
77
77
78
78
79
- def to_integer_array (values , dtype = None ):
79
+ def integer_array (values , dtype = None , copy = False ):
80
80
"""
81
81
Infer and return an integer array of the values.
82
82
@@ -94,7 +94,8 @@ def to_integer_array(values, dtype=None):
94
94
------
95
95
TypeError if incompatible types
96
96
"""
97
- return IntegerArray (values , dtype = dtype , copy = False )
97
+ values , mask = coerce_to_array (values , dtype = dtype , copy = copy )
98
+ return IntegerArray (values , mask )
98
99
99
100
100
101
def safe_cast (values , dtype , copy ):
@@ -206,7 +207,7 @@ class IntegerArray(ExtensionArray, ExtensionOpsMixin):
206
207
def dtype (self ):
207
208
return _dtypes [str (self ._data .dtype )]
208
209
209
- def __init__ (self , values , mask = None , dtype = None , copy = False ):
210
+ def __init__ (self , values , mask , copy = False ):
210
211
"""
211
212
Parameters
212
213
----------
@@ -219,25 +220,33 @@ def __init__(self, values, mask=None, dtype=None, copy=False):
219
220
-------
220
221
IntegerArray
221
222
"""
222
- self ._data , self ._mask = coerce_to_array (
223
- values , dtype = dtype , mask = mask , copy = copy )
223
+ if not (isinstance (values , np .ndarray )
224
+ and np .issubdtype (values .dtype , np .integer )):
225
+ raise TypeError ("values should be integer numpy array" )
226
+ if not (isinstance (mask , np .ndarray ) and mask .dtype == np .bool_ ):
227
+ raise TypeError ("mask should be boolean numpy array" )
228
+
229
+ if copy :
230
+ values = values .copy ()
231
+ mask = mask .copy ()
232
+
233
+ self ._data = values
234
+ self ._mask = mask
224
235
225
236
@classmethod
226
237
def _from_sequence (cls , scalars , dtype = None , copy = False ):
227
- return cls (scalars , dtype = dtype , copy = copy )
238
+ return integer_array (scalars , dtype = dtype , copy = copy )
228
239
229
240
@classmethod
230
241
def _from_factorized (cls , values , original ):
231
- return cls (values , dtype = original .dtype )
242
+ return integer_array (values , dtype = original .dtype )
232
243
233
244
def __getitem__ (self , item ):
234
245
if is_integer (item ):
235
246
if self ._mask [item ]:
236
247
return self .dtype .na_value
237
248
return self ._data [item ]
238
- return type (self )(self ._data [item ],
239
- mask = self ._mask [item ],
240
- dtype = self .dtype )
249
+ return type (self )(self ._data [item ], self ._mask [item ])
241
250
242
251
def _coerce_to_ndarray (self ):
243
252
"""
@@ -294,7 +303,7 @@ def take(self, indexer, allow_fill=False, fill_value=None):
294
303
result [fill_mask ] = fill_value
295
304
mask = mask ^ fill_mask
296
305
297
- return type (self )(result , mask = mask , dtype = self . dtype , copy = False )
306
+ return type (self )(result , mask , copy = False )
298
307
299
308
def copy (self , deep = False ):
300
309
data , mask = self ._data , self ._mask
@@ -304,7 +313,7 @@ def copy(self, deep=False):
304
313
else :
305
314
data = data .copy ()
306
315
mask = mask .copy ()
307
- return type (self )(data , mask , dtype = self . dtype , copy = False )
316
+ return type (self )(data , mask , copy = False )
308
317
309
318
def __setitem__ (self , key , value ):
310
319
_is_scalar = is_scalar (value )
@@ -356,7 +365,7 @@ def _na_value(self):
356
365
def _concat_same_type (cls , to_concat ):
357
366
data = np .concatenate ([x ._data for x in to_concat ])
358
367
mask = np .concatenate ([x ._mask for x in to_concat ])
359
- return cls (data , mask = mask , dtype = to_concat [ 0 ]. dtype )
368
+ return cls (data , mask )
360
369
361
370
def astype (self , dtype , copy = True ):
362
371
"""Cast to a NumPy array or IntegerArray with 'dtype'.
@@ -386,8 +395,7 @@ def astype(self, dtype, copy=True):
386
395
if isinstance (dtype , _IntegerDtype ):
387
396
result = self ._data .astype (dtype .numpy_dtype ,
388
397
casting = 'same_kind' , copy = False )
389
- return type (self )(result , mask = self ._mask ,
390
- dtype = dtype , copy = False )
398
+ return type (self )(result , mask = self ._mask , copy = False )
391
399
392
400
# coerce
393
401
data = self ._coerce_to_ndarray ()
@@ -523,7 +531,7 @@ def _maybe_mask_result(self, result, mask, other, op_name):
523
531
result [mask ] = np .nan
524
532
return result
525
533
526
- return type (self )(result , mask = mask , dtype = self . dtype , copy = False )
534
+ return type (self )(result , mask , copy = False )
527
535
528
536
@classmethod
529
537
def _create_arithmetic_method (cls , op ):
0 commit comments