Skip to content

Commit 43a0102

Browse files
committed
CLN: moved some functionality from series._sanitize to com._dtype_from_scalar
1 parent 6cdea33 commit 43a0102

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

pandas/core/common.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from pandas.util.py3compat import StringIO, BytesIO
2525

2626
from pandas.core.config import get_option
27+
from pandas.core import array as pa
2728

2829
# XXX: HACK for NumPy 1.5.1 to suppress warnings
2930
try:
@@ -645,7 +646,21 @@ def take_fast(arr, indexer, mask, needs_masking, axis=0, out=None,
645646

646647
def _dtype_from_scalar(val):
647648
""" interpret the dtype from a scalar, upcast floats and ints """
648-
if isinstance(val, np.datetime64):
649+
650+
# a 1-element ndarray
651+
if isinstance(val, pa.Array):
652+
return val.item(), val.dtype
653+
654+
elif isinstance(val, basestring):
655+
656+
# If we create an empty array using a string to infer
657+
# the dtype, NumPy will only allocate one character per entry
658+
# so this is kind of bad. Alternately we could use np.repeat
659+
# instead of np.empty (but then you still don't want things
660+
# coming out as np.str_!
661+
return val, np.object_
662+
663+
elif isinstance(val, np.datetime64):
649664
# ugly hacklet
650665
val = lib.Timestamp(val).value
651666
return val, np.dtype('M8[ns]')

pandas/core/series.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3127,29 +3127,16 @@ def _try_cast(arr):
31273127
elif index is not None:
31283128
value = data
31293129

3130-
# If we create an empty array using a string to infer
3131-
# the dtype, NumPy will only allocate one character per entry
3132-
# so this is kind of bad. Alternately we could use np.repeat
3133-
# instead of np.empty (but then you still don't want things
3134-
# coming out as np.str_!
3135-
if isinstance(value, basestring) and dtype is None:
3136-
dtype = np.object_
3137-
3130+
# figure out the dtype from the value (upcast if necessary)
31383131
if dtype is None:
3139-
3140-
# a 1-element ndarray
3141-
if isinstance(value, pa.Array):
3142-
dtype = value.dtype
3143-
value = value.item()
3144-
else:
3145-
value, dtype = com._dtype_from_scalar(value)
3146-
3147-
subarr = pa.empty(len(index), dtype=dtype)
3132+
value, dtype = com._dtype_from_scalar(value)
31483133
else:
31493134
# need to possibly convert the value here
31503135
value = com._possibly_cast_to_datetime(value, dtype)
3151-
subarr = pa.empty(len(index), dtype=dtype)
3136+
3137+
subarr = pa.empty(len(index), dtype=dtype)
31523138
subarr.fill(value)
3139+
31533140
else:
31543141
return subarr.item()
31553142

0 commit comments

Comments
 (0)