numerictypes.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. """
  2. numerictypes: Define the numeric type objects
  3. This module is designed so "from numerictypes import \\*" is safe.
  4. Exported symbols include:
  5. Dictionary with all registered number types (including aliases):
  6. typeDict
  7. Type objects (not all will be available, depends on platform):
  8. see variable sctypes for which ones you have
  9. Bit-width names
  10. int8 int16 int32 int64 int128
  11. uint8 uint16 uint32 uint64 uint128
  12. float16 float32 float64 float96 float128 float256
  13. complex32 complex64 complex128 complex192 complex256 complex512
  14. datetime64 timedelta64
  15. c-based names
  16. bool_
  17. object_
  18. void, str_, unicode_
  19. byte, ubyte,
  20. short, ushort
  21. intc, uintc,
  22. intp, uintp,
  23. int_, uint,
  24. longlong, ulonglong,
  25. single, csingle,
  26. float_, complex_,
  27. longfloat, clongfloat,
  28. As part of the type-hierarchy: xx -- is bit-width
  29. generic
  30. +-> bool_ (kind=b)
  31. +-> number
  32. | +-> integer
  33. | | +-> signedinteger (intxx) (kind=i)
  34. | | | byte
  35. | | | short
  36. | | | intc
  37. | | | intp int0
  38. | | | int_
  39. | | | longlong
  40. | | \\-> unsignedinteger (uintxx) (kind=u)
  41. | | ubyte
  42. | | ushort
  43. | | uintc
  44. | | uintp uint0
  45. | | uint_
  46. | | ulonglong
  47. | +-> inexact
  48. | +-> floating (floatxx) (kind=f)
  49. | | half
  50. | | single
  51. | | float_ (double)
  52. | | longfloat
  53. | \\-> complexfloating (complexxx) (kind=c)
  54. | csingle (singlecomplex)
  55. | complex_ (cfloat, cdouble)
  56. | clongfloat (longcomplex)
  57. +-> flexible
  58. | +-> character
  59. | | str_ (string_, bytes_) (kind=S) [Python 2]
  60. | | unicode_ (kind=U) [Python 2]
  61. | |
  62. | | bytes_ (string_) (kind=S) [Python 3]
  63. | | str_ (unicode_) (kind=U) [Python 3]
  64. | |
  65. | \\-> void (kind=V)
  66. \\-> object_ (not used much) (kind=O)
  67. """
  68. import types as _types
  69. import numbers
  70. import warnings
  71. from numpy.core.multiarray import (
  72. typeinfo, ndarray, array, empty, dtype, datetime_data,
  73. datetime_as_string, busday_offset, busday_count, is_busday,
  74. busdaycalendar
  75. )
  76. from numpy.core.overrides import set_module
  77. # we add more at the bottom
  78. __all__ = ['sctypeDict', 'sctypeNA', 'typeDict', 'typeNA', 'sctypes',
  79. 'ScalarType', 'obj2sctype', 'cast', 'nbytes', 'sctype2char',
  80. 'maximum_sctype', 'issctype', 'typecodes', 'find_common_type',
  81. 'issubdtype', 'datetime_data', 'datetime_as_string',
  82. 'busday_offset', 'busday_count', 'is_busday', 'busdaycalendar',
  83. ]
  84. # we don't need all these imports, but we need to keep them for compatibility
  85. # for users using np.core.numerictypes.UPPER_TABLE
  86. from ._string_helpers import (
  87. english_lower, english_upper, english_capitalize, LOWER_TABLE, UPPER_TABLE
  88. )
  89. from ._type_aliases import (
  90. sctypeDict,
  91. sctypeNA,
  92. allTypes,
  93. bitname,
  94. sctypes,
  95. _concrete_types,
  96. _concrete_typeinfo,
  97. _bits_of,
  98. )
  99. from ._dtype import _kind_name
  100. # we don't export these for import *, but we do want them accessible
  101. # as numerictypes.bool, etc.
  102. from builtins import bool, int, float, complex, object, str, bytes
  103. from numpy.compat import long, unicode
  104. # We use this later
  105. generic = allTypes['generic']
  106. genericTypeRank = ['bool', 'int8', 'uint8', 'int16', 'uint16',
  107. 'int32', 'uint32', 'int64', 'uint64', 'int128',
  108. 'uint128', 'float16',
  109. 'float32', 'float64', 'float80', 'float96', 'float128',
  110. 'float256',
  111. 'complex32', 'complex64', 'complex128', 'complex160',
  112. 'complex192', 'complex256', 'complex512', 'object']
  113. @set_module('numpy')
  114. def maximum_sctype(t):
  115. """
  116. Return the scalar type of highest precision of the same kind as the input.
  117. Parameters
  118. ----------
  119. t : dtype or dtype specifier
  120. The input data type. This can be a `dtype` object or an object that
  121. is convertible to a `dtype`.
  122. Returns
  123. -------
  124. out : dtype
  125. The highest precision data type of the same kind (`dtype.kind`) as `t`.
  126. See Also
  127. --------
  128. obj2sctype, mintypecode, sctype2char
  129. dtype
  130. Examples
  131. --------
  132. >>> np.maximum_sctype(int)
  133. <class 'numpy.int64'>
  134. >>> np.maximum_sctype(np.uint8)
  135. <class 'numpy.uint64'>
  136. >>> np.maximum_sctype(complex)
  137. <class 'numpy.complex256'> # may vary
  138. >>> np.maximum_sctype(str)
  139. <class 'numpy.str_'>
  140. >>> np.maximum_sctype('i2')
  141. <class 'numpy.int64'>
  142. >>> np.maximum_sctype('f4')
  143. <class 'numpy.float128'> # may vary
  144. """
  145. g = obj2sctype(t)
  146. if g is None:
  147. return t
  148. t = g
  149. base = _kind_name(dtype(t))
  150. if base in sctypes:
  151. return sctypes[base][-1]
  152. else:
  153. return t
  154. @set_module('numpy')
  155. def issctype(rep):
  156. """
  157. Determines whether the given object represents a scalar data-type.
  158. Parameters
  159. ----------
  160. rep : any
  161. If `rep` is an instance of a scalar dtype, True is returned. If not,
  162. False is returned.
  163. Returns
  164. -------
  165. out : bool
  166. Boolean result of check whether `rep` is a scalar dtype.
  167. See Also
  168. --------
  169. issubsctype, issubdtype, obj2sctype, sctype2char
  170. Examples
  171. --------
  172. >>> np.issctype(np.int32)
  173. True
  174. >>> np.issctype(list)
  175. False
  176. >>> np.issctype(1.1)
  177. False
  178. Strings are also a scalar type:
  179. >>> np.issctype(np.dtype('str'))
  180. True
  181. """
  182. if not isinstance(rep, (type, dtype)):
  183. return False
  184. try:
  185. res = obj2sctype(rep)
  186. if res and res != object_:
  187. return True
  188. return False
  189. except Exception:
  190. return False
  191. @set_module('numpy')
  192. def obj2sctype(rep, default=None):
  193. """
  194. Return the scalar dtype or NumPy equivalent of Python type of an object.
  195. Parameters
  196. ----------
  197. rep : any
  198. The object of which the type is returned.
  199. default : any, optional
  200. If given, this is returned for objects whose types can not be
  201. determined. If not given, None is returned for those objects.
  202. Returns
  203. -------
  204. dtype : dtype or Python type
  205. The data type of `rep`.
  206. See Also
  207. --------
  208. sctype2char, issctype, issubsctype, issubdtype, maximum_sctype
  209. Examples
  210. --------
  211. >>> np.obj2sctype(np.int32)
  212. <class 'numpy.int32'>
  213. >>> np.obj2sctype(np.array([1., 2.]))
  214. <class 'numpy.float64'>
  215. >>> np.obj2sctype(np.array([1.j]))
  216. <class 'numpy.complex128'>
  217. >>> np.obj2sctype(dict)
  218. <class 'numpy.object_'>
  219. >>> np.obj2sctype('string')
  220. >>> np.obj2sctype(1, default=list)
  221. <class 'list'>
  222. """
  223. # prevent abstract classes being upcast
  224. if isinstance(rep, type) and issubclass(rep, generic):
  225. return rep
  226. # extract dtype from arrays
  227. if isinstance(rep, ndarray):
  228. return rep.dtype.type
  229. # fall back on dtype to convert
  230. try:
  231. res = dtype(rep)
  232. except Exception:
  233. return default
  234. else:
  235. return res.type
  236. @set_module('numpy')
  237. def issubclass_(arg1, arg2):
  238. """
  239. Determine if a class is a subclass of a second class.
  240. `issubclass_` is equivalent to the Python built-in ``issubclass``,
  241. except that it returns False instead of raising a TypeError if one
  242. of the arguments is not a class.
  243. Parameters
  244. ----------
  245. arg1 : class
  246. Input class. True is returned if `arg1` is a subclass of `arg2`.
  247. arg2 : class or tuple of classes.
  248. Input class. If a tuple of classes, True is returned if `arg1` is a
  249. subclass of any of the tuple elements.
  250. Returns
  251. -------
  252. out : bool
  253. Whether `arg1` is a subclass of `arg2` or not.
  254. See Also
  255. --------
  256. issubsctype, issubdtype, issctype
  257. Examples
  258. --------
  259. >>> np.issubclass_(np.int32, int)
  260. False
  261. >>> np.issubclass_(np.int32, float)
  262. False
  263. >>> np.issubclass_(np.float64, float)
  264. True
  265. """
  266. try:
  267. return issubclass(arg1, arg2)
  268. except TypeError:
  269. return False
  270. @set_module('numpy')
  271. def issubsctype(arg1, arg2):
  272. """
  273. Determine if the first argument is a subclass of the second argument.
  274. Parameters
  275. ----------
  276. arg1, arg2 : dtype or dtype specifier
  277. Data-types.
  278. Returns
  279. -------
  280. out : bool
  281. The result.
  282. See Also
  283. --------
  284. issctype, issubdtype, obj2sctype
  285. Examples
  286. --------
  287. >>> np.issubsctype('S8', str)
  288. False
  289. >>> np.issubsctype(np.array([1]), int)
  290. True
  291. >>> np.issubsctype(np.array([1]), float)
  292. False
  293. """
  294. return issubclass(obj2sctype(arg1), obj2sctype(arg2))
  295. @set_module('numpy')
  296. def issubdtype(arg1, arg2):
  297. """
  298. Returns True if first argument is a typecode lower/equal in type hierarchy.
  299. Parameters
  300. ----------
  301. arg1, arg2 : dtype_like
  302. dtype or string representing a typecode.
  303. Returns
  304. -------
  305. out : bool
  306. See Also
  307. --------
  308. issubsctype, issubclass_
  309. numpy.core.numerictypes : Overview of numpy type hierarchy.
  310. Examples
  311. --------
  312. >>> np.issubdtype('S1', np.string_)
  313. True
  314. >>> np.issubdtype(np.float64, np.float32)
  315. False
  316. """
  317. if not issubclass_(arg1, generic):
  318. arg1 = dtype(arg1).type
  319. if not issubclass_(arg2, generic):
  320. arg2 = dtype(arg2).type
  321. return issubclass(arg1, arg2)
  322. # This dictionary allows look up based on any alias for an array data-type
  323. class _typedict(dict):
  324. """
  325. Base object for a dictionary for look-up with any alias for an array dtype.
  326. Instances of `_typedict` can not be used as dictionaries directly,
  327. first they have to be populated.
  328. """
  329. def __getitem__(self, obj):
  330. return dict.__getitem__(self, obj2sctype(obj))
  331. nbytes = _typedict()
  332. _alignment = _typedict()
  333. _maxvals = _typedict()
  334. _minvals = _typedict()
  335. def _construct_lookups():
  336. for name, info in _concrete_typeinfo.items():
  337. obj = info.type
  338. nbytes[obj] = info.bits // 8
  339. _alignment[obj] = info.alignment
  340. if len(info) > 5:
  341. _maxvals[obj] = info.max
  342. _minvals[obj] = info.min
  343. else:
  344. _maxvals[obj] = None
  345. _minvals[obj] = None
  346. _construct_lookups()
  347. @set_module('numpy')
  348. def sctype2char(sctype):
  349. """
  350. Return the string representation of a scalar dtype.
  351. Parameters
  352. ----------
  353. sctype : scalar dtype or object
  354. If a scalar dtype, the corresponding string character is
  355. returned. If an object, `sctype2char` tries to infer its scalar type
  356. and then return the corresponding string character.
  357. Returns
  358. -------
  359. typechar : str
  360. The string character corresponding to the scalar type.
  361. Raises
  362. ------
  363. ValueError
  364. If `sctype` is an object for which the type can not be inferred.
  365. See Also
  366. --------
  367. obj2sctype, issctype, issubsctype, mintypecode
  368. Examples
  369. --------
  370. >>> for sctype in [np.int32, np.double, np.complex_, np.string_, np.ndarray]:
  371. ... print(np.sctype2char(sctype))
  372. l # may vary
  373. d
  374. D
  375. S
  376. O
  377. >>> x = np.array([1., 2-1.j])
  378. >>> np.sctype2char(x)
  379. 'D'
  380. >>> np.sctype2char(list)
  381. 'O'
  382. """
  383. sctype = obj2sctype(sctype)
  384. if sctype is None:
  385. raise ValueError("unrecognized type")
  386. if sctype not in _concrete_types:
  387. # for compatibility
  388. raise KeyError(sctype)
  389. return dtype(sctype).char
  390. # Create dictionary of casting functions that wrap sequences
  391. # indexed by type or type character
  392. cast = _typedict()
  393. for key in _concrete_types:
  394. cast[key] = lambda x, k=key: array(x, copy=False).astype(k)
  395. try:
  396. ScalarType = [_types.IntType, _types.FloatType, _types.ComplexType,
  397. _types.LongType, _types.BooleanType,
  398. _types.StringType, _types.UnicodeType, _types.BufferType]
  399. except AttributeError:
  400. # Py3K
  401. ScalarType = [int, float, complex, int, bool, bytes, str, memoryview]
  402. ScalarType.extend(_concrete_types)
  403. ScalarType = tuple(ScalarType)
  404. # Now add the types we've determined to this module
  405. for key in allTypes:
  406. globals()[key] = allTypes[key]
  407. __all__.append(key)
  408. del key
  409. typecodes = {'Character':'c',
  410. 'Integer':'bhilqp',
  411. 'UnsignedInteger':'BHILQP',
  412. 'Float':'efdg',
  413. 'Complex':'FDG',
  414. 'AllInteger':'bBhHiIlLqQpP',
  415. 'AllFloat':'efdgFDG',
  416. 'Datetime': 'Mm',
  417. 'All':'?bhilqpBHILQPefdgFDGSUVOMm'}
  418. # backwards compatibility --- deprecated name
  419. typeDict = sctypeDict
  420. typeNA = sctypeNA
  421. # b -> boolean
  422. # u -> unsigned integer
  423. # i -> signed integer
  424. # f -> floating point
  425. # c -> complex
  426. # M -> datetime
  427. # m -> timedelta
  428. # S -> string
  429. # U -> Unicode string
  430. # V -> record
  431. # O -> Python object
  432. _kind_list = ['b', 'u', 'i', 'f', 'c', 'S', 'U', 'V', 'O', 'M', 'm']
  433. __test_types = '?'+typecodes['AllInteger'][:-2]+typecodes['AllFloat']+'O'
  434. __len_test_types = len(__test_types)
  435. # Keep incrementing until a common type both can be coerced to
  436. # is found. Otherwise, return None
  437. def _find_common_coerce(a, b):
  438. if a > b:
  439. return a
  440. try:
  441. thisind = __test_types.index(a.char)
  442. except ValueError:
  443. return None
  444. return _can_coerce_all([a, b], start=thisind)
  445. # Find a data-type that all data-types in a list can be coerced to
  446. def _can_coerce_all(dtypelist, start=0):
  447. N = len(dtypelist)
  448. if N == 0:
  449. return None
  450. if N == 1:
  451. return dtypelist[0]
  452. thisind = start
  453. while thisind < __len_test_types:
  454. newdtype = dtype(__test_types[thisind])
  455. numcoerce = len([x for x in dtypelist if newdtype >= x])
  456. if numcoerce == N:
  457. return newdtype
  458. thisind += 1
  459. return None
  460. def _register_types():
  461. numbers.Integral.register(integer)
  462. numbers.Complex.register(inexact)
  463. numbers.Real.register(floating)
  464. numbers.Number.register(number)
  465. _register_types()
  466. @set_module('numpy')
  467. def find_common_type(array_types, scalar_types):
  468. """
  469. Determine common type following standard coercion rules.
  470. Parameters
  471. ----------
  472. array_types : sequence
  473. A list of dtypes or dtype convertible objects representing arrays.
  474. scalar_types : sequence
  475. A list of dtypes or dtype convertible objects representing scalars.
  476. Returns
  477. -------
  478. datatype : dtype
  479. The common data type, which is the maximum of `array_types` ignoring
  480. `scalar_types`, unless the maximum of `scalar_types` is of a
  481. different kind (`dtype.kind`). If the kind is not understood, then
  482. None is returned.
  483. See Also
  484. --------
  485. dtype, common_type, can_cast, mintypecode
  486. Examples
  487. --------
  488. >>> np.find_common_type([], [np.int64, np.float32, complex])
  489. dtype('complex128')
  490. >>> np.find_common_type([np.int64, np.float32], [])
  491. dtype('float64')
  492. The standard casting rules ensure that a scalar cannot up-cast an
  493. array unless the scalar is of a fundamentally different kind of data
  494. (i.e. under a different hierarchy in the data type hierarchy) then
  495. the array:
  496. >>> np.find_common_type([np.float32], [np.int64, np.float64])
  497. dtype('float32')
  498. Complex is of a different type, so it up-casts the float in the
  499. `array_types` argument:
  500. >>> np.find_common_type([np.float32], [complex])
  501. dtype('complex128')
  502. Type specifier strings are convertible to dtypes and can therefore
  503. be used instead of dtypes:
  504. >>> np.find_common_type(['f4', 'f4', 'i4'], ['c8'])
  505. dtype('complex128')
  506. """
  507. array_types = [dtype(x) for x in array_types]
  508. scalar_types = [dtype(x) for x in scalar_types]
  509. maxa = _can_coerce_all(array_types)
  510. maxsc = _can_coerce_all(scalar_types)
  511. if maxa is None:
  512. return maxsc
  513. if maxsc is None:
  514. return maxa
  515. try:
  516. index_a = _kind_list.index(maxa.kind)
  517. index_sc = _kind_list.index(maxsc.kind)
  518. except ValueError:
  519. return None
  520. if index_sc > index_a:
  521. return _find_common_coerce(maxsc, maxa)
  522. else:
  523. return maxa