Skip to content

Commit 0e7c20e

Browse files
committed
CLN: in common.py merged _dtype_from_scalar and _infer_dtype
yield _infer_dtype_from_scalar
1 parent ac3cdab commit 0e7c20e

File tree

5 files changed

+26
-30
lines changed

5 files changed

+26
-30
lines changed

pandas/core/common.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -644,11 +644,15 @@ def take_fast(arr, indexer, mask, needs_masking, axis=0, out=None,
644644
take_f(arr, indexer, out=out, fill_value=fill_value)
645645
return out
646646

647-
def _dtype_from_scalar(val):
648-
""" interpret the dtype from a scalar, upcast floats and ints """
647+
def _infer_dtype_from_scalar(val):
648+
""" interpret the dtype from a scalar, upcast floats and ints
649+
return the new value and the dtype """
649650

650651
# a 1-element ndarray
651652
if isinstance(val, pa.Array):
653+
if val.ndim != 0:
654+
raise ValueError("invalid ndarray passed to _dtype_from_scalar")
655+
652656
return val.item(), val.dtype
653657

654658
elif isinstance(val, basestring):
@@ -665,14 +669,19 @@ def _dtype_from_scalar(val):
665669
val = lib.Timestamp(val).value
666670
return val, np.dtype('M8[ns]')
667671

672+
elif is_bool(val):
673+
return val, np.bool_
674+
668675
# provide implicity upcast on scalars
669676
elif is_integer(val):
670-
if not is_bool(val):
671677
return val, np.int64
672678
elif is_float(val):
673679
return val, np.float64
674680

675-
return val, type(val)
681+
elif is_complex(val):
682+
return val, np.complex_
683+
684+
return val, np.object_
676685

677686
def _maybe_promote(dtype, fill_value=np.nan):
678687
if issubclass(dtype.type, np.datetime64):
@@ -918,20 +927,6 @@ def _possibly_cast_to_datetime(value, dtype, coerce = False):
918927
return value
919928

920929

921-
def _infer_dtype(value):
922-
# provide upcasting here for floats/ints
923-
if isinstance(value, (float, np.floating)):
924-
return np.float64
925-
elif isinstance(value, (bool, np.bool_)):
926-
return np.bool_
927-
elif isinstance(value, (int, long, np.integer)):
928-
return np.int64
929-
elif isinstance(value, (complex, np.complexfloating)):
930-
return np.complex_
931-
else:
932-
return np.object_
933-
934-
935930
def _possibly_cast_item(obj, item, dtype):
936931
chunk = obj[item]
937932

pandas/core/frame.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import numpy.ma as ma
2424

2525
from pandas.core.common import (isnull, notnull, PandasError, _try_sort,
26-
_default_index, _is_sequence, _dtype_from_scalar)
26+
_default_index, _is_sequence, _infer_dtype_from_scalar)
2727
from pandas.core.generic import NDFrame
2828
from pandas.core.index import Index, MultiIndex, _ensure_index
2929
from pandas.core.indexing import (_NDFrameIndexer, _maybe_droplevels,
@@ -437,7 +437,7 @@ def __init__(self, data=None, index=None, columns=None, dtype=None,
437437
if isinstance(data, basestring) and dtype is None:
438438
dtype = np.object_
439439
if dtype is None:
440-
data, dtype = _dtype_from_scalar(data)
440+
data, dtype = _infer_dtype_from_scalar(data)
441441

442442
values = np.empty((len(index), len(columns)), dtype=dtype)
443443
values.fill(data)
@@ -1878,7 +1878,7 @@ def set_value(self, index, col, value):
18781878
new_index, new_columns = self._expand_axes((index, col))
18791879
result = self.reindex(index=new_index, columns=new_columns,
18801880
copy=False)
1881-
likely_dtype = com._infer_dtype(value)
1881+
value, likely_dtype = _infer_dtype_from_scalar(value)
18821882

18831883
made_bigger = not np.array_equal(new_columns, self.columns)
18841884

@@ -2208,7 +2208,7 @@ def _sanitize_column(self, key, value):
22082208
existing_piece = self[key]
22092209

22102210
# upcast the scalar
2211-
value, dtype = _dtype_from_scalar(value)
2211+
value, dtype = _infer_dtype_from_scalar(value)
22122212

22132213
# transpose hack
22142214
if isinstance(existing_piece, DataFrame):
@@ -2226,7 +2226,7 @@ def _sanitize_column(self, key, value):
22262226

22272227
else:
22282228
# upcast the scalar
2229-
value, dtype = _dtype_from_scalar(value)
2229+
value, dtype = _infer_dtype_from_scalar(value)
22302230
value = np.array(np.repeat(value, len(self.index)), dtype=dtype)
22312231

22322232
value = com._possibly_cast_to_datetime(value, dtype)

pandas/core/internals.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1412,7 +1412,7 @@ def _make_na_block(self, items, ref_items, fill_value=np.nan):
14121412
block_shape = list(self.shape)
14131413
block_shape[0] = len(items)
14141414

1415-
dtype = com._infer_dtype(fill_value)
1415+
fill_value, dtype = com._infer_dtype_from_scalar(fill_value)
14161416
block_values = np.empty(block_shape, dtype=dtype)
14171417
block_values.fill(fill_value)
14181418
na_block = make_block(block_values, items, ref_items)

pandas/core/panel.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import sys
88
import numpy as np
99
from pandas.core.common import (PandasError, _mut_exclusive,
10-
_try_sort, _default_index, _infer_dtype,
10+
_try_sort, _default_index, _infer_dtype_from_scalar,
1111
notnull)
1212
from pandas.core.categorical import Factor
1313
from pandas.core.index import (Index, MultiIndex, _ensure_index,
@@ -657,8 +657,8 @@ def set_value(self, *args):
657657
axes = self._expand_axes(args)
658658
d = self._construct_axes_dict_from(self, axes, copy=False)
659659
result = self.reindex(**d)
660-
661-
likely_dtype = com._infer_dtype(args[-1])
660+
args = list(args)
661+
args[-1], likely_dtype = _infer_dtype_from_scalar(args[-1])
662662
made_bigger = not np.array_equal(
663663
axes[0], getattr(self, self._info_axis))
664664
# how to make this logic simpler?
@@ -693,7 +693,7 @@ def __setitem__(self, key, value):
693693
assert(value.shape == shape[1:])
694694
mat = np.asarray(value)
695695
elif np.isscalar(value):
696-
dtype = _infer_dtype(value)
696+
value, dtype = _infer_dtype_from_scalar(value)
697697
mat = np.empty(shape[1:], dtype=dtype)
698698
mat.fill(value)
699699
else:

pandas/core/series.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
from pandas.core.common import (isnull, notnull, _is_bool_indexer,
1818
_default_index, _maybe_upcast,
19-
_asarray_tuplesafe, is_integer_dtype)
19+
_asarray_tuplesafe, is_integer_dtype,
20+
_infer_dtype_from_scalar)
2021
from pandas.core.index import (Index, MultiIndex, InvalidIndexError,
2122
_ensure_index, _handle_legacy_indexes)
2223
from pandas.core.indexing import _SeriesIndexer, _check_bool_indexer
@@ -3129,7 +3130,7 @@ def _try_cast(arr):
31293130

31303131
# figure out the dtype from the value (upcast if necessary)
31313132
if dtype is None:
3132-
value, dtype = com._dtype_from_scalar(value)
3133+
value, dtype = _infer_dtype_from_scalar(value)
31333134
else:
31343135
# need to possibly convert the value here
31353136
value = com._possibly_cast_to_datetime(value, dtype)

0 commit comments

Comments
 (0)