Skip to content

Commit 2104948

Browse files
committed
Factor the common type discovery to an internal function.
1 parent 6782bc7 commit 2104948

File tree

4 files changed

+16
-9
lines changed

4 files changed

+16
-9
lines changed

pandas/core/internals.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
_infer_dtype_from_scalar,
3636
_soft_convert_objects,
3737
_possibly_convert_objects,
38-
_astype_nansafe)
38+
_astype_nansafe,
39+
_find_common_type)
3940
from pandas.types.missing import (isnull, array_equivalent,
4041
_is_na_compat,
4142
is_null_datelike_scalar)
@@ -4460,7 +4461,7 @@ def _interleaved_dtype(blocks):
44604461
elif have_int and not have_float and not have_complex:
44614462
# if we are mixing unsigned and signed, then return
44624463
# the next biggest int type (if we can)
4463-
lcd = np.find_common_type([b.dtype for b in counts[IntBlock]], [])
4464+
lcd = _find_common_type([b.dtype for b in counts[IntBlock]])
44644465
kinds = set([i.dtype.kind for i in counts[IntBlock]])
44654466
if len(kinds) == 1:
44664467
return lcd
@@ -4477,7 +4478,7 @@ def _interleaved_dtype(blocks):
44774478
return np.dtype('c16')
44784479
else:
44794480
introspection_blks = counts[FloatBlock] + counts[SparseBlock]
4480-
return np.find_common_type([b.dtype for b in introspection_blks], [])
4481+
return _find_common_type([b.dtype for b in introspection_blks])
44814482

44824483

44834484
def _consolidate(blocks):

pandas/core/ops.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
is_bool_dtype, is_datetimetz,
3131
is_list_like,
3232
_ensure_object)
33-
from pandas.types.cast import _maybe_upcast_putmask
33+
from pandas.types.cast import _maybe_upcast_putmask, _find_common_type
3434
from pandas.types.generic import ABCSeries, ABCIndex, ABCPeriodIndex
3535

3636
# -----------------------------------------------------------------------------
@@ -616,7 +616,7 @@ def na_op(x, y):
616616
raise_on_error=True, **eval_kwargs)
617617
except TypeError:
618618
if isinstance(y, (np.ndarray, ABCSeries, pd.Index)):
619-
dtype = np.find_common_type([x.dtype, y.dtype], [])
619+
dtype = _find_common_type([x.dtype, y.dtype])
620620
result = np.empty(x.size, dtype=dtype)
621621
mask = notnull(x) & notnull(y)
622622
result[mask] = op(x[mask], _values_from_object(y[mask]))

pandas/tests/frame/test_indexing.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -2760,10 +2760,10 @@ def test_indexing_multiple(self):
27602760
tm.assert_sp_frame_equal(self.sdf, self.sdf.loc[:])
27612761
tm.assert_sp_frame_equal(self.sdf.iloc[[1, 2]],
27622762
pd.SparseDataFrame({
2763-
'string': ['b', 'c'],
2764-
'int': [2, 3],
2765-
'float': [1.2, 1.3],
2766-
'object': [{}, set()]
2763+
'string': self.string_series.iloc[[1, 2]],
2764+
'int': self.int_series.iloc[[1, 2]],
2765+
'float': self.float_series.iloc[[1, 2]],
2766+
'object': self.object_series.iloc[[1, 2]]
27672767
}, index=[1, 2])[self.cols])
27682768
tm.assert_sp_frame_equal(self.sdf[['int', 'string']],
27692769
pd.SparseDataFrame({

pandas/types/cast.py

+6
Original file line numberDiff line numberDiff line change
@@ -861,3 +861,9 @@ def _possibly_cast_to_datetime(value, dtype, errors='raise'):
861861
value = _possibly_infer_to_datetimelike(value)
862862

863863
return value
864+
865+
866+
def _find_common_type(types):
867+
"""Find a common data type among the given dtypes."""
868+
# TODO: enable using pandas specific types
869+
return np.find_common_type(types, [])

0 commit comments

Comments
 (0)