Skip to content

Commit 8d675ad

Browse files
committed
Add tests for common dtypes, raises check for pandas ones.
1 parent eebcb23 commit 8d675ad

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

pandas/tests/types/test_cast.py

+40-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
_possibly_convert_objects,
1616
_infer_dtype_from_scalar,
1717
_maybe_convert_string_to_object,
18-
_maybe_convert_scalar)
18+
_maybe_convert_scalar,
19+
_find_common_type)
20+
from pandas.types.dtypes import (CategoricalDtype,
21+
DatetimeTZDtype)
1922
from pandas.util import testing as tm
2023

2124
_multiprocess_can_split_ = True
@@ -188,6 +191,42 @@ def test_possibly_convert_objects_copy(self):
188191
self.assertTrue(values is not out)
189192

190193

194+
class TestCommonTypes(tm.TestCase):
195+
def setUp(self):
196+
super(TestCommonTypes, self).setUp()
197+
198+
def test_numpy_dtypes(self):
199+
self.assertEqual(_find_common_type([np.int64]), np.int64)
200+
self.assertEqual(_find_common_type([np.uint64]), np.uint64)
201+
self.assertEqual(_find_common_type([np.float32]), np.float32)
202+
self.assertEqual(_find_common_type([np.object]), np.object)
203+
204+
self.assertEqual(_find_common_type([np.int16, np.int64]),
205+
np.int64)
206+
self.assertEqual(_find_common_type([np.int32, np.uint32]),
207+
np.int64)
208+
self.assertEqual(_find_common_type([np.object, np.float32]),
209+
np.object)
210+
self.assertEqual(_find_common_type([np.object, np.int16]),
211+
np.object)
212+
self.assertEqual(_find_common_type([np.int16, np.float64]),
213+
np.float64)
214+
self.assertEqual(_find_common_type([np.float16, np.int16]),
215+
np.float32)
216+
self.assertEqual(_find_common_type([np.float16, np.int64]),
217+
np.float64)
218+
self.assertEqual(_find_common_type([np.complex128, np.int32]),
219+
np.complex128)
220+
self.assertEqual(_find_common_type([np.uint64, np.int64]),
221+
np.float64)
222+
223+
def test_pandas_dtypes(self):
224+
with self.assertRaises(TypeError):
225+
self.assertEqual(_find_common_type([CategoricalDtype]),
226+
CategoricalDtype)
227+
self.assertEqual(_find_common_type([DatetimeTZDtype]),
228+
DatetimeTZDtype)
229+
191230
if __name__ == '__main__':
192231
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
193232
exit=False)

pandas/types/cast.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
_ensure_int32, _ensure_int64,
2020
_NS_DTYPE, _TD_DTYPE, _INT64_DTYPE,
2121
_DATELIKE_DTYPES, _POSSIBLY_CAST_DTYPES)
22+
from .dtypes import ExtensionDtype
2223
from .generic import ABCDatetimeIndex, ABCPeriodIndex, ABCSeries
2324
from .missing import isnull, notnull
2425
from .inference import is_list_like
@@ -865,5 +866,9 @@ def _possibly_cast_to_datetime(value, dtype, errors='raise'):
865866

866867
def _find_common_type(types):
867868
"""Find a common data type among the given dtypes."""
868-
# TODO: enable using pandas specific types
869+
# TODO: enable using pandas-specific types
870+
if any(issubclass(t, ExtensionDtype) or isinstance(t, ExtensionDtype)
871+
for t in types):
872+
raise TypeError("Common type discovery is currently only "
873+
"supported for pure numpy dtypes.")
869874
return np.find_common_type(types, [])

0 commit comments

Comments
 (0)