@@ -273,7 +273,7 @@ def maybe_promote(dtype, fill_value=np.nan):
273
273
else :
274
274
if issubclass (dtype .type , np .datetime64 ):
275
275
try :
276
- fill_value = lib .Timestamp (fill_value ).value
276
+ fill_value = tslib .Timestamp (fill_value ).value
277
277
except :
278
278
# the proper thing to do here would probably be to upcast
279
279
# to object (but numpy 1.6.1 doesn't do this properly)
@@ -334,6 +334,23 @@ def maybe_promote(dtype, fill_value=np.nan):
334
334
return dtype , fill_value
335
335
336
336
337
+ def infer_dtype_from (val , pandas_dtype = False ):
338
+ """
339
+ interpret the dtype from a scalar or array. This is a convenience
340
+ routines to infer dtype from a scalar or an array
341
+
342
+ Parameters
343
+ ----------
344
+ pandas_dtype : bool, default False
345
+ whether to infer dtype including pandas extension types.
346
+ If False, scalar/array belongs to pandas extension types is inferred as
347
+ object
348
+ """
349
+ if is_scalar (val ):
350
+ return infer_dtype_from_scalar (val , pandas_dtype = pandas_dtype )
351
+ return infer_dtype_from_array (val , pandas_dtype = pandas_dtype )
352
+
353
+
337
354
def infer_dtype_from_scalar (val , pandas_dtype = False ):
338
355
"""
339
356
interpret the dtype from a scalar
@@ -350,9 +367,9 @@ def infer_dtype_from_scalar(val, pandas_dtype=False):
350
367
351
368
# a 1-element ndarray
352
369
if isinstance (val , np .ndarray ):
370
+ msg = "invalid ndarray passed to _infer_dtype_from_scalar"
353
371
if val .ndim != 0 :
354
- raise ValueError (
355
- "invalid ndarray passed to _infer_dtype_from_scalar" )
372
+ raise ValueError (msg )
356
373
357
374
dtype = val .dtype
358
375
val = val .item ()
@@ -409,24 +426,31 @@ def infer_dtype_from_scalar(val, pandas_dtype=False):
409
426
return dtype , val
410
427
411
428
412
- def infer_dtype_from_array (arr ):
429
+ def infer_dtype_from_array (arr , pandas_dtype = False ):
413
430
"""
414
431
infer the dtype from a scalar or array
415
432
416
433
Parameters
417
434
----------
418
435
arr : scalar or array
436
+ pandas_dtype : bool, default False
437
+ whether to infer dtype including pandas extension types.
438
+ If False, array belongs to pandas extension types
439
+ is inferred as object
419
440
420
441
Returns
421
442
-------
422
- tuple (numpy-compat dtype, array)
443
+ tuple (numpy-compat/pandas-compat dtype, array)
423
444
424
445
Notes
425
446
-----
426
- These infer to numpy dtypes exactly
427
- with the exception that mixed / object dtypes
447
+ if pandas_dtype=False. these infer to numpy dtypes
448
+ exactly with the exception that mixed / object dtypes
428
449
are not coerced by stringifying or conversion
429
450
451
+ if pandas_dtype=True. datetime64tz-aware/categorical
452
+ types will retain there character.
453
+
430
454
Examples
431
455
--------
432
456
>>> np.asarray([1, '1'])
@@ -443,6 +467,12 @@ def infer_dtype_from_array(arr):
443
467
if not is_list_like (arr ):
444
468
arr = [arr ]
445
469
470
+ if pandas_dtype and is_extension_type (arr ):
471
+ return arr .dtype , arr
472
+
473
+ elif isinstance (arr , ABCSeries ):
474
+ return arr .dtype , np .asarray (arr )
475
+
446
476
# don't force numpy coerce with nan's
447
477
inferred = lib .infer_dtype (arr )
448
478
if inferred in ['string' , 'bytes' , 'unicode' ,
@@ -553,7 +583,7 @@ def conv(r, dtype):
553
583
if isnull (r ):
554
584
pass
555
585
elif dtype == _NS_DTYPE :
556
- r = lib .Timestamp (r )
586
+ r = tslib .Timestamp (r )
557
587
elif dtype == _TD_DTYPE :
558
588
r = _coerce_scalar_to_timedelta_type (r )
559
589
elif dtype == np .bool_ :
@@ -1027,3 +1057,31 @@ def find_common_type(types):
1027
1057
return np .object
1028
1058
1029
1059
return np .find_common_type (types , [])
1060
+
1061
+
1062
+ def cast_scalar_to_array (shape , value , dtype = None ):
1063
+ """
1064
+ create np.ndarray of specified shape and dtype, filled with values
1065
+
1066
+ Parameters
1067
+ ----------
1068
+ shape : tuple
1069
+ value : scalar value
1070
+ dtype : np.dtype, optional
1071
+ dtype to coerce
1072
+
1073
+ Returns
1074
+ -------
1075
+ ndarray of shape, filled with value, of specified / inferred dtype
1076
+
1077
+ """
1078
+
1079
+ if dtype is None :
1080
+ dtype , fill_value = infer_dtype_from_scalar (value )
1081
+ else :
1082
+ fill_value = value
1083
+
1084
+ values = np .empty (shape , dtype = dtype )
1085
+ values .fill (fill_value )
1086
+
1087
+ return values
0 commit comments