6
6
import warnings
7
7
8
8
from pandas ._libs import tslib , lib
9
- from pandas ._libs .tslib import iNaT
9
+ from pandas ._libs .tslib import iNaT , Timestamp
10
10
from pandas .compat import string_types , text_type , PY3
11
11
from .common import (_ensure_object , is_bool , is_integer , is_float ,
12
12
is_complex , is_datetimetz , is_categorical_dtype ,
@@ -333,6 +333,23 @@ def maybe_promote(dtype, fill_value=np.nan):
333
333
return dtype , fill_value
334
334
335
335
336
+ def infer_dtype_from (val , pandas_dtype = False ):
337
+ """
338
+ interpret the dtype from a scalar or array. This is a convenience
339
+ routines to infer dtype from a scalar or an array
340
+
341
+ Parameters
342
+ ----------
343
+ pandas_dtype : bool, default False
344
+ whether to infer dtype including pandas extension types.
345
+ If False, scalar/array belongs to pandas extension types is inferred as
346
+ object
347
+ """
348
+ if is_scalar (val ):
349
+ return infer_dtype_from_scalar (val , pandas_dtype = pandas_dtype )
350
+ return infer_dtype_from_array (val , pandas_dtype = pandas_dtype )
351
+
352
+
336
353
def infer_dtype_from_scalar (val , pandas_dtype = False ):
337
354
"""
338
355
interpret the dtype from a scalar
@@ -408,24 +425,32 @@ def infer_dtype_from_scalar(val, pandas_dtype=False):
408
425
return dtype , val
409
426
410
427
411
- def infer_dtype_from_array (arr ):
428
+ def infer_dtype_from_array (arr , pandas_dtype = False ):
412
429
"""
413
430
infer the dtype from a scalar or array
414
431
415
432
Parameters
416
433
----------
417
434
arr : scalar or array
435
+ pandas_dtype : bool, default False
436
+ whether to infer dtype including pandas extension types.
437
+ If False, array belongs to pandas extension types
438
+ is inferred as object
418
439
419
440
Returns
420
441
-------
421
- tuple (numpy-compat dtype, array)
442
+ tuple (numpy-compat/pandas-compat dtype, array)
422
443
423
444
Notes
424
445
-----
425
- These infer to numpy dtypes exactly
426
- with the exception that mixed / object dtypes
446
+
447
+ if pandas_dtype=False. these infer to numpy dtypes
448
+ exactly with the exception that mixed / object dtypes
427
449
are not coerced by stringifying or conversion
428
450
451
+ if pandas_dtype=True. datetime64tz-aware/categorical
452
+ types will retain there character.
453
+
429
454
Examples
430
455
--------
431
456
>>> np.asarray([1, '1'])
@@ -442,6 +467,10 @@ def infer_dtype_from_array(arr):
442
467
if not is_list_like (arr ):
443
468
arr = [arr ]
444
469
470
+ if pandas_dtype and (is_categorical_dtype (arr ) or
471
+ is_datetime64tz_dtype (arr )):
472
+ return arr .dtype , arr
473
+
445
474
# don't force numpy coerce with nan's
446
475
inferred = lib .infer_dtype (arr )
447
476
if inferred in ['string' , 'bytes' , 'unicode' ,
@@ -1028,13 +1057,25 @@ def find_common_type(types):
1028
1057
return np .find_common_type (types , [])
1029
1058
1030
1059
1031
- def _cast_scalar_to_array (shape , value , dtype = None ):
1060
+ def cast_scalar_to_array (shape , value , dtype = None ):
1032
1061
"""
1033
1062
create np.ndarray of specified shape and dtype, filled with values
1063
+
1064
+ Parameters
1065
+ ----------
1066
+ shape : tuple
1067
+ value : scalar value
1068
+ dtype : np.dtype, optional
1069
+ dtype to coerce
1070
+
1071
+ Returns
1072
+ -------
1073
+ ndarray of shape, filled with value, of specified / inferred dtype
1074
+
1034
1075
"""
1035
1076
1036
1077
if dtype is None :
1037
- dtype , fill_value = _infer_dtype_from_scalar (value )
1078
+ dtype , fill_value = infer_dtype_from_scalar (value )
1038
1079
else :
1039
1080
fill_value = value
1040
1081
0 commit comments