From a3a2ee25242269844757ad2526ace3cd8e85089b Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Wed, 4 Oct 2017 10:41:23 -0500 Subject: [PATCH] Return dtype, not type --- pandas/core/dtypes/cast.py | 12 ++++++------ pandas/core/internals.py | 35 ++++++++++++++++++----------------- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index 0096783d1dddb5..9b7877cc072803 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -483,8 +483,8 @@ def infer_dtype_from_array(arr, pandas_dtype=False): return arr.dtype, arr -def _maybe_infer_dtype_type(element): - """Try to infer an object's dtype's type, for use in arithmetic ops +def maybe_infer_dtype_type(element): + """Try to infer an object's dtype, for use in arithmetic ops Uses `element.dtype` if that's available. Objects implementing the iterator protocol are cast to a NumPy array, @@ -503,16 +503,16 @@ def _maybe_infer_dtype_type(element): Examples -------- >>> from collections import namedtuple - >>> Foo = namedtuple("dtype") - >>> _maybe_infer_dtype_type(Foo(np.dtype("i8"))) + >>> Foo = namedtuple("Foo", "dtype") + >>> maybe_infer_dtype_type(Foo(np.dtype("i8"))) numpy.int64 """ tipo = None if hasattr(element, 'dtype'): - tipo = element.dtype.type + tipo = element.dtype elif is_list_like(element): element = np.asarray(element) - tipo = element.dtype.type + tipo = element.dtype return tipo diff --git a/pandas/core/internals.py b/pandas/core/internals.py index b6f8fc5fef853e..945afc432eef79 100644 --- a/pandas/core/internals.py +++ b/pandas/core/internals.py @@ -45,7 +45,7 @@ maybe_convert_objects, astype_nansafe, find_common_type, - _maybe_infer_dtype_type) + maybe_infer_dtype_type) from pandas.core.dtypes.missing import ( isna, notna, array_equivalent, _isna_compat, @@ -630,9 +630,9 @@ def convert(self, copy=True, **kwargs): def _can_hold_element(self, element): """ require the same dtype as ourselves """ dtype = self.values.dtype.type - tipo = _maybe_infer_dtype_type(element) + tipo = maybe_infer_dtype_type(element) if tipo: - return issubclass(tipo, dtype) + return issubclass(tipo.type, dtype) return isinstance(element, dtype) def _try_cast_result(self, result, dtype=None): @@ -1806,10 +1806,10 @@ class FloatBlock(FloatOrComplexBlock): _downcast_dtype = 'int64' def _can_hold_element(self, element): - tipo = _maybe_infer_dtype_type(element) + tipo = maybe_infer_dtype_type(element) if tipo: - return (issubclass(tipo, (np.floating, np.integer)) and - not issubclass(tipo, (np.datetime64, np.timedelta64))) + return (issubclass(tipo.type, (np.floating, np.integer)) and + not issubclass(tipo.type, (np.datetime64, np.timedelta64))) return (isinstance(element, (float, int, np.floating, np.int_)) and not isinstance(element, (bool, np.bool_, datetime, timedelta, np.datetime64, np.timedelta64))) @@ -1855,9 +1855,9 @@ class ComplexBlock(FloatOrComplexBlock): is_complex = True def _can_hold_element(self, element): - tipo = _maybe_infer_dtype_type(element) + tipo = maybe_infer_dtype_type(element) if tipo: - return issubclass(tipo, + return issubclass(tipo.type, (np.floating, np.integer, np.complexfloating)) return (isinstance(element, (float, int, complex, np.float_, np.int_)) and @@ -1873,11 +1873,12 @@ class IntBlock(NumericBlock): _can_hold_na = False def _can_hold_element(self, element): - tipo = _maybe_infer_dtype_type(element) + tipo = maybe_infer_dtype_type(element) if tipo: - return (issubclass(tipo, np.integer) and - not issubclass(tipo, (np.datetime64, np.timedelta64)) and - self.dtype.itemsize >= element.dtype.itemsize) + return (issubclass(tipo.type, np.integer) and + not issubclass(tipo.type, (np.datetime64, + np.timedelta64)) and + self.dtype.itemsize >= tipo.itemsize) return is_integer(element) def should_store(self, value): @@ -1915,9 +1916,9 @@ def _box_func(self): return lambda x: tslib.Timedelta(x, unit='ns') def _can_hold_element(self, element): - tipo = _maybe_infer_dtype_type(element) + tipo = maybe_infer_dtype_type(element) if tipo: - return issubclass(tipo, np.timedelta64) + return issubclass(tipo.type, np.timedelta64) return isinstance(element, (timedelta, np.timedelta64)) def fillna(self, value, **kwargs): @@ -2015,9 +2016,9 @@ class BoolBlock(NumericBlock): _can_hold_na = False def _can_hold_element(self, element): - tipo = _maybe_infer_dtype_type(element) + tipo = maybe_infer_dtype_type(element) if tipo: - return issubclass(tipo, np.bool_) + return issubclass(tipo.type, np.bool_) return isinstance(element, (bool, np.bool_)) def should_store(self, value): @@ -2447,7 +2448,7 @@ def _astype(self, dtype, mgr=None, **kwargs): return super(DatetimeBlock, self)._astype(dtype=dtype, **kwargs) def _can_hold_element(self, element): - tipo = _maybe_infer_dtype_type(element) + tipo = maybe_infer_dtype_type(element) if tipo: # TODO: this still uses asarray, instead of dtype.type element = np.array(element)