Skip to content

Commit

Permalink
Return dtype, not type
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAugspurger committed Oct 4, 2017
1 parent 7a587f6 commit a3a2ee2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
12 changes: 6 additions & 6 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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


Expand Down
35 changes: 18 additions & 17 deletions pandas/core/internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)))
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit a3a2ee2

Please sign in to comment.