numpy 2.3.5__cp313-cp313-macosx_14_0_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of numpy might be problematic. Click here for more details.

Files changed (897) hide show
  1. numpy/__config__.py +170 -0
  2. numpy/__config__.pyi +102 -0
  3. numpy/__init__.cython-30.pxd +1241 -0
  4. numpy/__init__.pxd +1154 -0
  5. numpy/__init__.py +945 -0
  6. numpy/__init__.pyi +6147 -0
  7. numpy/_array_api_info.py +346 -0
  8. numpy/_array_api_info.pyi +207 -0
  9. numpy/_configtool.py +39 -0
  10. numpy/_configtool.pyi +1 -0
  11. numpy/_core/__init__.py +186 -0
  12. numpy/_core/__init__.pyi +2 -0
  13. numpy/_core/_add_newdocs.py +6967 -0
  14. numpy/_core/_add_newdocs.pyi +3 -0
  15. numpy/_core/_add_newdocs_scalars.py +390 -0
  16. numpy/_core/_add_newdocs_scalars.pyi +16 -0
  17. numpy/_core/_asarray.py +134 -0
  18. numpy/_core/_asarray.pyi +41 -0
  19. numpy/_core/_dtype.py +366 -0
  20. numpy/_core/_dtype.pyi +58 -0
  21. numpy/_core/_dtype_ctypes.py +120 -0
  22. numpy/_core/_dtype_ctypes.pyi +83 -0
  23. numpy/_core/_exceptions.py +162 -0
  24. numpy/_core/_exceptions.pyi +55 -0
  25. numpy/_core/_internal.py +958 -0
  26. numpy/_core/_internal.pyi +72 -0
  27. numpy/_core/_machar.py +355 -0
  28. numpy/_core/_machar.pyi +55 -0
  29. numpy/_core/_methods.py +255 -0
  30. numpy/_core/_methods.pyi +22 -0
  31. numpy/_core/_multiarray_tests.cpython-313-darwin.so +0 -0
  32. numpy/_core/_multiarray_umath.cpython-313-darwin.so +0 -0
  33. numpy/_core/_operand_flag_tests.cpython-313-darwin.so +0 -0
  34. numpy/_core/_rational_tests.cpython-313-darwin.so +0 -0
  35. numpy/_core/_simd.cpython-313-darwin.so +0 -0
  36. numpy/_core/_simd.pyi +25 -0
  37. numpy/_core/_string_helpers.py +100 -0
  38. numpy/_core/_string_helpers.pyi +12 -0
  39. numpy/_core/_struct_ufunc_tests.cpython-313-darwin.so +0 -0
  40. numpy/_core/_type_aliases.py +119 -0
  41. numpy/_core/_type_aliases.pyi +97 -0
  42. numpy/_core/_ufunc_config.py +491 -0
  43. numpy/_core/_ufunc_config.pyi +78 -0
  44. numpy/_core/_umath_tests.cpython-313-darwin.so +0 -0
  45. numpy/_core/arrayprint.py +1775 -0
  46. numpy/_core/arrayprint.pyi +238 -0
  47. numpy/_core/cversions.py +13 -0
  48. numpy/_core/defchararray.py +1427 -0
  49. numpy/_core/defchararray.pyi +1135 -0
  50. numpy/_core/einsumfunc.py +1498 -0
  51. numpy/_core/einsumfunc.pyi +184 -0
  52. numpy/_core/fromnumeric.py +4269 -0
  53. numpy/_core/fromnumeric.pyi +1750 -0
  54. numpy/_core/function_base.py +545 -0
  55. numpy/_core/function_base.pyi +278 -0
  56. numpy/_core/getlimits.py +748 -0
  57. numpy/_core/getlimits.pyi +3 -0
  58. numpy/_core/include/numpy/__multiarray_api.c +376 -0
  59. numpy/_core/include/numpy/__multiarray_api.h +1628 -0
  60. numpy/_core/include/numpy/__ufunc_api.c +54 -0
  61. numpy/_core/include/numpy/__ufunc_api.h +341 -0
  62. numpy/_core/include/numpy/_neighborhood_iterator_imp.h +90 -0
  63. numpy/_core/include/numpy/_numpyconfig.h +33 -0
  64. numpy/_core/include/numpy/_public_dtype_api_table.h +86 -0
  65. numpy/_core/include/numpy/arrayobject.h +7 -0
  66. numpy/_core/include/numpy/arrayscalars.h +196 -0
  67. numpy/_core/include/numpy/dtype_api.h +480 -0
  68. numpy/_core/include/numpy/halffloat.h +70 -0
  69. numpy/_core/include/numpy/ndarrayobject.h +304 -0
  70. numpy/_core/include/numpy/ndarraytypes.h +1950 -0
  71. numpy/_core/include/numpy/npy_2_compat.h +249 -0
  72. numpy/_core/include/numpy/npy_2_complexcompat.h +28 -0
  73. numpy/_core/include/numpy/npy_3kcompat.h +374 -0
  74. numpy/_core/include/numpy/npy_common.h +977 -0
  75. numpy/_core/include/numpy/npy_cpu.h +124 -0
  76. numpy/_core/include/numpy/npy_endian.h +78 -0
  77. numpy/_core/include/numpy/npy_math.h +602 -0
  78. numpy/_core/include/numpy/npy_no_deprecated_api.h +20 -0
  79. numpy/_core/include/numpy/npy_os.h +42 -0
  80. numpy/_core/include/numpy/numpyconfig.h +182 -0
  81. numpy/_core/include/numpy/random/LICENSE.txt +21 -0
  82. numpy/_core/include/numpy/random/bitgen.h +20 -0
  83. numpy/_core/include/numpy/random/distributions.h +209 -0
  84. numpy/_core/include/numpy/random/libdivide.h +2079 -0
  85. numpy/_core/include/numpy/ufuncobject.h +343 -0
  86. numpy/_core/include/numpy/utils.h +37 -0
  87. numpy/_core/lib/libnpymath.a +0 -0
  88. numpy/_core/lib/npy-pkg-config/mlib.ini +12 -0
  89. numpy/_core/lib/npy-pkg-config/npymath.ini +20 -0
  90. numpy/_core/lib/pkgconfig/numpy.pc +7 -0
  91. numpy/_core/memmap.py +363 -0
  92. numpy/_core/memmap.pyi +3 -0
  93. numpy/_core/multiarray.py +1762 -0
  94. numpy/_core/multiarray.pyi +1285 -0
  95. numpy/_core/numeric.py +2760 -0
  96. numpy/_core/numeric.pyi +882 -0
  97. numpy/_core/numerictypes.py +633 -0
  98. numpy/_core/numerictypes.pyi +197 -0
  99. numpy/_core/overrides.py +183 -0
  100. numpy/_core/overrides.pyi +48 -0
  101. numpy/_core/printoptions.py +32 -0
  102. numpy/_core/printoptions.pyi +28 -0
  103. numpy/_core/records.py +1089 -0
  104. numpy/_core/records.pyi +333 -0
  105. numpy/_core/shape_base.py +998 -0
  106. numpy/_core/shape_base.pyi +175 -0
  107. numpy/_core/strings.py +1829 -0
  108. numpy/_core/strings.pyi +511 -0
  109. numpy/_core/tests/_locales.py +72 -0
  110. numpy/_core/tests/_natype.py +205 -0
  111. numpy/_core/tests/data/astype_copy.pkl +0 -0
  112. numpy/_core/tests/data/generate_umath_validation_data.cpp +170 -0
  113. numpy/_core/tests/data/recarray_from_file.fits +0 -0
  114. numpy/_core/tests/data/umath-validation-set-README.txt +15 -0
  115. numpy/_core/tests/data/umath-validation-set-arccos.csv +1429 -0
  116. numpy/_core/tests/data/umath-validation-set-arccosh.csv +1429 -0
  117. numpy/_core/tests/data/umath-validation-set-arcsin.csv +1429 -0
  118. numpy/_core/tests/data/umath-validation-set-arcsinh.csv +1429 -0
  119. numpy/_core/tests/data/umath-validation-set-arctan.csv +1429 -0
  120. numpy/_core/tests/data/umath-validation-set-arctanh.csv +1429 -0
  121. numpy/_core/tests/data/umath-validation-set-cbrt.csv +1429 -0
  122. numpy/_core/tests/data/umath-validation-set-cos.csv +1375 -0
  123. numpy/_core/tests/data/umath-validation-set-cosh.csv +1429 -0
  124. numpy/_core/tests/data/umath-validation-set-exp.csv +412 -0
  125. numpy/_core/tests/data/umath-validation-set-exp2.csv +1429 -0
  126. numpy/_core/tests/data/umath-validation-set-expm1.csv +1429 -0
  127. numpy/_core/tests/data/umath-validation-set-log.csv +271 -0
  128. numpy/_core/tests/data/umath-validation-set-log10.csv +1629 -0
  129. numpy/_core/tests/data/umath-validation-set-log1p.csv +1429 -0
  130. numpy/_core/tests/data/umath-validation-set-log2.csv +1629 -0
  131. numpy/_core/tests/data/umath-validation-set-sin.csv +1370 -0
  132. numpy/_core/tests/data/umath-validation-set-sinh.csv +1429 -0
  133. numpy/_core/tests/data/umath-validation-set-tan.csv +1429 -0
  134. numpy/_core/tests/data/umath-validation-set-tanh.csv +1429 -0
  135. numpy/_core/tests/examples/cython/checks.pyx +373 -0
  136. numpy/_core/tests/examples/cython/meson.build +43 -0
  137. numpy/_core/tests/examples/cython/setup.py +39 -0
  138. numpy/_core/tests/examples/limited_api/limited_api1.c +17 -0
  139. numpy/_core/tests/examples/limited_api/limited_api2.pyx +11 -0
  140. numpy/_core/tests/examples/limited_api/limited_api_latest.c +19 -0
  141. numpy/_core/tests/examples/limited_api/meson.build +59 -0
  142. numpy/_core/tests/examples/limited_api/setup.py +24 -0
  143. numpy/_core/tests/test__exceptions.py +90 -0
  144. numpy/_core/tests/test_abc.py +54 -0
  145. numpy/_core/tests/test_api.py +654 -0
  146. numpy/_core/tests/test_argparse.py +92 -0
  147. numpy/_core/tests/test_array_api_info.py +113 -0
  148. numpy/_core/tests/test_array_coercion.py +911 -0
  149. numpy/_core/tests/test_array_interface.py +222 -0
  150. numpy/_core/tests/test_arraymethod.py +84 -0
  151. numpy/_core/tests/test_arrayobject.py +75 -0
  152. numpy/_core/tests/test_arrayprint.py +1328 -0
  153. numpy/_core/tests/test_casting_floatingpoint_errors.py +154 -0
  154. numpy/_core/tests/test_casting_unittests.py +817 -0
  155. numpy/_core/tests/test_conversion_utils.py +206 -0
  156. numpy/_core/tests/test_cpu_dispatcher.py +49 -0
  157. numpy/_core/tests/test_cpu_features.py +432 -0
  158. numpy/_core/tests/test_custom_dtypes.py +315 -0
  159. numpy/_core/tests/test_cython.py +351 -0
  160. numpy/_core/tests/test_datetime.py +2734 -0
  161. numpy/_core/tests/test_defchararray.py +825 -0
  162. numpy/_core/tests/test_deprecations.py +454 -0
  163. numpy/_core/tests/test_dlpack.py +190 -0
  164. numpy/_core/tests/test_dtype.py +1995 -0
  165. numpy/_core/tests/test_einsum.py +1317 -0
  166. numpy/_core/tests/test_errstate.py +131 -0
  167. numpy/_core/tests/test_extint128.py +217 -0
  168. numpy/_core/tests/test_function_base.py +503 -0
  169. numpy/_core/tests/test_getlimits.py +205 -0
  170. numpy/_core/tests/test_half.py +568 -0
  171. numpy/_core/tests/test_hashtable.py +35 -0
  172. numpy/_core/tests/test_indexerrors.py +125 -0
  173. numpy/_core/tests/test_indexing.py +1455 -0
  174. numpy/_core/tests/test_item_selection.py +167 -0
  175. numpy/_core/tests/test_limited_api.py +102 -0
  176. numpy/_core/tests/test_longdouble.py +369 -0
  177. numpy/_core/tests/test_machar.py +30 -0
  178. numpy/_core/tests/test_mem_overlap.py +930 -0
  179. numpy/_core/tests/test_mem_policy.py +452 -0
  180. numpy/_core/tests/test_memmap.py +246 -0
  181. numpy/_core/tests/test_multiarray.py +10577 -0
  182. numpy/_core/tests/test_multithreading.py +292 -0
  183. numpy/_core/tests/test_nditer.py +3498 -0
  184. numpy/_core/tests/test_nep50_promotions.py +287 -0
  185. numpy/_core/tests/test_numeric.py +4247 -0
  186. numpy/_core/tests/test_numerictypes.py +651 -0
  187. numpy/_core/tests/test_overrides.py +791 -0
  188. numpy/_core/tests/test_print.py +200 -0
  189. numpy/_core/tests/test_protocols.py +46 -0
  190. numpy/_core/tests/test_records.py +544 -0
  191. numpy/_core/tests/test_regression.py +2670 -0
  192. numpy/_core/tests/test_scalar_ctors.py +207 -0
  193. numpy/_core/tests/test_scalar_methods.py +246 -0
  194. numpy/_core/tests/test_scalarbuffer.py +153 -0
  195. numpy/_core/tests/test_scalarinherit.py +105 -0
  196. numpy/_core/tests/test_scalarmath.py +1176 -0
  197. numpy/_core/tests/test_scalarprint.py +403 -0
  198. numpy/_core/tests/test_shape_base.py +891 -0
  199. numpy/_core/tests/test_simd.py +1341 -0
  200. numpy/_core/tests/test_simd_module.py +103 -0
  201. numpy/_core/tests/test_stringdtype.py +1814 -0
  202. numpy/_core/tests/test_strings.py +1499 -0
  203. numpy/_core/tests/test_ufunc.py +3313 -0
  204. numpy/_core/tests/test_umath.py +4928 -0
  205. numpy/_core/tests/test_umath_accuracy.py +124 -0
  206. numpy/_core/tests/test_umath_complex.py +626 -0
  207. numpy/_core/tests/test_unicode.py +368 -0
  208. numpy/_core/umath.py +60 -0
  209. numpy/_core/umath.pyi +197 -0
  210. numpy/_distributor_init.py +15 -0
  211. numpy/_distributor_init.pyi +1 -0
  212. numpy/_expired_attrs_2_0.py +79 -0
  213. numpy/_expired_attrs_2_0.pyi +62 -0
  214. numpy/_globals.py +96 -0
  215. numpy/_globals.pyi +17 -0
  216. numpy/_pyinstaller/__init__.py +0 -0
  217. numpy/_pyinstaller/__init__.pyi +0 -0
  218. numpy/_pyinstaller/hook-numpy.py +36 -0
  219. numpy/_pyinstaller/hook-numpy.pyi +13 -0
  220. numpy/_pyinstaller/tests/__init__.py +16 -0
  221. numpy/_pyinstaller/tests/pyinstaller-smoke.py +32 -0
  222. numpy/_pyinstaller/tests/test_pyinstaller.py +35 -0
  223. numpy/_pytesttester.py +201 -0
  224. numpy/_pytesttester.pyi +18 -0
  225. numpy/_typing/__init__.py +148 -0
  226. numpy/_typing/_add_docstring.py +153 -0
  227. numpy/_typing/_array_like.py +106 -0
  228. numpy/_typing/_char_codes.py +213 -0
  229. numpy/_typing/_dtype_like.py +114 -0
  230. numpy/_typing/_extended_precision.py +15 -0
  231. numpy/_typing/_nbit.py +19 -0
  232. numpy/_typing/_nbit_base.py +94 -0
  233. numpy/_typing/_nbit_base.pyi +40 -0
  234. numpy/_typing/_nested_sequence.py +79 -0
  235. numpy/_typing/_scalars.py +20 -0
  236. numpy/_typing/_shape.py +8 -0
  237. numpy/_typing/_ufunc.py +7 -0
  238. numpy/_typing/_ufunc.pyi +941 -0
  239. numpy/_utils/__init__.py +95 -0
  240. numpy/_utils/__init__.pyi +30 -0
  241. numpy/_utils/_convertions.py +18 -0
  242. numpy/_utils/_convertions.pyi +4 -0
  243. numpy/_utils/_inspect.py +192 -0
  244. numpy/_utils/_inspect.pyi +71 -0
  245. numpy/_utils/_pep440.py +486 -0
  246. numpy/_utils/_pep440.pyi +121 -0
  247. numpy/char/__init__.py +2 -0
  248. numpy/char/__init__.pyi +111 -0
  249. numpy/conftest.py +258 -0
  250. numpy/core/__init__.py +33 -0
  251. numpy/core/__init__.pyi +0 -0
  252. numpy/core/_dtype.py +10 -0
  253. numpy/core/_dtype.pyi +0 -0
  254. numpy/core/_dtype_ctypes.py +10 -0
  255. numpy/core/_dtype_ctypes.pyi +0 -0
  256. numpy/core/_internal.py +27 -0
  257. numpy/core/_multiarray_umath.py +57 -0
  258. numpy/core/_utils.py +21 -0
  259. numpy/core/arrayprint.py +10 -0
  260. numpy/core/defchararray.py +10 -0
  261. numpy/core/einsumfunc.py +10 -0
  262. numpy/core/fromnumeric.py +10 -0
  263. numpy/core/function_base.py +10 -0
  264. numpy/core/getlimits.py +10 -0
  265. numpy/core/multiarray.py +25 -0
  266. numpy/core/numeric.py +12 -0
  267. numpy/core/numerictypes.py +10 -0
  268. numpy/core/overrides.py +10 -0
  269. numpy/core/overrides.pyi +7 -0
  270. numpy/core/records.py +10 -0
  271. numpy/core/shape_base.py +10 -0
  272. numpy/core/umath.py +10 -0
  273. numpy/ctypeslib/__init__.py +13 -0
  274. numpy/ctypeslib/__init__.pyi +33 -0
  275. numpy/ctypeslib/_ctypeslib.py +603 -0
  276. numpy/ctypeslib/_ctypeslib.pyi +245 -0
  277. numpy/doc/ufuncs.py +138 -0
  278. numpy/dtypes.py +41 -0
  279. numpy/dtypes.pyi +631 -0
  280. numpy/exceptions.py +247 -0
  281. numpy/exceptions.pyi +27 -0
  282. numpy/f2py/__init__.py +86 -0
  283. numpy/f2py/__init__.pyi +6 -0
  284. numpy/f2py/__main__.py +5 -0
  285. numpy/f2py/__version__.py +1 -0
  286. numpy/f2py/__version__.pyi +1 -0
  287. numpy/f2py/_backends/__init__.py +9 -0
  288. numpy/f2py/_backends/__init__.pyi +5 -0
  289. numpy/f2py/_backends/_backend.py +44 -0
  290. numpy/f2py/_backends/_backend.pyi +46 -0
  291. numpy/f2py/_backends/_distutils.py +76 -0
  292. numpy/f2py/_backends/_distutils.pyi +13 -0
  293. numpy/f2py/_backends/_meson.py +231 -0
  294. numpy/f2py/_backends/_meson.pyi +63 -0
  295. numpy/f2py/_backends/meson.build.template +55 -0
  296. numpy/f2py/_isocbind.py +62 -0
  297. numpy/f2py/_isocbind.pyi +13 -0
  298. numpy/f2py/_src_pyf.py +247 -0
  299. numpy/f2py/_src_pyf.pyi +29 -0
  300. numpy/f2py/auxfuncs.py +1004 -0
  301. numpy/f2py/auxfuncs.pyi +264 -0
  302. numpy/f2py/capi_maps.py +811 -0
  303. numpy/f2py/capi_maps.pyi +33 -0
  304. numpy/f2py/cb_rules.py +665 -0
  305. numpy/f2py/cb_rules.pyi +17 -0
  306. numpy/f2py/cfuncs.py +1563 -0
  307. numpy/f2py/cfuncs.pyi +31 -0
  308. numpy/f2py/common_rules.py +143 -0
  309. numpy/f2py/common_rules.pyi +9 -0
  310. numpy/f2py/crackfortran.py +3725 -0
  311. numpy/f2py/crackfortran.pyi +258 -0
  312. numpy/f2py/diagnose.py +149 -0
  313. numpy/f2py/diagnose.pyi +1 -0
  314. numpy/f2py/f2py2e.py +786 -0
  315. numpy/f2py/f2py2e.pyi +76 -0
  316. numpy/f2py/f90mod_rules.py +269 -0
  317. numpy/f2py/f90mod_rules.pyi +16 -0
  318. numpy/f2py/func2subr.py +329 -0
  319. numpy/f2py/func2subr.pyi +7 -0
  320. numpy/f2py/rules.py +1629 -0
  321. numpy/f2py/rules.pyi +43 -0
  322. numpy/f2py/setup.cfg +3 -0
  323. numpy/f2py/src/fortranobject.c +1436 -0
  324. numpy/f2py/src/fortranobject.h +173 -0
  325. numpy/f2py/symbolic.py +1516 -0
  326. numpy/f2py/symbolic.pyi +221 -0
  327. numpy/f2py/tests/__init__.py +16 -0
  328. numpy/f2py/tests/src/abstract_interface/foo.f90 +34 -0
  329. numpy/f2py/tests/src/abstract_interface/gh18403_mod.f90 +6 -0
  330. numpy/f2py/tests/src/array_from_pyobj/wrapmodule.c +235 -0
  331. numpy/f2py/tests/src/assumed_shape/.f2py_f2cmap +1 -0
  332. numpy/f2py/tests/src/assumed_shape/foo_free.f90 +34 -0
  333. numpy/f2py/tests/src/assumed_shape/foo_mod.f90 +41 -0
  334. numpy/f2py/tests/src/assumed_shape/foo_use.f90 +19 -0
  335. numpy/f2py/tests/src/assumed_shape/precision.f90 +4 -0
  336. numpy/f2py/tests/src/block_docstring/foo.f +6 -0
  337. numpy/f2py/tests/src/callback/foo.f +62 -0
  338. numpy/f2py/tests/src/callback/gh17797.f90 +7 -0
  339. numpy/f2py/tests/src/callback/gh18335.f90 +17 -0
  340. numpy/f2py/tests/src/callback/gh25211.f +10 -0
  341. numpy/f2py/tests/src/callback/gh25211.pyf +18 -0
  342. numpy/f2py/tests/src/callback/gh26681.f90 +18 -0
  343. numpy/f2py/tests/src/cli/gh_22819.pyf +6 -0
  344. numpy/f2py/tests/src/cli/hi77.f +3 -0
  345. numpy/f2py/tests/src/cli/hiworld.f90 +3 -0
  346. numpy/f2py/tests/src/common/block.f +11 -0
  347. numpy/f2py/tests/src/common/gh19161.f90 +10 -0
  348. numpy/f2py/tests/src/crackfortran/accesstype.f90 +13 -0
  349. numpy/f2py/tests/src/crackfortran/common_with_division.f +17 -0
  350. numpy/f2py/tests/src/crackfortran/data_common.f +8 -0
  351. numpy/f2py/tests/src/crackfortran/data_multiplier.f +5 -0
  352. numpy/f2py/tests/src/crackfortran/data_stmts.f90 +20 -0
  353. numpy/f2py/tests/src/crackfortran/data_with_comments.f +8 -0
  354. numpy/f2py/tests/src/crackfortran/foo_deps.f90 +6 -0
  355. numpy/f2py/tests/src/crackfortran/gh15035.f +16 -0
  356. numpy/f2py/tests/src/crackfortran/gh17859.f +12 -0
  357. numpy/f2py/tests/src/crackfortran/gh22648.pyf +7 -0
  358. numpy/f2py/tests/src/crackfortran/gh23533.f +5 -0
  359. numpy/f2py/tests/src/crackfortran/gh23598.f90 +4 -0
  360. numpy/f2py/tests/src/crackfortran/gh23598Warn.f90 +11 -0
  361. numpy/f2py/tests/src/crackfortran/gh23879.f90 +20 -0
  362. numpy/f2py/tests/src/crackfortran/gh27697.f90 +12 -0
  363. numpy/f2py/tests/src/crackfortran/gh2848.f90 +13 -0
  364. numpy/f2py/tests/src/crackfortran/operators.f90 +49 -0
  365. numpy/f2py/tests/src/crackfortran/privatemod.f90 +11 -0
  366. numpy/f2py/tests/src/crackfortran/publicmod.f90 +10 -0
  367. numpy/f2py/tests/src/crackfortran/pubprivmod.f90 +10 -0
  368. numpy/f2py/tests/src/crackfortran/unicode_comment.f90 +4 -0
  369. numpy/f2py/tests/src/f2cmap/.f2py_f2cmap +1 -0
  370. numpy/f2py/tests/src/f2cmap/isoFortranEnvMap.f90 +9 -0
  371. numpy/f2py/tests/src/isocintrin/isoCtests.f90 +34 -0
  372. numpy/f2py/tests/src/kind/foo.f90 +20 -0
  373. numpy/f2py/tests/src/mixed/foo.f +5 -0
  374. numpy/f2py/tests/src/mixed/foo_fixed.f90 +8 -0
  375. numpy/f2py/tests/src/mixed/foo_free.f90 +8 -0
  376. numpy/f2py/tests/src/modules/gh25337/data.f90 +8 -0
  377. numpy/f2py/tests/src/modules/gh25337/use_data.f90 +6 -0
  378. numpy/f2py/tests/src/modules/gh26920/two_mods_with_no_public_entities.f90 +21 -0
  379. numpy/f2py/tests/src/modules/gh26920/two_mods_with_one_public_routine.f90 +21 -0
  380. numpy/f2py/tests/src/modules/module_data_docstring.f90 +12 -0
  381. numpy/f2py/tests/src/modules/use_modules.f90 +20 -0
  382. numpy/f2py/tests/src/negative_bounds/issue_20853.f90 +7 -0
  383. numpy/f2py/tests/src/parameter/constant_array.f90 +45 -0
  384. numpy/f2py/tests/src/parameter/constant_both.f90 +57 -0
  385. numpy/f2py/tests/src/parameter/constant_compound.f90 +15 -0
  386. numpy/f2py/tests/src/parameter/constant_integer.f90 +22 -0
  387. numpy/f2py/tests/src/parameter/constant_non_compound.f90 +23 -0
  388. numpy/f2py/tests/src/parameter/constant_real.f90 +23 -0
  389. numpy/f2py/tests/src/quoted_character/foo.f +14 -0
  390. numpy/f2py/tests/src/regression/AB.inc +1 -0
  391. numpy/f2py/tests/src/regression/assignOnlyModule.f90 +25 -0
  392. numpy/f2py/tests/src/regression/datonly.f90 +17 -0
  393. numpy/f2py/tests/src/regression/f77comments.f +26 -0
  394. numpy/f2py/tests/src/regression/f77fixedform.f95 +5 -0
  395. numpy/f2py/tests/src/regression/f90continuation.f90 +9 -0
  396. numpy/f2py/tests/src/regression/incfile.f90 +5 -0
  397. numpy/f2py/tests/src/regression/inout.f90 +9 -0
  398. numpy/f2py/tests/src/regression/lower_f2py_fortran.f90 +5 -0
  399. numpy/f2py/tests/src/regression/mod_derived_types.f90 +23 -0
  400. numpy/f2py/tests/src/return_character/foo77.f +45 -0
  401. numpy/f2py/tests/src/return_character/foo90.f90 +48 -0
  402. numpy/f2py/tests/src/return_complex/foo77.f +45 -0
  403. numpy/f2py/tests/src/return_complex/foo90.f90 +48 -0
  404. numpy/f2py/tests/src/return_integer/foo77.f +56 -0
  405. numpy/f2py/tests/src/return_integer/foo90.f90 +59 -0
  406. numpy/f2py/tests/src/return_logical/foo77.f +56 -0
  407. numpy/f2py/tests/src/return_logical/foo90.f90 +59 -0
  408. numpy/f2py/tests/src/return_real/foo77.f +45 -0
  409. numpy/f2py/tests/src/return_real/foo90.f90 +48 -0
  410. numpy/f2py/tests/src/routines/funcfortranname.f +5 -0
  411. numpy/f2py/tests/src/routines/funcfortranname.pyf +11 -0
  412. numpy/f2py/tests/src/routines/subrout.f +4 -0
  413. numpy/f2py/tests/src/routines/subrout.pyf +10 -0
  414. numpy/f2py/tests/src/size/foo.f90 +44 -0
  415. numpy/f2py/tests/src/string/char.f90 +29 -0
  416. numpy/f2py/tests/src/string/fixed_string.f90 +34 -0
  417. numpy/f2py/tests/src/string/gh24008.f +8 -0
  418. numpy/f2py/tests/src/string/gh24662.f90 +7 -0
  419. numpy/f2py/tests/src/string/gh25286.f90 +14 -0
  420. numpy/f2py/tests/src/string/gh25286.pyf +12 -0
  421. numpy/f2py/tests/src/string/gh25286_bc.pyf +12 -0
  422. numpy/f2py/tests/src/string/scalar_string.f90 +9 -0
  423. numpy/f2py/tests/src/string/string.f +12 -0
  424. numpy/f2py/tests/src/value_attrspec/gh21665.f90 +9 -0
  425. numpy/f2py/tests/test_abstract_interface.py +26 -0
  426. numpy/f2py/tests/test_array_from_pyobj.py +678 -0
  427. numpy/f2py/tests/test_assumed_shape.py +50 -0
  428. numpy/f2py/tests/test_block_docstring.py +20 -0
  429. numpy/f2py/tests/test_callback.py +263 -0
  430. numpy/f2py/tests/test_character.py +641 -0
  431. numpy/f2py/tests/test_common.py +23 -0
  432. numpy/f2py/tests/test_crackfortran.py +421 -0
  433. numpy/f2py/tests/test_data.py +71 -0
  434. numpy/f2py/tests/test_docs.py +64 -0
  435. numpy/f2py/tests/test_f2cmap.py +17 -0
  436. numpy/f2py/tests/test_f2py2e.py +964 -0
  437. numpy/f2py/tests/test_isoc.py +56 -0
  438. numpy/f2py/tests/test_kind.py +53 -0
  439. numpy/f2py/tests/test_mixed.py +35 -0
  440. numpy/f2py/tests/test_modules.py +83 -0
  441. numpy/f2py/tests/test_parameter.py +129 -0
  442. numpy/f2py/tests/test_pyf_src.py +43 -0
  443. numpy/f2py/tests/test_quoted_character.py +18 -0
  444. numpy/f2py/tests/test_regression.py +187 -0
  445. numpy/f2py/tests/test_return_character.py +48 -0
  446. numpy/f2py/tests/test_return_complex.py +67 -0
  447. numpy/f2py/tests/test_return_integer.py +55 -0
  448. numpy/f2py/tests/test_return_logical.py +65 -0
  449. numpy/f2py/tests/test_return_real.py +109 -0
  450. numpy/f2py/tests/test_routines.py +29 -0
  451. numpy/f2py/tests/test_semicolon_split.py +75 -0
  452. numpy/f2py/tests/test_size.py +45 -0
  453. numpy/f2py/tests/test_string.py +100 -0
  454. numpy/f2py/tests/test_symbolic.py +495 -0
  455. numpy/f2py/tests/test_value_attrspec.py +15 -0
  456. numpy/f2py/tests/util.py +442 -0
  457. numpy/f2py/use_rules.py +99 -0
  458. numpy/f2py/use_rules.pyi +9 -0
  459. numpy/fft/__init__.py +215 -0
  460. numpy/fft/__init__.pyi +43 -0
  461. numpy/fft/_helper.py +235 -0
  462. numpy/fft/_helper.pyi +45 -0
  463. numpy/fft/_pocketfft.py +1693 -0
  464. numpy/fft/_pocketfft.pyi +138 -0
  465. numpy/fft/_pocketfft_umath.cpython-313-darwin.so +0 -0
  466. numpy/fft/helper.py +17 -0
  467. numpy/fft/helper.pyi +22 -0
  468. numpy/fft/tests/__init__.py +0 -0
  469. numpy/fft/tests/test_helper.py +167 -0
  470. numpy/fft/tests/test_pocketfft.py +589 -0
  471. numpy/lib/__init__.py +97 -0
  472. numpy/lib/__init__.pyi +44 -0
  473. numpy/lib/_array_utils_impl.py +62 -0
  474. numpy/lib/_array_utils_impl.pyi +26 -0
  475. numpy/lib/_arraypad_impl.py +890 -0
  476. numpy/lib/_arraypad_impl.pyi +89 -0
  477. numpy/lib/_arraysetops_impl.py +1260 -0
  478. numpy/lib/_arraysetops_impl.pyi +468 -0
  479. numpy/lib/_arrayterator_impl.py +224 -0
  480. numpy/lib/_arrayterator_impl.pyi +46 -0
  481. numpy/lib/_datasource.py +700 -0
  482. numpy/lib/_datasource.pyi +31 -0
  483. numpy/lib/_format_impl.py +1036 -0
  484. numpy/lib/_format_impl.pyi +26 -0
  485. numpy/lib/_function_base_impl.py +5844 -0
  486. numpy/lib/_function_base_impl.pyi +1164 -0
  487. numpy/lib/_histograms_impl.py +1085 -0
  488. numpy/lib/_histograms_impl.pyi +50 -0
  489. numpy/lib/_index_tricks_impl.py +1067 -0
  490. numpy/lib/_index_tricks_impl.pyi +208 -0
  491. numpy/lib/_iotools.py +900 -0
  492. numpy/lib/_iotools.pyi +114 -0
  493. numpy/lib/_nanfunctions_impl.py +2024 -0
  494. numpy/lib/_nanfunctions_impl.pyi +52 -0
  495. numpy/lib/_npyio_impl.py +2596 -0
  496. numpy/lib/_npyio_impl.pyi +301 -0
  497. numpy/lib/_polynomial_impl.py +1465 -0
  498. numpy/lib/_polynomial_impl.pyi +318 -0
  499. numpy/lib/_scimath_impl.py +642 -0
  500. numpy/lib/_scimath_impl.pyi +93 -0
  501. numpy/lib/_shape_base_impl.py +1301 -0
  502. numpy/lib/_shape_base_impl.pyi +235 -0
  503. numpy/lib/_stride_tricks_impl.py +549 -0
  504. numpy/lib/_stride_tricks_impl.pyi +74 -0
  505. numpy/lib/_twodim_base_impl.py +1201 -0
  506. numpy/lib/_twodim_base_impl.pyi +438 -0
  507. numpy/lib/_type_check_impl.py +699 -0
  508. numpy/lib/_type_check_impl.pyi +350 -0
  509. numpy/lib/_ufunclike_impl.py +207 -0
  510. numpy/lib/_ufunclike_impl.pyi +67 -0
  511. numpy/lib/_user_array_impl.py +299 -0
  512. numpy/lib/_user_array_impl.pyi +225 -0
  513. numpy/lib/_utils_impl.py +784 -0
  514. numpy/lib/_utils_impl.pyi +10 -0
  515. numpy/lib/_version.py +154 -0
  516. numpy/lib/_version.pyi +17 -0
  517. numpy/lib/array_utils.py +7 -0
  518. numpy/lib/array_utils.pyi +12 -0
  519. numpy/lib/format.py +24 -0
  520. numpy/lib/format.pyi +66 -0
  521. numpy/lib/introspect.py +95 -0
  522. numpy/lib/introspect.pyi +3 -0
  523. numpy/lib/mixins.py +180 -0
  524. numpy/lib/mixins.pyi +77 -0
  525. numpy/lib/npyio.py +1 -0
  526. numpy/lib/npyio.pyi +9 -0
  527. numpy/lib/recfunctions.py +1681 -0
  528. numpy/lib/recfunctions.pyi +435 -0
  529. numpy/lib/scimath.py +13 -0
  530. numpy/lib/scimath.pyi +30 -0
  531. numpy/lib/stride_tricks.py +1 -0
  532. numpy/lib/stride_tricks.pyi +6 -0
  533. numpy/lib/tests/__init__.py +0 -0
  534. numpy/lib/tests/data/py2-np0-objarr.npy +0 -0
  535. numpy/lib/tests/data/py2-objarr.npy +0 -0
  536. numpy/lib/tests/data/py2-objarr.npz +0 -0
  537. numpy/lib/tests/data/py3-objarr.npy +0 -0
  538. numpy/lib/tests/data/py3-objarr.npz +0 -0
  539. numpy/lib/tests/data/python3.npy +0 -0
  540. numpy/lib/tests/data/win64python2.npy +0 -0
  541. numpy/lib/tests/test__datasource.py +352 -0
  542. numpy/lib/tests/test__iotools.py +360 -0
  543. numpy/lib/tests/test__version.py +64 -0
  544. numpy/lib/tests/test_array_utils.py +32 -0
  545. numpy/lib/tests/test_arraypad.py +1415 -0
  546. numpy/lib/tests/test_arraysetops.py +1074 -0
  547. numpy/lib/tests/test_arrayterator.py +46 -0
  548. numpy/lib/tests/test_format.py +1054 -0
  549. numpy/lib/tests/test_function_base.py +4573 -0
  550. numpy/lib/tests/test_histograms.py +855 -0
  551. numpy/lib/tests/test_index_tricks.py +573 -0
  552. numpy/lib/tests/test_io.py +2848 -0
  553. numpy/lib/tests/test_loadtxt.py +1101 -0
  554. numpy/lib/tests/test_mixins.py +215 -0
  555. numpy/lib/tests/test_nanfunctions.py +1438 -0
  556. numpy/lib/tests/test_packbits.py +376 -0
  557. numpy/lib/tests/test_polynomial.py +320 -0
  558. numpy/lib/tests/test_recfunctions.py +1052 -0
  559. numpy/lib/tests/test_regression.py +231 -0
  560. numpy/lib/tests/test_shape_base.py +813 -0
  561. numpy/lib/tests/test_stride_tricks.py +656 -0
  562. numpy/lib/tests/test_twodim_base.py +559 -0
  563. numpy/lib/tests/test_type_check.py +473 -0
  564. numpy/lib/tests/test_ufunclike.py +97 -0
  565. numpy/lib/tests/test_utils.py +80 -0
  566. numpy/lib/user_array.py +1 -0
  567. numpy/lib/user_array.pyi +1 -0
  568. numpy/linalg/__init__.py +98 -0
  569. numpy/linalg/__init__.pyi +73 -0
  570. numpy/linalg/_linalg.py +3682 -0
  571. numpy/linalg/_linalg.pyi +475 -0
  572. numpy/linalg/_umath_linalg.cpython-313-darwin.so +0 -0
  573. numpy/linalg/_umath_linalg.pyi +61 -0
  574. numpy/linalg/lapack_lite.cpython-313-darwin.so +0 -0
  575. numpy/linalg/lapack_lite.pyi +141 -0
  576. numpy/linalg/linalg.py +17 -0
  577. numpy/linalg/linalg.pyi +69 -0
  578. numpy/linalg/tests/__init__.py +0 -0
  579. numpy/linalg/tests/test_deprecations.py +20 -0
  580. numpy/linalg/tests/test_linalg.py +2443 -0
  581. numpy/linalg/tests/test_regression.py +181 -0
  582. numpy/ma/API_CHANGES.txt +135 -0
  583. numpy/ma/LICENSE +24 -0
  584. numpy/ma/README.rst +236 -0
  585. numpy/ma/__init__.py +53 -0
  586. numpy/ma/__init__.pyi +458 -0
  587. numpy/ma/core.py +8933 -0
  588. numpy/ma/core.pyi +1462 -0
  589. numpy/ma/extras.py +2344 -0
  590. numpy/ma/extras.pyi +138 -0
  591. numpy/ma/mrecords.py +773 -0
  592. numpy/ma/mrecords.pyi +96 -0
  593. numpy/ma/tests/__init__.py +0 -0
  594. numpy/ma/tests/test_arrayobject.py +40 -0
  595. numpy/ma/tests/test_core.py +5886 -0
  596. numpy/ma/tests/test_deprecations.py +87 -0
  597. numpy/ma/tests/test_extras.py +1998 -0
  598. numpy/ma/tests/test_mrecords.py +497 -0
  599. numpy/ma/tests/test_old_ma.py +942 -0
  600. numpy/ma/tests/test_regression.py +100 -0
  601. numpy/ma/tests/test_subclassing.py +469 -0
  602. numpy/ma/testutils.py +294 -0
  603. numpy/matlib.py +380 -0
  604. numpy/matlib.pyi +582 -0
  605. numpy/matrixlib/__init__.py +12 -0
  606. numpy/matrixlib/__init__.pyi +5 -0
  607. numpy/matrixlib/defmatrix.py +1119 -0
  608. numpy/matrixlib/defmatrix.pyi +17 -0
  609. numpy/matrixlib/tests/__init__.py +0 -0
  610. numpy/matrixlib/tests/test_defmatrix.py +455 -0
  611. numpy/matrixlib/tests/test_interaction.py +360 -0
  612. numpy/matrixlib/tests/test_masked_matrix.py +240 -0
  613. numpy/matrixlib/tests/test_matrix_linalg.py +105 -0
  614. numpy/matrixlib/tests/test_multiarray.py +17 -0
  615. numpy/matrixlib/tests/test_numeric.py +18 -0
  616. numpy/matrixlib/tests/test_regression.py +31 -0
  617. numpy/polynomial/__init__.py +187 -0
  618. numpy/polynomial/__init__.pyi +25 -0
  619. numpy/polynomial/_polybase.py +1191 -0
  620. numpy/polynomial/_polybase.pyi +285 -0
  621. numpy/polynomial/_polytypes.pyi +892 -0
  622. numpy/polynomial/chebyshev.py +2003 -0
  623. numpy/polynomial/chebyshev.pyi +181 -0
  624. numpy/polynomial/hermite.py +1740 -0
  625. numpy/polynomial/hermite.pyi +107 -0
  626. numpy/polynomial/hermite_e.py +1642 -0
  627. numpy/polynomial/hermite_e.pyi +107 -0
  628. numpy/polynomial/laguerre.py +1675 -0
  629. numpy/polynomial/laguerre.pyi +100 -0
  630. numpy/polynomial/legendre.py +1605 -0
  631. numpy/polynomial/legendre.pyi +100 -0
  632. numpy/polynomial/polynomial.py +1616 -0
  633. numpy/polynomial/polynomial.pyi +89 -0
  634. numpy/polynomial/polyutils.py +759 -0
  635. numpy/polynomial/polyutils.pyi +423 -0
  636. numpy/polynomial/tests/__init__.py +0 -0
  637. numpy/polynomial/tests/test_chebyshev.py +623 -0
  638. numpy/polynomial/tests/test_classes.py +618 -0
  639. numpy/polynomial/tests/test_hermite.py +558 -0
  640. numpy/polynomial/tests/test_hermite_e.py +559 -0
  641. numpy/polynomial/tests/test_laguerre.py +540 -0
  642. numpy/polynomial/tests/test_legendre.py +571 -0
  643. numpy/polynomial/tests/test_polynomial.py +669 -0
  644. numpy/polynomial/tests/test_polyutils.py +128 -0
  645. numpy/polynomial/tests/test_printing.py +555 -0
  646. numpy/polynomial/tests/test_symbol.py +217 -0
  647. numpy/py.typed +0 -0
  648. numpy/random/LICENSE.md +71 -0
  649. numpy/random/__init__.pxd +14 -0
  650. numpy/random/__init__.py +213 -0
  651. numpy/random/__init__.pyi +124 -0
  652. numpy/random/_bounded_integers.cpython-313-darwin.so +0 -0
  653. numpy/random/_bounded_integers.pxd +29 -0
  654. numpy/random/_bounded_integers.pyi +1 -0
  655. numpy/random/_common.cpython-313-darwin.so +0 -0
  656. numpy/random/_common.pxd +107 -0
  657. numpy/random/_common.pyi +16 -0
  658. numpy/random/_examples/cffi/extending.py +44 -0
  659. numpy/random/_examples/cffi/parse.py +53 -0
  660. numpy/random/_examples/cython/extending.pyx +77 -0
  661. numpy/random/_examples/cython/extending_distributions.pyx +118 -0
  662. numpy/random/_examples/cython/meson.build +53 -0
  663. numpy/random/_examples/numba/extending.py +86 -0
  664. numpy/random/_examples/numba/extending_distributions.py +67 -0
  665. numpy/random/_generator.cpython-313-darwin.so +0 -0
  666. numpy/random/_generator.pyi +861 -0
  667. numpy/random/_mt19937.cpython-313-darwin.so +0 -0
  668. numpy/random/_mt19937.pyi +25 -0
  669. numpy/random/_pcg64.cpython-313-darwin.so +0 -0
  670. numpy/random/_pcg64.pyi +44 -0
  671. numpy/random/_philox.cpython-313-darwin.so +0 -0
  672. numpy/random/_philox.pyi +39 -0
  673. numpy/random/_pickle.py +88 -0
  674. numpy/random/_pickle.pyi +43 -0
  675. numpy/random/_sfc64.cpython-313-darwin.so +0 -0
  676. numpy/random/_sfc64.pyi +28 -0
  677. numpy/random/bit_generator.cpython-313-darwin.so +0 -0
  678. numpy/random/bit_generator.pxd +35 -0
  679. numpy/random/bit_generator.pyi +124 -0
  680. numpy/random/c_distributions.pxd +119 -0
  681. numpy/random/lib/libnpyrandom.a +0 -0
  682. numpy/random/mtrand.cpython-313-darwin.so +0 -0
  683. numpy/random/mtrand.pyi +703 -0
  684. numpy/random/tests/__init__.py +0 -0
  685. numpy/random/tests/data/__init__.py +0 -0
  686. numpy/random/tests/data/generator_pcg64_np121.pkl.gz +0 -0
  687. numpy/random/tests/data/generator_pcg64_np126.pkl.gz +0 -0
  688. numpy/random/tests/data/mt19937-testset-1.csv +1001 -0
  689. numpy/random/tests/data/mt19937-testset-2.csv +1001 -0
  690. numpy/random/tests/data/pcg64-testset-1.csv +1001 -0
  691. numpy/random/tests/data/pcg64-testset-2.csv +1001 -0
  692. numpy/random/tests/data/pcg64dxsm-testset-1.csv +1001 -0
  693. numpy/random/tests/data/pcg64dxsm-testset-2.csv +1001 -0
  694. numpy/random/tests/data/philox-testset-1.csv +1001 -0
  695. numpy/random/tests/data/philox-testset-2.csv +1001 -0
  696. numpy/random/tests/data/sfc64-testset-1.csv +1001 -0
  697. numpy/random/tests/data/sfc64-testset-2.csv +1001 -0
  698. numpy/random/tests/data/sfc64_np126.pkl.gz +0 -0
  699. numpy/random/tests/test_direct.py +592 -0
  700. numpy/random/tests/test_extending.py +127 -0
  701. numpy/random/tests/test_generator_mt19937.py +2809 -0
  702. numpy/random/tests/test_generator_mt19937_regressions.py +207 -0
  703. numpy/random/tests/test_random.py +1757 -0
  704. numpy/random/tests/test_randomstate.py +2130 -0
  705. numpy/random/tests/test_randomstate_regression.py +217 -0
  706. numpy/random/tests/test_regression.py +152 -0
  707. numpy/random/tests/test_seed_sequence.py +79 -0
  708. numpy/random/tests/test_smoke.py +819 -0
  709. numpy/rec/__init__.py +2 -0
  710. numpy/rec/__init__.pyi +23 -0
  711. numpy/strings/__init__.py +2 -0
  712. numpy/strings/__init__.pyi +97 -0
  713. numpy/testing/__init__.py +22 -0
  714. numpy/testing/__init__.pyi +102 -0
  715. numpy/testing/_private/__init__.py +0 -0
  716. numpy/testing/_private/__init__.pyi +0 -0
  717. numpy/testing/_private/extbuild.py +250 -0
  718. numpy/testing/_private/extbuild.pyi +25 -0
  719. numpy/testing/_private/utils.py +2752 -0
  720. numpy/testing/_private/utils.pyi +499 -0
  721. numpy/testing/overrides.py +84 -0
  722. numpy/testing/overrides.pyi +11 -0
  723. numpy/testing/print_coercion_tables.py +207 -0
  724. numpy/testing/print_coercion_tables.pyi +27 -0
  725. numpy/testing/tests/__init__.py +0 -0
  726. numpy/testing/tests/test_utils.py +1917 -0
  727. numpy/tests/__init__.py +0 -0
  728. numpy/tests/test__all__.py +10 -0
  729. numpy/tests/test_configtool.py +48 -0
  730. numpy/tests/test_ctypeslib.py +377 -0
  731. numpy/tests/test_lazyloading.py +38 -0
  732. numpy/tests/test_matlib.py +59 -0
  733. numpy/tests/test_numpy_config.py +46 -0
  734. numpy/tests/test_numpy_version.py +54 -0
  735. numpy/tests/test_public_api.py +806 -0
  736. numpy/tests/test_reloading.py +74 -0
  737. numpy/tests/test_scripts.py +49 -0
  738. numpy/tests/test_warnings.py +78 -0
  739. numpy/typing/__init__.py +201 -0
  740. numpy/typing/mypy_plugin.py +195 -0
  741. numpy/typing/tests/__init__.py +0 -0
  742. numpy/typing/tests/data/fail/arithmetic.pyi +126 -0
  743. numpy/typing/tests/data/fail/array_constructors.pyi +34 -0
  744. numpy/typing/tests/data/fail/array_like.pyi +15 -0
  745. numpy/typing/tests/data/fail/array_pad.pyi +6 -0
  746. numpy/typing/tests/data/fail/arrayprint.pyi +16 -0
  747. numpy/typing/tests/data/fail/arrayterator.pyi +14 -0
  748. numpy/typing/tests/data/fail/bitwise_ops.pyi +17 -0
  749. numpy/typing/tests/data/fail/char.pyi +65 -0
  750. numpy/typing/tests/data/fail/chararray.pyi +62 -0
  751. numpy/typing/tests/data/fail/comparisons.pyi +27 -0
  752. numpy/typing/tests/data/fail/constants.pyi +3 -0
  753. numpy/typing/tests/data/fail/datasource.pyi +15 -0
  754. numpy/typing/tests/data/fail/dtype.pyi +17 -0
  755. numpy/typing/tests/data/fail/einsumfunc.pyi +12 -0
  756. numpy/typing/tests/data/fail/flatiter.pyi +20 -0
  757. numpy/typing/tests/data/fail/fromnumeric.pyi +148 -0
  758. numpy/typing/tests/data/fail/histograms.pyi +12 -0
  759. numpy/typing/tests/data/fail/index_tricks.pyi +14 -0
  760. numpy/typing/tests/data/fail/lib_function_base.pyi +62 -0
  761. numpy/typing/tests/data/fail/lib_polynomial.pyi +29 -0
  762. numpy/typing/tests/data/fail/lib_utils.pyi +3 -0
  763. numpy/typing/tests/data/fail/lib_version.pyi +6 -0
  764. numpy/typing/tests/data/fail/linalg.pyi +48 -0
  765. numpy/typing/tests/data/fail/ma.pyi +143 -0
  766. numpy/typing/tests/data/fail/memmap.pyi +5 -0
  767. numpy/typing/tests/data/fail/modules.pyi +17 -0
  768. numpy/typing/tests/data/fail/multiarray.pyi +52 -0
  769. numpy/typing/tests/data/fail/ndarray.pyi +11 -0
  770. numpy/typing/tests/data/fail/ndarray_misc.pyi +36 -0
  771. numpy/typing/tests/data/fail/nditer.pyi +8 -0
  772. numpy/typing/tests/data/fail/nested_sequence.pyi +16 -0
  773. numpy/typing/tests/data/fail/npyio.pyi +24 -0
  774. numpy/typing/tests/data/fail/numerictypes.pyi +5 -0
  775. numpy/typing/tests/data/fail/random.pyi +62 -0
  776. numpy/typing/tests/data/fail/rec.pyi +17 -0
  777. numpy/typing/tests/data/fail/scalars.pyi +87 -0
  778. numpy/typing/tests/data/fail/shape.pyi +6 -0
  779. numpy/typing/tests/data/fail/shape_base.pyi +8 -0
  780. numpy/typing/tests/data/fail/stride_tricks.pyi +9 -0
  781. numpy/typing/tests/data/fail/strings.pyi +52 -0
  782. numpy/typing/tests/data/fail/testing.pyi +28 -0
  783. numpy/typing/tests/data/fail/twodim_base.pyi +32 -0
  784. numpy/typing/tests/data/fail/type_check.pyi +13 -0
  785. numpy/typing/tests/data/fail/ufunc_config.pyi +21 -0
  786. numpy/typing/tests/data/fail/ufunclike.pyi +21 -0
  787. numpy/typing/tests/data/fail/ufuncs.pyi +17 -0
  788. numpy/typing/tests/data/fail/warnings_and_errors.pyi +5 -0
  789. numpy/typing/tests/data/misc/extended_precision.pyi +9 -0
  790. numpy/typing/tests/data/mypy.ini +9 -0
  791. numpy/typing/tests/data/pass/arithmetic.py +612 -0
  792. numpy/typing/tests/data/pass/array_constructors.py +137 -0
  793. numpy/typing/tests/data/pass/array_like.py +43 -0
  794. numpy/typing/tests/data/pass/arrayprint.py +37 -0
  795. numpy/typing/tests/data/pass/arrayterator.py +27 -0
  796. numpy/typing/tests/data/pass/bitwise_ops.py +131 -0
  797. numpy/typing/tests/data/pass/comparisons.py +315 -0
  798. numpy/typing/tests/data/pass/dtype.py +57 -0
  799. numpy/typing/tests/data/pass/einsumfunc.py +36 -0
  800. numpy/typing/tests/data/pass/flatiter.py +19 -0
  801. numpy/typing/tests/data/pass/fromnumeric.py +272 -0
  802. numpy/typing/tests/data/pass/index_tricks.py +60 -0
  803. numpy/typing/tests/data/pass/lib_user_array.py +22 -0
  804. numpy/typing/tests/data/pass/lib_utils.py +19 -0
  805. numpy/typing/tests/data/pass/lib_version.py +18 -0
  806. numpy/typing/tests/data/pass/literal.py +51 -0
  807. numpy/typing/tests/data/pass/ma.py +174 -0
  808. numpy/typing/tests/data/pass/mod.py +149 -0
  809. numpy/typing/tests/data/pass/modules.py +45 -0
  810. numpy/typing/tests/data/pass/multiarray.py +76 -0
  811. numpy/typing/tests/data/pass/ndarray_conversion.py +87 -0
  812. numpy/typing/tests/data/pass/ndarray_misc.py +203 -0
  813. numpy/typing/tests/data/pass/ndarray_shape_manipulation.py +47 -0
  814. numpy/typing/tests/data/pass/nditer.py +4 -0
  815. numpy/typing/tests/data/pass/numeric.py +95 -0
  816. numpy/typing/tests/data/pass/numerictypes.py +17 -0
  817. numpy/typing/tests/data/pass/random.py +1497 -0
  818. numpy/typing/tests/data/pass/recfunctions.py +161 -0
  819. numpy/typing/tests/data/pass/scalars.py +248 -0
  820. numpy/typing/tests/data/pass/shape.py +19 -0
  821. numpy/typing/tests/data/pass/simple.py +168 -0
  822. numpy/typing/tests/data/pass/simple_py3.py +6 -0
  823. numpy/typing/tests/data/pass/ufunc_config.py +64 -0
  824. numpy/typing/tests/data/pass/ufunclike.py +47 -0
  825. numpy/typing/tests/data/pass/ufuncs.py +16 -0
  826. numpy/typing/tests/data/pass/warnings_and_errors.py +6 -0
  827. numpy/typing/tests/data/reveal/arithmetic.pyi +720 -0
  828. numpy/typing/tests/data/reveal/array_api_info.pyi +70 -0
  829. numpy/typing/tests/data/reveal/array_constructors.pyi +249 -0
  830. numpy/typing/tests/data/reveal/arraypad.pyi +22 -0
  831. numpy/typing/tests/data/reveal/arrayprint.pyi +25 -0
  832. numpy/typing/tests/data/reveal/arraysetops.pyi +74 -0
  833. numpy/typing/tests/data/reveal/arrayterator.pyi +27 -0
  834. numpy/typing/tests/data/reveal/bitwise_ops.pyi +167 -0
  835. numpy/typing/tests/data/reveal/char.pyi +224 -0
  836. numpy/typing/tests/data/reveal/chararray.pyi +137 -0
  837. numpy/typing/tests/data/reveal/comparisons.pyi +264 -0
  838. numpy/typing/tests/data/reveal/constants.pyi +14 -0
  839. numpy/typing/tests/data/reveal/ctypeslib.pyi +81 -0
  840. numpy/typing/tests/data/reveal/datasource.pyi +23 -0
  841. numpy/typing/tests/data/reveal/dtype.pyi +136 -0
  842. numpy/typing/tests/data/reveal/einsumfunc.pyi +39 -0
  843. numpy/typing/tests/data/reveal/emath.pyi +54 -0
  844. numpy/typing/tests/data/reveal/fft.pyi +37 -0
  845. numpy/typing/tests/data/reveal/flatiter.pyi +47 -0
  846. numpy/typing/tests/data/reveal/fromnumeric.pyi +347 -0
  847. numpy/typing/tests/data/reveal/getlimits.pyi +51 -0
  848. numpy/typing/tests/data/reveal/histograms.pyi +25 -0
  849. numpy/typing/tests/data/reveal/index_tricks.pyi +70 -0
  850. numpy/typing/tests/data/reveal/lib_function_base.pyi +213 -0
  851. numpy/typing/tests/data/reveal/lib_polynomial.pyi +144 -0
  852. numpy/typing/tests/data/reveal/lib_utils.pyi +17 -0
  853. numpy/typing/tests/data/reveal/lib_version.pyi +20 -0
  854. numpy/typing/tests/data/reveal/linalg.pyi +132 -0
  855. numpy/typing/tests/data/reveal/ma.pyi +369 -0
  856. numpy/typing/tests/data/reveal/matrix.pyi +73 -0
  857. numpy/typing/tests/data/reveal/memmap.pyi +19 -0
  858. numpy/typing/tests/data/reveal/mod.pyi +179 -0
  859. numpy/typing/tests/data/reveal/modules.pyi +51 -0
  860. numpy/typing/tests/data/reveal/multiarray.pyi +194 -0
  861. numpy/typing/tests/data/reveal/nbit_base_example.pyi +21 -0
  862. numpy/typing/tests/data/reveal/ndarray_assignability.pyi +77 -0
  863. numpy/typing/tests/data/reveal/ndarray_conversion.pyi +85 -0
  864. numpy/typing/tests/data/reveal/ndarray_misc.pyi +247 -0
  865. numpy/typing/tests/data/reveal/ndarray_shape_manipulation.pyi +39 -0
  866. numpy/typing/tests/data/reveal/nditer.pyi +49 -0
  867. numpy/typing/tests/data/reveal/nested_sequence.pyi +25 -0
  868. numpy/typing/tests/data/reveal/npyio.pyi +83 -0
  869. numpy/typing/tests/data/reveal/numeric.pyi +134 -0
  870. numpy/typing/tests/data/reveal/numerictypes.pyi +16 -0
  871. numpy/typing/tests/data/reveal/polynomial_polybase.pyi +220 -0
  872. numpy/typing/tests/data/reveal/polynomial_polyutils.pyi +219 -0
  873. numpy/typing/tests/data/reveal/polynomial_series.pyi +138 -0
  874. numpy/typing/tests/data/reveal/random.pyi +1546 -0
  875. numpy/typing/tests/data/reveal/rec.pyi +171 -0
  876. numpy/typing/tests/data/reveal/scalars.pyi +191 -0
  877. numpy/typing/tests/data/reveal/shape.pyi +13 -0
  878. numpy/typing/tests/data/reveal/shape_base.pyi +52 -0
  879. numpy/typing/tests/data/reveal/stride_tricks.pyi +27 -0
  880. numpy/typing/tests/data/reveal/strings.pyi +196 -0
  881. numpy/typing/tests/data/reveal/testing.pyi +198 -0
  882. numpy/typing/tests/data/reveal/twodim_base.pyi +145 -0
  883. numpy/typing/tests/data/reveal/type_check.pyi +67 -0
  884. numpy/typing/tests/data/reveal/ufunc_config.pyi +30 -0
  885. numpy/typing/tests/data/reveal/ufunclike.pyi +31 -0
  886. numpy/typing/tests/data/reveal/ufuncs.pyi +123 -0
  887. numpy/typing/tests/data/reveal/warnings_and_errors.pyi +11 -0
  888. numpy/typing/tests/test_isfile.py +32 -0
  889. numpy/typing/tests/test_runtime.py +102 -0
  890. numpy/typing/tests/test_typing.py +205 -0
  891. numpy/version.py +11 -0
  892. numpy/version.pyi +18 -0
  893. numpy-2.3.5.dist-info/LICENSE.txt +971 -0
  894. numpy-2.3.5.dist-info/METADATA +1093 -0
  895. numpy-2.3.5.dist-info/RECORD +897 -0
  896. numpy-2.3.5.dist-info/WHEEL +6 -0
  897. numpy-2.3.5.dist-info/entry_points.txt +13 -0
@@ -0,0 +1,2734 @@
1
+ import datetime
2
+ import pickle
3
+
4
+ import pytest
5
+
6
+ import numpy
7
+ import numpy as np
8
+ from numpy.testing import (
9
+ IS_WASM,
10
+ assert_,
11
+ assert_array_equal,
12
+ assert_equal,
13
+ assert_raises,
14
+ assert_raises_regex,
15
+ assert_warns,
16
+ suppress_warnings,
17
+ )
18
+
19
+ # Use pytz to test out various time zones if available
20
+ try:
21
+ from pytz import timezone as tz
22
+ _has_pytz = True
23
+ except ImportError:
24
+ _has_pytz = False
25
+
26
+ try:
27
+ RecursionError
28
+ except NameError:
29
+ RecursionError = RuntimeError # python < 3.5
30
+
31
+
32
+ def _assert_equal_hash(v1, v2):
33
+ assert v1 == v2
34
+ assert hash(v1) == hash(v2)
35
+ assert v2 in {v1}
36
+
37
+
38
+ class TestDateTime:
39
+
40
+ def test_string(self):
41
+ msg = "no explicit representation of timezones available for " \
42
+ "np.datetime64"
43
+ with pytest.warns(UserWarning, match=msg):
44
+ np.datetime64('2000-01-01T00+01')
45
+
46
+ def test_datetime(self):
47
+ msg = "no explicit representation of timezones available for " \
48
+ "np.datetime64"
49
+ with pytest.warns(UserWarning, match=msg):
50
+ t0 = np.datetime64('2023-06-09T12:18:40Z', 'ns')
51
+
52
+ t0 = np.datetime64('2023-06-09T12:18:40', 'ns')
53
+
54
+ def test_datetime_dtype_creation(self):
55
+ for unit in ['Y', 'M', 'W', 'D',
56
+ 'h', 'm', 's', 'ms', 'us',
57
+ 'μs', # alias for us
58
+ 'ns', 'ps', 'fs', 'as']:
59
+ dt1 = np.dtype(f'M8[750{unit}]')
60
+ assert_(dt1 == np.dtype(f'datetime64[750{unit}]'))
61
+ dt2 = np.dtype(f'm8[{unit}]')
62
+ assert_(dt2 == np.dtype(f'timedelta64[{unit}]'))
63
+
64
+ # Generic units shouldn't add [] to the end
65
+ assert_equal(str(np.dtype("M8")), "datetime64")
66
+
67
+ # Should be possible to specify the endianness
68
+ assert_equal(np.dtype("=M8"), np.dtype("M8"))
69
+ assert_equal(np.dtype("=M8[s]"), np.dtype("M8[s]"))
70
+ assert_(np.dtype(">M8") == np.dtype("M8") or
71
+ np.dtype("<M8") == np.dtype("M8"))
72
+ assert_(np.dtype(">M8[D]") == np.dtype("M8[D]") or
73
+ np.dtype("<M8[D]") == np.dtype("M8[D]"))
74
+ assert_(np.dtype(">M8") != np.dtype("<M8"))
75
+
76
+ assert_equal(np.dtype("=m8"), np.dtype("m8"))
77
+ assert_equal(np.dtype("=m8[s]"), np.dtype("m8[s]"))
78
+ assert_(np.dtype(">m8") == np.dtype("m8") or
79
+ np.dtype("<m8") == np.dtype("m8"))
80
+ assert_(np.dtype(">m8[D]") == np.dtype("m8[D]") or
81
+ np.dtype("<m8[D]") == np.dtype("m8[D]"))
82
+ assert_(np.dtype(">m8") != np.dtype("<m8"))
83
+
84
+ # Check that the parser rejects bad datetime types
85
+ assert_raises(TypeError, np.dtype, 'M8[badunit]')
86
+ assert_raises(TypeError, np.dtype, 'm8[badunit]')
87
+ assert_raises(TypeError, np.dtype, 'M8[YY]')
88
+ assert_raises(TypeError, np.dtype, 'm8[YY]')
89
+ assert_raises(TypeError, np.dtype, 'm4')
90
+ assert_raises(TypeError, np.dtype, 'M7')
91
+ assert_raises(TypeError, np.dtype, 'm7')
92
+ assert_raises(TypeError, np.dtype, 'M16')
93
+ assert_raises(TypeError, np.dtype, 'm16')
94
+ assert_raises(TypeError, np.dtype, 'M8[3000000000ps]')
95
+
96
+ def test_datetime_casting_rules(self):
97
+ # Cannot cast safely/same_kind between timedelta and datetime
98
+ assert_(not np.can_cast('m8', 'M8', casting='same_kind'))
99
+ assert_(not np.can_cast('M8', 'm8', casting='same_kind'))
100
+ assert_(not np.can_cast('m8', 'M8', casting='safe'))
101
+ assert_(not np.can_cast('M8', 'm8', casting='safe'))
102
+
103
+ # Can cast safely/same_kind from integer to timedelta
104
+ assert_(np.can_cast('i8', 'm8', casting='same_kind'))
105
+ assert_(np.can_cast('i8', 'm8', casting='safe'))
106
+ assert_(np.can_cast('i4', 'm8', casting='same_kind'))
107
+ assert_(np.can_cast('i4', 'm8', casting='safe'))
108
+ assert_(np.can_cast('u4', 'm8', casting='same_kind'))
109
+ assert_(np.can_cast('u4', 'm8', casting='safe'))
110
+
111
+ # Cannot cast safely from unsigned integer of the same size, which
112
+ # could overflow
113
+ assert_(np.can_cast('u8', 'm8', casting='same_kind'))
114
+ assert_(not np.can_cast('u8', 'm8', casting='safe'))
115
+
116
+ # Cannot cast safely/same_kind from float to timedelta
117
+ assert_(not np.can_cast('f4', 'm8', casting='same_kind'))
118
+ assert_(not np.can_cast('f4', 'm8', casting='safe'))
119
+
120
+ # Cannot cast safely/same_kind from integer to datetime
121
+ assert_(not np.can_cast('i8', 'M8', casting='same_kind'))
122
+ assert_(not np.can_cast('i8', 'M8', casting='safe'))
123
+
124
+ # Cannot cast safely/same_kind from bool to datetime
125
+ assert_(not np.can_cast('b1', 'M8', casting='same_kind'))
126
+ assert_(not np.can_cast('b1', 'M8', casting='safe'))
127
+ # Can cast safely/same_kind from bool to timedelta
128
+ assert_(np.can_cast('b1', 'm8', casting='same_kind'))
129
+ assert_(np.can_cast('b1', 'm8', casting='safe'))
130
+
131
+ # Can cast datetime safely from months/years to days
132
+ assert_(np.can_cast('M8[M]', 'M8[D]', casting='safe'))
133
+ assert_(np.can_cast('M8[Y]', 'M8[D]', casting='safe'))
134
+ # Cannot cast timedelta safely from months/years to days
135
+ assert_(not np.can_cast('m8[M]', 'm8[D]', casting='safe'))
136
+ assert_(not np.can_cast('m8[Y]', 'm8[D]', casting='safe'))
137
+ # Can cast datetime same_kind from months/years to days
138
+ assert_(np.can_cast('M8[M]', 'M8[D]', casting='same_kind'))
139
+ assert_(np.can_cast('M8[Y]', 'M8[D]', casting='same_kind'))
140
+ # Can't cast timedelta same_kind from months/years to days
141
+ assert_(not np.can_cast('m8[M]', 'm8[D]', casting='same_kind'))
142
+ assert_(not np.can_cast('m8[Y]', 'm8[D]', casting='same_kind'))
143
+ # Can cast datetime same_kind across the date/time boundary
144
+ assert_(np.can_cast('M8[D]', 'M8[h]', casting='same_kind'))
145
+ # Can cast timedelta same_kind across the date/time boundary
146
+ assert_(np.can_cast('m8[D]', 'm8[h]', casting='same_kind'))
147
+ assert_(np.can_cast('m8[h]', 'm8[D]', casting='same_kind'))
148
+
149
+ # Cannot cast safely if the integer multiplier doesn't divide
150
+ assert_(not np.can_cast('M8[7h]', 'M8[3h]', casting='safe'))
151
+ assert_(not np.can_cast('M8[3h]', 'M8[6h]', casting='safe'))
152
+ # But can cast same_kind
153
+ assert_(np.can_cast('M8[7h]', 'M8[3h]', casting='same_kind'))
154
+ # Can cast safely if the integer multiplier does divide
155
+ assert_(np.can_cast('M8[6h]', 'M8[3h]', casting='safe'))
156
+
157
+ # We can always cast types with generic units (corresponding to NaT) to
158
+ # more specific types
159
+ assert_(np.can_cast('m8', 'm8[h]', casting='same_kind'))
160
+ assert_(np.can_cast('m8', 'm8[h]', casting='safe'))
161
+ assert_(np.can_cast('M8', 'M8[h]', casting='same_kind'))
162
+ assert_(np.can_cast('M8', 'M8[h]', casting='safe'))
163
+ # but not the other way around
164
+ assert_(not np.can_cast('m8[h]', 'm8', casting='same_kind'))
165
+ assert_(not np.can_cast('m8[h]', 'm8', casting='safe'))
166
+ assert_(not np.can_cast('M8[h]', 'M8', casting='same_kind'))
167
+ assert_(not np.can_cast('M8[h]', 'M8', casting='safe'))
168
+
169
+ def test_datetime_prefix_conversions(self):
170
+ # regression tests related to gh-19631;
171
+ # test metric prefixes from seconds down to
172
+ # attoseconds for bidirectional conversions
173
+ smaller_units = ['M8[7000ms]',
174
+ 'M8[2000us]',
175
+ 'M8[1000ns]',
176
+ 'M8[5000ns]',
177
+ 'M8[2000ps]',
178
+ 'M8[9000fs]',
179
+ 'M8[1000as]',
180
+ 'M8[2000000ps]',
181
+ 'M8[1000000as]',
182
+ 'M8[2000000000ps]',
183
+ 'M8[1000000000as]']
184
+ larger_units = ['M8[7s]',
185
+ 'M8[2ms]',
186
+ 'M8[us]',
187
+ 'M8[5us]',
188
+ 'M8[2ns]',
189
+ 'M8[9ps]',
190
+ 'M8[1fs]',
191
+ 'M8[2us]',
192
+ 'M8[1ps]',
193
+ 'M8[2ms]',
194
+ 'M8[1ns]']
195
+ for larger_unit, smaller_unit in zip(larger_units, smaller_units):
196
+ assert np.can_cast(larger_unit, smaller_unit, casting='safe')
197
+ assert np.can_cast(smaller_unit, larger_unit, casting='safe')
198
+
199
+ @pytest.mark.parametrize("unit", [
200
+ "s", "ms", "us", "ns", "ps", "fs", "as"])
201
+ def test_prohibit_negative_datetime(self, unit):
202
+ with assert_raises(TypeError):
203
+ np.array([1], dtype=f"M8[-1{unit}]")
204
+
205
+ def test_compare_generic_nat(self):
206
+ # regression tests for gh-6452
207
+ assert_(np.datetime64('NaT') !=
208
+ np.datetime64('2000') + np.timedelta64('NaT'))
209
+ assert_(np.datetime64('NaT') != np.datetime64('NaT', 'us'))
210
+ assert_(np.datetime64('NaT', 'us') != np.datetime64('NaT'))
211
+
212
+ @pytest.mark.parametrize("size", [
213
+ 3, 21, 217, 1000])
214
+ def test_datetime_nat_argsort_stability(self, size):
215
+ # NaT < NaT should be False internally for
216
+ # sort stability
217
+ expected = np.arange(size)
218
+ arr = np.tile(np.datetime64('NaT'), size)
219
+ assert_equal(np.argsort(arr, kind='mergesort'), expected)
220
+
221
+ @pytest.mark.parametrize("size", [
222
+ 3, 21, 217, 1000])
223
+ def test_timedelta_nat_argsort_stability(self, size):
224
+ # NaT < NaT should be False internally for
225
+ # sort stability
226
+ expected = np.arange(size)
227
+ arr = np.tile(np.timedelta64('NaT'), size)
228
+ assert_equal(np.argsort(arr, kind='mergesort'), expected)
229
+
230
+ @pytest.mark.parametrize("arr, expected", [
231
+ # the example provided in gh-12629
232
+ (['NaT', 1, 2, 3],
233
+ [1, 2, 3, 'NaT']),
234
+ # multiple NaTs
235
+ (['NaT', 9, 'NaT', -707],
236
+ [-707, 9, 'NaT', 'NaT']),
237
+ # this sort explores another code path for NaT
238
+ ([1, -2, 3, 'NaT'],
239
+ [-2, 1, 3, 'NaT']),
240
+ # 2-D array
241
+ ([[51, -220, 'NaT'],
242
+ [-17, 'NaT', -90]],
243
+ [[-220, 51, 'NaT'],
244
+ [-90, -17, 'NaT']]),
245
+ ])
246
+ @pytest.mark.parametrize("dtype", [
247
+ 'M8[ns]', 'M8[us]',
248
+ 'm8[ns]', 'm8[us]'])
249
+ def test_datetime_timedelta_sort_nat(self, arr, expected, dtype):
250
+ # fix for gh-12629 and gh-15063; NaT sorting to end of array
251
+ arr = np.array(arr, dtype=dtype)
252
+ expected = np.array(expected, dtype=dtype)
253
+ arr.sort()
254
+ assert_equal(arr, expected)
255
+
256
+ def test_datetime_scalar_construction(self):
257
+ # Construct with different units
258
+ assert_equal(np.datetime64('1950-03-12', 'D'),
259
+ np.datetime64('1950-03-12'))
260
+ assert_equal(np.datetime64('1950-03-12T13', 's'),
261
+ np.datetime64('1950-03-12T13', 'm'))
262
+
263
+ # Default construction means NaT
264
+ assert_equal(np.datetime64(), np.datetime64('NaT'))
265
+
266
+ # Some basic strings and repr
267
+ assert_equal(str(np.datetime64('NaT')), 'NaT')
268
+ assert_equal(repr(np.datetime64('NaT')),
269
+ "np.datetime64('NaT')")
270
+ assert_equal(str(np.datetime64('2011-02')), '2011-02')
271
+ assert_equal(repr(np.datetime64('2011-02')),
272
+ "np.datetime64('2011-02')")
273
+
274
+ # None gets constructed as NaT
275
+ assert_equal(np.datetime64(None), np.datetime64('NaT'))
276
+
277
+ # Default construction of NaT is in generic units
278
+ assert_equal(np.datetime64().dtype, np.dtype('M8'))
279
+ assert_equal(np.datetime64('NaT').dtype, np.dtype('M8'))
280
+
281
+ # Construction from integers requires a specified unit
282
+ assert_raises(ValueError, np.datetime64, 17)
283
+
284
+ # When constructing from a scalar or zero-dimensional array,
285
+ # it either keeps the units or you can override them.
286
+ a = np.datetime64('2000-03-18T16', 'h')
287
+ b = np.array('2000-03-18T16', dtype='M8[h]')
288
+
289
+ assert_equal(a.dtype, np.dtype('M8[h]'))
290
+ assert_equal(b.dtype, np.dtype('M8[h]'))
291
+
292
+ assert_equal(np.datetime64(a), a)
293
+ assert_equal(np.datetime64(a).dtype, np.dtype('M8[h]'))
294
+
295
+ assert_equal(np.datetime64(b), a)
296
+ assert_equal(np.datetime64(b).dtype, np.dtype('M8[h]'))
297
+
298
+ assert_equal(np.datetime64(a, 's'), a)
299
+ assert_equal(np.datetime64(a, 's').dtype, np.dtype('M8[s]'))
300
+
301
+ assert_equal(np.datetime64(b, 's'), a)
302
+ assert_equal(np.datetime64(b, 's').dtype, np.dtype('M8[s]'))
303
+
304
+ # Construction from datetime.date
305
+ assert_equal(np.datetime64('1945-03-25'),
306
+ np.datetime64(datetime.date(1945, 3, 25)))
307
+ assert_equal(np.datetime64('2045-03-25', 'D'),
308
+ np.datetime64(datetime.date(2045, 3, 25), 'D'))
309
+ # Construction from datetime.datetime
310
+ assert_equal(np.datetime64('1980-01-25T14:36:22.5'),
311
+ np.datetime64(datetime.datetime(1980, 1, 25,
312
+ 14, 36, 22, 500000)))
313
+
314
+ # Construction with time units from a date is okay
315
+ assert_equal(np.datetime64('1920-03-13', 'h'),
316
+ np.datetime64('1920-03-13T00'))
317
+ assert_equal(np.datetime64('1920-03', 'm'),
318
+ np.datetime64('1920-03-01T00:00'))
319
+ assert_equal(np.datetime64('1920', 's'),
320
+ np.datetime64('1920-01-01T00:00:00'))
321
+ assert_equal(np.datetime64(datetime.date(2045, 3, 25), 'ms'),
322
+ np.datetime64('2045-03-25T00:00:00.000'))
323
+
324
+ # Construction with date units from a datetime is also okay
325
+ assert_equal(np.datetime64('1920-03-13T18', 'D'),
326
+ np.datetime64('1920-03-13'))
327
+ assert_equal(np.datetime64('1920-03-13T18:33:12', 'M'),
328
+ np.datetime64('1920-03'))
329
+ assert_equal(np.datetime64('1920-03-13T18:33:12.5', 'Y'),
330
+ np.datetime64('1920'))
331
+
332
+ def test_datetime_scalar_construction_timezone(self):
333
+ msg = "no explicit representation of timezones available for " \
334
+ "np.datetime64"
335
+ # verify that supplying an explicit timezone works, but is deprecated
336
+ with pytest.warns(UserWarning, match=msg):
337
+ assert_equal(np.datetime64('2000-01-01T00Z'),
338
+ np.datetime64('2000-01-01T00'))
339
+ with pytest.warns(UserWarning, match=msg):
340
+ assert_equal(np.datetime64('2000-01-01T00-08'),
341
+ np.datetime64('2000-01-01T08'))
342
+
343
+ def test_datetime_array_find_type(self):
344
+ dt = np.datetime64('1970-01-01', 'M')
345
+ arr = np.array([dt])
346
+ assert_equal(arr.dtype, np.dtype('M8[M]'))
347
+
348
+ # at the moment, we don't automatically convert these to datetime64
349
+
350
+ dt = datetime.date(1970, 1, 1)
351
+ arr = np.array([dt])
352
+ assert_equal(arr.dtype, np.dtype('O'))
353
+
354
+ dt = datetime.datetime(1970, 1, 1, 12, 30, 40)
355
+ arr = np.array([dt])
356
+ assert_equal(arr.dtype, np.dtype('O'))
357
+
358
+ # find "supertype" for non-dates and dates
359
+
360
+ b = np.bool(True)
361
+ dm = np.datetime64('1970-01-01', 'M')
362
+ d = datetime.date(1970, 1, 1)
363
+ dt = datetime.datetime(1970, 1, 1, 12, 30, 40)
364
+
365
+ arr = np.array([b, dm])
366
+ assert_equal(arr.dtype, np.dtype('O'))
367
+
368
+ arr = np.array([b, d])
369
+ assert_equal(arr.dtype, np.dtype('O'))
370
+
371
+ arr = np.array([b, dt])
372
+ assert_equal(arr.dtype, np.dtype('O'))
373
+
374
+ arr = np.array([d, d]).astype('datetime64')
375
+ assert_equal(arr.dtype, np.dtype('M8[D]'))
376
+
377
+ arr = np.array([dt, dt]).astype('datetime64')
378
+ assert_equal(arr.dtype, np.dtype('M8[us]'))
379
+
380
+ @pytest.mark.parametrize("unit", [
381
+ # test all date / time units and use
382
+ # "generic" to select generic unit
383
+ ("Y"), ("M"), ("W"), ("D"), ("h"), ("m"),
384
+ ("s"), ("ms"), ("us"), ("ns"), ("ps"),
385
+ ("fs"), ("as"), ("generic")])
386
+ def test_timedelta_np_int_construction(self, unit):
387
+ # regression test for gh-7617
388
+ if unit != "generic":
389
+ assert_equal(np.timedelta64(np.int64(123), unit),
390
+ np.timedelta64(123, unit))
391
+ else:
392
+ assert_equal(np.timedelta64(np.int64(123)),
393
+ np.timedelta64(123))
394
+
395
+ def test_timedelta_scalar_construction(self):
396
+ # Construct with different units
397
+ assert_equal(np.timedelta64(7, 'D'),
398
+ np.timedelta64(1, 'W'))
399
+ assert_equal(np.timedelta64(120, 's'),
400
+ np.timedelta64(2, 'm'))
401
+
402
+ # Default construction means 0
403
+ assert_equal(np.timedelta64(), np.timedelta64(0))
404
+
405
+ # None gets constructed as NaT
406
+ assert_equal(np.timedelta64(None), np.timedelta64('NaT'))
407
+
408
+ # Some basic strings and repr
409
+ assert_equal(str(np.timedelta64('NaT')), 'NaT')
410
+ assert_equal(repr(np.timedelta64('NaT')),
411
+ "np.timedelta64('NaT')")
412
+ assert_equal(str(np.timedelta64(3, 's')), '3 seconds')
413
+ assert_equal(repr(np.timedelta64(-3, 's')),
414
+ "np.timedelta64(-3,'s')")
415
+ assert_equal(repr(np.timedelta64(12)),
416
+ "np.timedelta64(12)")
417
+
418
+ # Construction from an integer produces generic units
419
+ assert_equal(np.timedelta64(12).dtype, np.dtype('m8'))
420
+
421
+ # When constructing from a scalar or zero-dimensional array,
422
+ # it either keeps the units or you can override them.
423
+ a = np.timedelta64(2, 'h')
424
+ b = np.array(2, dtype='m8[h]')
425
+
426
+ assert_equal(a.dtype, np.dtype('m8[h]'))
427
+ assert_equal(b.dtype, np.dtype('m8[h]'))
428
+
429
+ assert_equal(np.timedelta64(a), a)
430
+ assert_equal(np.timedelta64(a).dtype, np.dtype('m8[h]'))
431
+
432
+ assert_equal(np.timedelta64(b), a)
433
+ assert_equal(np.timedelta64(b).dtype, np.dtype('m8[h]'))
434
+
435
+ assert_equal(np.timedelta64(a, 's'), a)
436
+ assert_equal(np.timedelta64(a, 's').dtype, np.dtype('m8[s]'))
437
+
438
+ assert_equal(np.timedelta64(b, 's'), a)
439
+ assert_equal(np.timedelta64(b, 's').dtype, np.dtype('m8[s]'))
440
+
441
+ # Construction from datetime.timedelta
442
+ assert_equal(np.timedelta64(5, 'D'),
443
+ np.timedelta64(datetime.timedelta(days=5)))
444
+ assert_equal(np.timedelta64(102347621, 's'),
445
+ np.timedelta64(datetime.timedelta(seconds=102347621)))
446
+ assert_equal(np.timedelta64(-10234760000, 'us'),
447
+ np.timedelta64(datetime.timedelta(
448
+ microseconds=-10234760000)))
449
+ assert_equal(np.timedelta64(10234760000, 'us'),
450
+ np.timedelta64(datetime.timedelta(
451
+ microseconds=10234760000)))
452
+ assert_equal(np.timedelta64(1023476, 'ms'),
453
+ np.timedelta64(datetime.timedelta(milliseconds=1023476)))
454
+ assert_equal(np.timedelta64(10, 'm'),
455
+ np.timedelta64(datetime.timedelta(minutes=10)))
456
+ assert_equal(np.timedelta64(281, 'h'),
457
+ np.timedelta64(datetime.timedelta(hours=281)))
458
+ assert_equal(np.timedelta64(28, 'W'),
459
+ np.timedelta64(datetime.timedelta(weeks=28)))
460
+
461
+ # Cannot construct across nonlinear time unit boundaries
462
+ a = np.timedelta64(3, 's')
463
+ assert_raises(TypeError, np.timedelta64, a, 'M')
464
+ assert_raises(TypeError, np.timedelta64, a, 'Y')
465
+ a = np.timedelta64(6, 'M')
466
+ assert_raises(TypeError, np.timedelta64, a, 'D')
467
+ assert_raises(TypeError, np.timedelta64, a, 'h')
468
+ a = np.timedelta64(1, 'Y')
469
+ assert_raises(TypeError, np.timedelta64, a, 'D')
470
+ assert_raises(TypeError, np.timedelta64, a, 'm')
471
+ a = datetime.timedelta(seconds=3)
472
+ assert_raises(TypeError, np.timedelta64, a, 'M')
473
+ assert_raises(TypeError, np.timedelta64, a, 'Y')
474
+ a = datetime.timedelta(weeks=3)
475
+ assert_raises(TypeError, np.timedelta64, a, 'M')
476
+ assert_raises(TypeError, np.timedelta64, a, 'Y')
477
+ a = datetime.timedelta()
478
+ assert_raises(TypeError, np.timedelta64, a, 'M')
479
+ assert_raises(TypeError, np.timedelta64, a, 'Y')
480
+
481
+ def test_timedelta_object_array_conversion(self):
482
+ # Regression test for gh-11096
483
+ inputs = [datetime.timedelta(28),
484
+ datetime.timedelta(30),
485
+ datetime.timedelta(31)]
486
+ expected = np.array([28, 30, 31], dtype='timedelta64[D]')
487
+ actual = np.array(inputs, dtype='timedelta64[D]')
488
+ assert_equal(expected, actual)
489
+
490
+ def test_timedelta_0_dim_object_array_conversion(self):
491
+ # Regression test for gh-11151
492
+ test = np.array(datetime.timedelta(seconds=20))
493
+ actual = test.astype(np.timedelta64)
494
+ # expected value from the array constructor workaround
495
+ # described in above issue
496
+ expected = np.array(datetime.timedelta(seconds=20),
497
+ np.timedelta64)
498
+ assert_equal(actual, expected)
499
+
500
+ def test_timedelta_nat_format(self):
501
+ # gh-17552
502
+ assert_equal('NaT', f'{np.timedelta64("nat")}')
503
+
504
+ def test_timedelta_scalar_construction_units(self):
505
+ # String construction detecting units
506
+ assert_equal(np.datetime64('2010').dtype,
507
+ np.dtype('M8[Y]'))
508
+ assert_equal(np.datetime64('2010-03').dtype,
509
+ np.dtype('M8[M]'))
510
+ assert_equal(np.datetime64('2010-03-12').dtype,
511
+ np.dtype('M8[D]'))
512
+ assert_equal(np.datetime64('2010-03-12T17').dtype,
513
+ np.dtype('M8[h]'))
514
+ assert_equal(np.datetime64('2010-03-12T17:15').dtype,
515
+ np.dtype('M8[m]'))
516
+ assert_equal(np.datetime64('2010-03-12T17:15:08').dtype,
517
+ np.dtype('M8[s]'))
518
+
519
+ assert_equal(np.datetime64('2010-03-12T17:15:08.1').dtype,
520
+ np.dtype('M8[ms]'))
521
+ assert_equal(np.datetime64('2010-03-12T17:15:08.12').dtype,
522
+ np.dtype('M8[ms]'))
523
+ assert_equal(np.datetime64('2010-03-12T17:15:08.123').dtype,
524
+ np.dtype('M8[ms]'))
525
+
526
+ assert_equal(np.datetime64('2010-03-12T17:15:08.1234').dtype,
527
+ np.dtype('M8[us]'))
528
+ assert_equal(np.datetime64('2010-03-12T17:15:08.12345').dtype,
529
+ np.dtype('M8[us]'))
530
+ assert_equal(np.datetime64('2010-03-12T17:15:08.123456').dtype,
531
+ np.dtype('M8[us]'))
532
+
533
+ assert_equal(np.datetime64('1970-01-01T00:00:02.1234567').dtype,
534
+ np.dtype('M8[ns]'))
535
+ assert_equal(np.datetime64('1970-01-01T00:00:02.12345678').dtype,
536
+ np.dtype('M8[ns]'))
537
+ assert_equal(np.datetime64('1970-01-01T00:00:02.123456789').dtype,
538
+ np.dtype('M8[ns]'))
539
+
540
+ assert_equal(np.datetime64('1970-01-01T00:00:02.1234567890').dtype,
541
+ np.dtype('M8[ps]'))
542
+ assert_equal(np.datetime64('1970-01-01T00:00:02.12345678901').dtype,
543
+ np.dtype('M8[ps]'))
544
+ assert_equal(np.datetime64('1970-01-01T00:00:02.123456789012').dtype,
545
+ np.dtype('M8[ps]'))
546
+
547
+ assert_equal(np.datetime64(
548
+ '1970-01-01T00:00:02.1234567890123').dtype,
549
+ np.dtype('M8[fs]'))
550
+ assert_equal(np.datetime64(
551
+ '1970-01-01T00:00:02.12345678901234').dtype,
552
+ np.dtype('M8[fs]'))
553
+ assert_equal(np.datetime64(
554
+ '1970-01-01T00:00:02.123456789012345').dtype,
555
+ np.dtype('M8[fs]'))
556
+
557
+ assert_equal(np.datetime64(
558
+ '1970-01-01T00:00:02.1234567890123456').dtype,
559
+ np.dtype('M8[as]'))
560
+ assert_equal(np.datetime64(
561
+ '1970-01-01T00:00:02.12345678901234567').dtype,
562
+ np.dtype('M8[as]'))
563
+ assert_equal(np.datetime64(
564
+ '1970-01-01T00:00:02.123456789012345678').dtype,
565
+ np.dtype('M8[as]'))
566
+
567
+ # Python date object
568
+ assert_equal(np.datetime64(datetime.date(2010, 4, 16)).dtype,
569
+ np.dtype('M8[D]'))
570
+
571
+ # Python datetime object
572
+ assert_equal(np.datetime64(
573
+ datetime.datetime(2010, 4, 16, 13, 45, 18)).dtype,
574
+ np.dtype('M8[us]'))
575
+
576
+ # 'today' special value
577
+ assert_equal(np.datetime64('today').dtype,
578
+ np.dtype('M8[D]'))
579
+
580
+ # 'now' special value
581
+ assert_equal(np.datetime64('now').dtype,
582
+ np.dtype('M8[s]'))
583
+
584
+ def test_datetime_nat_casting(self):
585
+ a = np.array('NaT', dtype='M8[D]')
586
+ b = np.datetime64('NaT', '[D]')
587
+
588
+ # Arrays
589
+ assert_equal(a.astype('M8[s]'), np.array('NaT', dtype='M8[s]'))
590
+ assert_equal(a.astype('M8[ms]'), np.array('NaT', dtype='M8[ms]'))
591
+ assert_equal(a.astype('M8[M]'), np.array('NaT', dtype='M8[M]'))
592
+ assert_equal(a.astype('M8[Y]'), np.array('NaT', dtype='M8[Y]'))
593
+ assert_equal(a.astype('M8[W]'), np.array('NaT', dtype='M8[W]'))
594
+
595
+ # Scalars -> Scalars
596
+ assert_equal(np.datetime64(b, '[s]'), np.datetime64('NaT', '[s]'))
597
+ assert_equal(np.datetime64(b, '[ms]'), np.datetime64('NaT', '[ms]'))
598
+ assert_equal(np.datetime64(b, '[M]'), np.datetime64('NaT', '[M]'))
599
+ assert_equal(np.datetime64(b, '[Y]'), np.datetime64('NaT', '[Y]'))
600
+ assert_equal(np.datetime64(b, '[W]'), np.datetime64('NaT', '[W]'))
601
+
602
+ # Arrays -> Scalars
603
+ assert_equal(np.datetime64(a, '[s]'), np.datetime64('NaT', '[s]'))
604
+ assert_equal(np.datetime64(a, '[ms]'), np.datetime64('NaT', '[ms]'))
605
+ assert_equal(np.datetime64(a, '[M]'), np.datetime64('NaT', '[M]'))
606
+ assert_equal(np.datetime64(a, '[Y]'), np.datetime64('NaT', '[Y]'))
607
+ assert_equal(np.datetime64(a, '[W]'), np.datetime64('NaT', '[W]'))
608
+
609
+ # NaN -> NaT
610
+ nan = np.array([np.nan] * 8 + [0])
611
+ fnan = nan.astype('f')
612
+ lnan = nan.astype('g')
613
+ cnan = nan.astype('D')
614
+ cfnan = nan.astype('F')
615
+ clnan = nan.astype('G')
616
+ hnan = nan.astype(np.half)
617
+
618
+ nat = np.array([np.datetime64('NaT')] * 8 + [np.datetime64(0, 'D')])
619
+ assert_equal(nan.astype('M8[ns]'), nat)
620
+ assert_equal(fnan.astype('M8[ns]'), nat)
621
+ assert_equal(lnan.astype('M8[ns]'), nat)
622
+ assert_equal(cnan.astype('M8[ns]'), nat)
623
+ assert_equal(cfnan.astype('M8[ns]'), nat)
624
+ assert_equal(clnan.astype('M8[ns]'), nat)
625
+ assert_equal(hnan.astype('M8[ns]'), nat)
626
+
627
+ nat = np.array([np.timedelta64('NaT')] * 8 + [np.timedelta64(0)])
628
+ assert_equal(nan.astype('timedelta64[ns]'), nat)
629
+ assert_equal(fnan.astype('timedelta64[ns]'), nat)
630
+ assert_equal(lnan.astype('timedelta64[ns]'), nat)
631
+ assert_equal(cnan.astype('timedelta64[ns]'), nat)
632
+ assert_equal(cfnan.astype('timedelta64[ns]'), nat)
633
+ assert_equal(clnan.astype('timedelta64[ns]'), nat)
634
+ assert_equal(hnan.astype('timedelta64[ns]'), nat)
635
+
636
+ def test_days_creation(self):
637
+ assert_equal(np.array('1599', dtype='M8[D]').astype('i8'),
638
+ (1600 - 1970) * 365 - (1972 - 1600) / 4 + 3 - 365)
639
+ assert_equal(np.array('1600', dtype='M8[D]').astype('i8'),
640
+ (1600 - 1970) * 365 - (1972 - 1600) / 4 + 3)
641
+ assert_equal(np.array('1601', dtype='M8[D]').astype('i8'),
642
+ (1600 - 1970) * 365 - (1972 - 1600) / 4 + 3 + 366)
643
+ assert_equal(np.array('1900', dtype='M8[D]').astype('i8'),
644
+ (1900 - 1970) * 365 - (1970 - 1900) // 4)
645
+ assert_equal(np.array('1901', dtype='M8[D]').astype('i8'),
646
+ (1900 - 1970) * 365 - (1970 - 1900) // 4 + 365)
647
+ assert_equal(np.array('1967', dtype='M8[D]').astype('i8'), -3 * 365 - 1)
648
+ assert_equal(np.array('1968', dtype='M8[D]').astype('i8'), -2 * 365 - 1)
649
+ assert_equal(np.array('1969', dtype='M8[D]').astype('i8'), -1 * 365)
650
+ assert_equal(np.array('1970', dtype='M8[D]').astype('i8'), 0 * 365)
651
+ assert_equal(np.array('1971', dtype='M8[D]').astype('i8'), 1 * 365)
652
+ assert_equal(np.array('1972', dtype='M8[D]').astype('i8'), 2 * 365)
653
+ assert_equal(np.array('1973', dtype='M8[D]').astype('i8'), 3 * 365 + 1)
654
+ assert_equal(np.array('1974', dtype='M8[D]').astype('i8'), 4 * 365 + 1)
655
+ assert_equal(np.array('2000', dtype='M8[D]').astype('i8'),
656
+ (2000 - 1970) * 365 + (2000 - 1972) // 4)
657
+ assert_equal(np.array('2001', dtype='M8[D]').astype('i8'),
658
+ (2000 - 1970) * 365 + (2000 - 1972) // 4 + 366)
659
+ assert_equal(np.array('2400', dtype='M8[D]').astype('i8'),
660
+ (2400 - 1970) * 365 + (2400 - 1972) // 4 - 3)
661
+ assert_equal(np.array('2401', dtype='M8[D]').astype('i8'),
662
+ (2400 - 1970) * 365 + (2400 - 1972) // 4 - 3 + 366)
663
+
664
+ assert_equal(np.array('1600-02-29', dtype='M8[D]').astype('i8'),
665
+ (1600 - 1970) * 365 - (1972 - 1600) // 4 + 3 + 31 + 28)
666
+ assert_equal(np.array('1600-03-01', dtype='M8[D]').astype('i8'),
667
+ (1600 - 1970) * 365 - (1972 - 1600) // 4 + 3 + 31 + 29)
668
+ assert_equal(np.array('2000-02-29', dtype='M8[D]').astype('i8'),
669
+ (2000 - 1970) * 365 + (2000 - 1972) // 4 + 31 + 28)
670
+ assert_equal(np.array('2000-03-01', dtype='M8[D]').astype('i8'),
671
+ (2000 - 1970) * 365 + (2000 - 1972) // 4 + 31 + 29)
672
+ assert_equal(np.array('2001-03-22', dtype='M8[D]').astype('i8'),
673
+ (2000 - 1970) * 365 + (2000 - 1972) // 4 + 366 + 31 + 28 + 21)
674
+
675
+ def test_days_to_pydate(self):
676
+ assert_equal(np.array('1599', dtype='M8[D]').astype('O'),
677
+ datetime.date(1599, 1, 1))
678
+ assert_equal(np.array('1600', dtype='M8[D]').astype('O'),
679
+ datetime.date(1600, 1, 1))
680
+ assert_equal(np.array('1601', dtype='M8[D]').astype('O'),
681
+ datetime.date(1601, 1, 1))
682
+ assert_equal(np.array('1900', dtype='M8[D]').astype('O'),
683
+ datetime.date(1900, 1, 1))
684
+ assert_equal(np.array('1901', dtype='M8[D]').astype('O'),
685
+ datetime.date(1901, 1, 1))
686
+ assert_equal(np.array('2000', dtype='M8[D]').astype('O'),
687
+ datetime.date(2000, 1, 1))
688
+ assert_equal(np.array('2001', dtype='M8[D]').astype('O'),
689
+ datetime.date(2001, 1, 1))
690
+ assert_equal(np.array('1600-02-29', dtype='M8[D]').astype('O'),
691
+ datetime.date(1600, 2, 29))
692
+ assert_equal(np.array('1600-03-01', dtype='M8[D]').astype('O'),
693
+ datetime.date(1600, 3, 1))
694
+ assert_equal(np.array('2001-03-22', dtype='M8[D]').astype('O'),
695
+ datetime.date(2001, 3, 22))
696
+
697
+ def test_dtype_comparison(self):
698
+ assert_(not (np.dtype('M8[us]') == np.dtype('M8[ms]')))
699
+ assert_(np.dtype('M8[us]') != np.dtype('M8[ms]'))
700
+ assert_(np.dtype('M8[2D]') != np.dtype('M8[D]'))
701
+ assert_(np.dtype('M8[D]') != np.dtype('M8[2D]'))
702
+
703
+ def test_pydatetime_creation(self):
704
+ a = np.array(['1960-03-12', datetime.date(1960, 3, 12)], dtype='M8[D]')
705
+ assert_equal(a[0], a[1])
706
+ a = np.array(['1999-12-31', datetime.date(1999, 12, 31)], dtype='M8[D]')
707
+ assert_equal(a[0], a[1])
708
+ a = np.array(['2000-01-01', datetime.date(2000, 1, 1)], dtype='M8[D]')
709
+ assert_equal(a[0], a[1])
710
+ # Will fail if the date changes during the exact right moment
711
+ a = np.array(['today', datetime.date.today()], dtype='M8[D]')
712
+ assert_equal(a[0], a[1])
713
+ # datetime.datetime.now() returns local time, not UTC
714
+ #a = np.array(['now', datetime.datetime.now()], dtype='M8[s]')
715
+ #assert_equal(a[0], a[1])
716
+
717
+ # we can give a datetime.date time units
718
+ assert_equal(np.array(datetime.date(1960, 3, 12), dtype='M8[s]'),
719
+ np.array(np.datetime64('1960-03-12T00:00:00')))
720
+
721
+ def test_datetime_string_conversion(self):
722
+ a = ['2011-03-16', '1920-01-01', '2013-05-19']
723
+ str_a = np.array(a, dtype='S')
724
+ uni_a = np.array(a, dtype='U')
725
+ dt_a = np.array(a, dtype='M')
726
+
727
+ # String to datetime
728
+ assert_equal(dt_a, str_a.astype('M'))
729
+ assert_equal(dt_a.dtype, str_a.astype('M').dtype)
730
+ dt_b = np.empty_like(dt_a)
731
+ dt_b[...] = str_a
732
+ assert_equal(dt_a, dt_b)
733
+
734
+ # Datetime to string
735
+ assert_equal(str_a, dt_a.astype('S0'))
736
+ str_b = np.empty_like(str_a)
737
+ str_b[...] = dt_a
738
+ assert_equal(str_a, str_b)
739
+
740
+ # Unicode to datetime
741
+ assert_equal(dt_a, uni_a.astype('M'))
742
+ assert_equal(dt_a.dtype, uni_a.astype('M').dtype)
743
+ dt_b = np.empty_like(dt_a)
744
+ dt_b[...] = uni_a
745
+ assert_equal(dt_a, dt_b)
746
+
747
+ # Datetime to unicode
748
+ assert_equal(uni_a, dt_a.astype('U'))
749
+ uni_b = np.empty_like(uni_a)
750
+ uni_b[...] = dt_a
751
+ assert_equal(uni_a, uni_b)
752
+
753
+ # Datetime to long string - gh-9712
754
+ assert_equal(str_a, dt_a.astype((np.bytes_, 128)))
755
+ str_b = np.empty(str_a.shape, dtype=(np.bytes_, 128))
756
+ str_b[...] = dt_a
757
+ assert_equal(str_a, str_b)
758
+
759
+ @pytest.mark.parametrize("time_dtype", ["m8[D]", "M8[Y]"])
760
+ def test_time_byteswapping(self, time_dtype):
761
+ times = np.array(["2017", "NaT"], dtype=time_dtype)
762
+ times_swapped = times.astype(times.dtype.newbyteorder())
763
+ assert_array_equal(times, times_swapped)
764
+
765
+ unswapped = times_swapped.view(np.dtype("int64").newbyteorder())
766
+ assert_array_equal(unswapped, times.view(np.int64))
767
+
768
+ @pytest.mark.parametrize(["time1", "time2"],
769
+ [("M8[s]", "M8[D]"), ("m8[s]", "m8[ns]")])
770
+ def test_time_byteswapped_cast(self, time1, time2):
771
+ dtype1 = np.dtype(time1)
772
+ dtype2 = np.dtype(time2)
773
+ times = np.array(["2017", "NaT"], dtype=dtype1)
774
+ expected = times.astype(dtype2)
775
+
776
+ # Test that every byte-swapping combination also returns the same
777
+ # results (previous tests check that this comparison works fine).
778
+ res = times.astype(dtype1.newbyteorder()).astype(dtype2)
779
+ assert_array_equal(res, expected)
780
+ res = times.astype(dtype2.newbyteorder())
781
+ assert_array_equal(res, expected)
782
+ res = times.astype(dtype1.newbyteorder()).astype(dtype2.newbyteorder())
783
+ assert_array_equal(res, expected)
784
+
785
+ @pytest.mark.parametrize("time_dtype", ["m8[D]", "M8[Y]"])
786
+ @pytest.mark.parametrize("str_dtype", ["U", "S"])
787
+ def test_datetime_conversions_byteorders(self, str_dtype, time_dtype):
788
+ times = np.array(["2017", "NaT"], dtype=time_dtype)
789
+ # Unfortunately, timedelta does not roundtrip:
790
+ from_strings = np.array(["2017", "NaT"], dtype=str_dtype)
791
+ to_strings = times.astype(str_dtype) # assume this is correct
792
+
793
+ # Check that conversion from times to string works if src is swapped:
794
+ times_swapped = times.astype(times.dtype.newbyteorder())
795
+ res = times_swapped.astype(str_dtype)
796
+ assert_array_equal(res, to_strings)
797
+ # And also if both are swapped:
798
+ res = times_swapped.astype(to_strings.dtype.newbyteorder())
799
+ assert_array_equal(res, to_strings)
800
+ # only destination is swapped:
801
+ res = times.astype(to_strings.dtype.newbyteorder())
802
+ assert_array_equal(res, to_strings)
803
+
804
+ # Check that conversion from string to times works if src is swapped:
805
+ from_strings_swapped = from_strings.astype(
806
+ from_strings.dtype.newbyteorder())
807
+ res = from_strings_swapped.astype(time_dtype)
808
+ assert_array_equal(res, times)
809
+ # And if both are swapped:
810
+ res = from_strings_swapped.astype(times.dtype.newbyteorder())
811
+ assert_array_equal(res, times)
812
+ # Only destination is swapped:
813
+ res = from_strings.astype(times.dtype.newbyteorder())
814
+ assert_array_equal(res, times)
815
+
816
+ def test_datetime_array_str(self):
817
+ a = np.array(['2011-03-16', '1920-01-01', '2013-05-19'], dtype='M')
818
+ assert_equal(str(a), "['2011-03-16' '1920-01-01' '2013-05-19']")
819
+
820
+ a = np.array(['2011-03-16T13:55', '1920-01-01T03:12'], dtype='M')
821
+ assert_equal(np.array2string(a, separator=', ',
822
+ formatter={'datetime': lambda x:
823
+ f"'{np.datetime_as_string(x, timezone='UTC')}'"}),
824
+ "['2011-03-16T13:55Z', '1920-01-01T03:12Z']")
825
+
826
+ # Check that one NaT doesn't corrupt subsequent entries
827
+ a = np.array(['2010', 'NaT', '2030']).astype('M')
828
+ assert_equal(str(a), "['2010' 'NaT' '2030']")
829
+
830
+ def test_timedelta_array_str(self):
831
+ a = np.array([-1, 0, 100], dtype='m')
832
+ assert_equal(str(a), "[ -1 0 100]")
833
+ a = np.array(['NaT', 'NaT'], dtype='m')
834
+ assert_equal(str(a), "['NaT' 'NaT']")
835
+ # Check right-alignment with NaTs
836
+ a = np.array([-1, 'NaT', 0], dtype='m')
837
+ assert_equal(str(a), "[ -1 'NaT' 0]")
838
+ a = np.array([-1, 'NaT', 1234567], dtype='m')
839
+ assert_equal(str(a), "[ -1 'NaT' 1234567]")
840
+
841
+ # Test with other byteorder:
842
+ a = np.array([-1, 'NaT', 1234567], dtype='>m')
843
+ assert_equal(str(a), "[ -1 'NaT' 1234567]")
844
+ a = np.array([-1, 'NaT', 1234567], dtype='<m')
845
+ assert_equal(str(a), "[ -1 'NaT' 1234567]")
846
+
847
+ def test_timedelta_array_with_nats(self):
848
+ # Regression test for gh-29497.
849
+ x = np.array([np.timedelta64('nat'),
850
+ np.timedelta64('nat', 's'),
851
+ np.timedelta64('nat', 'ms'),
852
+ np.timedelta64(123, 'ms')])
853
+ for td in x[:3]:
854
+ assert np.isnat(td)
855
+
856
+ def test_timedelta_array_nat_assignment(self):
857
+ # Regression test for gh-29497.
858
+ x = np.zeros(3, dtype='m8[ms]')
859
+ x[1] = np.timedelta64('nat', 's')
860
+ assert np.isnat(x[1])
861
+
862
+ def test_pickle(self):
863
+ # Check that pickle roundtripping works
864
+ for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
865
+ dt = np.dtype('M8[7D]')
866
+ assert_equal(pickle.loads(pickle.dumps(dt, protocol=proto)), dt)
867
+ dt = np.dtype('M8[W]')
868
+ assert_equal(pickle.loads(pickle.dumps(dt, protocol=proto)), dt)
869
+ scalar = np.datetime64('2016-01-01T00:00:00.000000000')
870
+ assert_equal(pickle.loads(pickle.dumps(scalar, protocol=proto)),
871
+ scalar)
872
+ delta = scalar - np.datetime64('2015-01-01T00:00:00.000000000')
873
+ assert_equal(pickle.loads(pickle.dumps(delta, protocol=proto)),
874
+ delta)
875
+
876
+ # Check that loading pickles from 1.6 works
877
+ pkl = b"cnumpy\ndtype\np0\n(S'M8'\np1\nI0\nI1\ntp2\nRp3\n"\
878
+ b"(I4\nS'<'\np4\nNNNI-1\nI-1\nI0\n((dp5\n(S'D'\np6\n"\
879
+ b"I7\nI1\nI1\ntp7\ntp8\ntp9\nb."
880
+ assert_equal(pickle.loads(pkl), np.dtype('<M8[7D]'))
881
+ pkl = b"cnumpy\ndtype\np0\n(S'M8'\np1\nI0\nI1\ntp2\nRp3\n"\
882
+ b"(I4\nS'<'\np4\nNNNI-1\nI-1\nI0\n((dp5\n(S'W'\np6\n"\
883
+ b"I1\nI1\nI1\ntp7\ntp8\ntp9\nb."
884
+ assert_equal(pickle.loads(pkl), np.dtype('<M8[W]'))
885
+ pkl = b"cnumpy\ndtype\np0\n(S'M8'\np1\nI0\nI1\ntp2\nRp3\n"\
886
+ b"(I4\nS'>'\np4\nNNNI-1\nI-1\nI0\n((dp5\n(S'us'\np6\n"\
887
+ b"I1\nI1\nI1\ntp7\ntp8\ntp9\nb."
888
+ assert_equal(pickle.loads(pkl), np.dtype('>M8[us]'))
889
+
890
+ def test_gh_29555(self):
891
+ # check that dtype metadata round-trips when none
892
+ dt = np.dtype('>M8[us]')
893
+ assert dt.metadata is None
894
+ for proto in range(2, pickle.HIGHEST_PROTOCOL + 1):
895
+ res = pickle.loads(pickle.dumps(dt, protocol=proto))
896
+ assert_equal(res, dt)
897
+ assert res.metadata is None
898
+
899
+ def test_setstate(self):
900
+ "Verify that datetime dtype __setstate__ can handle bad arguments"
901
+ dt = np.dtype('>M8[us]')
902
+ assert_raises(ValueError, dt.__setstate__, (4, '>', None, None, None, -1, -1, 0, 1))
903
+ assert_(dt.__reduce__()[2] == np.dtype('>M8[us]').__reduce__()[2])
904
+ assert_raises(TypeError, dt.__setstate__, (4, '>', None, None, None, -1, -1, 0, ({}, 'xxx')))
905
+ assert_(dt.__reduce__()[2] == np.dtype('>M8[us]').__reduce__()[2])
906
+
907
+ def test_dtype_promotion(self):
908
+ # datetime <op> datetime computes the metadata gcd
909
+ # timedelta <op> timedelta computes the metadata gcd
910
+ for mM in ['m', 'M']:
911
+ assert_equal(
912
+ np.promote_types(np.dtype(mM + '8[2Y]'), np.dtype(mM + '8[2Y]')),
913
+ np.dtype(mM + '8[2Y]'))
914
+ assert_equal(
915
+ np.promote_types(np.dtype(mM + '8[12Y]'), np.dtype(mM + '8[15Y]')),
916
+ np.dtype(mM + '8[3Y]'))
917
+ assert_equal(
918
+ np.promote_types(np.dtype(mM + '8[62M]'), np.dtype(mM + '8[24M]')),
919
+ np.dtype(mM + '8[2M]'))
920
+ assert_equal(
921
+ np.promote_types(np.dtype(mM + '8[1W]'), np.dtype(mM + '8[2D]')),
922
+ np.dtype(mM + '8[1D]'))
923
+ assert_equal(
924
+ np.promote_types(np.dtype(mM + '8[W]'), np.dtype(mM + '8[13s]')),
925
+ np.dtype(mM + '8[s]'))
926
+ assert_equal(
927
+ np.promote_types(np.dtype(mM + '8[13W]'), np.dtype(mM + '8[49s]')),
928
+ np.dtype(mM + '8[7s]'))
929
+ # timedelta <op> timedelta raises when there is no reasonable gcd
930
+ assert_raises(TypeError, np.promote_types,
931
+ np.dtype('m8[Y]'), np.dtype('m8[D]'))
932
+ assert_raises(TypeError, np.promote_types,
933
+ np.dtype('m8[M]'), np.dtype('m8[W]'))
934
+ # timedelta and float cannot be safely cast with each other
935
+ assert_raises(TypeError, np.promote_types, "float32", "m8")
936
+ assert_raises(TypeError, np.promote_types, "m8", "float32")
937
+ assert_raises(TypeError, np.promote_types, "uint64", "m8")
938
+ assert_raises(TypeError, np.promote_types, "m8", "uint64")
939
+
940
+ # timedelta <op> timedelta may overflow with big unit ranges
941
+ assert_raises(OverflowError, np.promote_types,
942
+ np.dtype('m8[W]'), np.dtype('m8[fs]'))
943
+ assert_raises(OverflowError, np.promote_types,
944
+ np.dtype('m8[s]'), np.dtype('m8[as]'))
945
+
946
+ def test_cast_overflow(self):
947
+ # gh-4486
948
+ def cast():
949
+ numpy.datetime64("1971-01-01 00:00:00.000000000000000").astype("<M8[D]")
950
+ assert_raises(OverflowError, cast)
951
+
952
+ def cast2():
953
+ numpy.datetime64("2014").astype("<M8[fs]")
954
+ assert_raises(OverflowError, cast2)
955
+
956
+ def test_pyobject_roundtrip(self):
957
+ # All datetime types should be able to roundtrip through object
958
+ a = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0,
959
+ -1020040340, -2942398, -1, 0, 1, 234523453, 1199164176],
960
+ dtype=np.int64)
961
+ # With date units
962
+ for unit in ['M8[D]', 'M8[W]', 'M8[M]', 'M8[Y]']:
963
+ b = a.copy().view(dtype=unit)
964
+ b[0] = '-0001-01-01'
965
+ b[1] = '-0001-12-31'
966
+ b[2] = '0000-01-01'
967
+ b[3] = '0001-01-01'
968
+ b[4] = '1969-12-31'
969
+ b[5] = '1970-01-01'
970
+ b[6] = '9999-12-31'
971
+ b[7] = '10000-01-01'
972
+ b[8] = 'NaT'
973
+
974
+ assert_equal(b.astype(object).astype(unit), b,
975
+ f"Error roundtripping unit {unit}")
976
+ # With time units
977
+ for unit in ['M8[as]', 'M8[16fs]', 'M8[ps]', 'M8[us]',
978
+ 'M8[300as]', 'M8[20us]']:
979
+ b = a.copy().view(dtype=unit)
980
+ b[0] = '-0001-01-01T00'
981
+ b[1] = '-0001-12-31T00'
982
+ b[2] = '0000-01-01T00'
983
+ b[3] = '0001-01-01T00'
984
+ b[4] = '1969-12-31T23:59:59.999999'
985
+ b[5] = '1970-01-01T00'
986
+ b[6] = '9999-12-31T23:59:59.999999'
987
+ b[7] = '10000-01-01T00'
988
+ b[8] = 'NaT'
989
+
990
+ assert_equal(b.astype(object).astype(unit), b,
991
+ f"Error roundtripping unit {unit}")
992
+
993
+ def test_month_truncation(self):
994
+ # Make sure that months are truncating correctly
995
+ assert_equal(np.array('1945-03-01', dtype='M8[M]'),
996
+ np.array('1945-03-31', dtype='M8[M]'))
997
+ assert_equal(np.array('1969-11-01', dtype='M8[M]'),
998
+ np.array('1969-11-30T23:59:59.99999', dtype='M').astype('M8[M]'))
999
+ assert_equal(np.array('1969-12-01', dtype='M8[M]'),
1000
+ np.array('1969-12-31T23:59:59.99999', dtype='M').astype('M8[M]'))
1001
+ assert_equal(np.array('1970-01-01', dtype='M8[M]'),
1002
+ np.array('1970-01-31T23:59:59.99999', dtype='M').astype('M8[M]'))
1003
+ assert_equal(np.array('1980-02-01', dtype='M8[M]'),
1004
+ np.array('1980-02-29T23:59:59.99999', dtype='M').astype('M8[M]'))
1005
+
1006
+ def test_different_unit_comparison(self):
1007
+ # Check some years with date units
1008
+ for unit1 in ['Y', 'M', 'D']:
1009
+ dt1 = np.dtype(f'M8[{unit1}]')
1010
+ for unit2 in ['Y', 'M', 'D']:
1011
+ dt2 = np.dtype(f'M8[{unit2}]')
1012
+ assert_equal(np.array('1945', dtype=dt1),
1013
+ np.array('1945', dtype=dt2))
1014
+ assert_equal(np.array('1970', dtype=dt1),
1015
+ np.array('1970', dtype=dt2))
1016
+ assert_equal(np.array('9999', dtype=dt1),
1017
+ np.array('9999', dtype=dt2))
1018
+ assert_equal(np.array('10000', dtype=dt1),
1019
+ np.array('10000-01-01', dtype=dt2))
1020
+ assert_equal(np.datetime64('1945', unit1),
1021
+ np.datetime64('1945', unit2))
1022
+ assert_equal(np.datetime64('1970', unit1),
1023
+ np.datetime64('1970', unit2))
1024
+ assert_equal(np.datetime64('9999', unit1),
1025
+ np.datetime64('9999', unit2))
1026
+ assert_equal(np.datetime64('10000', unit1),
1027
+ np.datetime64('10000-01-01', unit2))
1028
+ # Check some datetimes with time units
1029
+ for unit1 in ['6h', 'h', 'm', 's', '10ms', 'ms', 'us']:
1030
+ dt1 = np.dtype(f'M8[{unit1}]')
1031
+ for unit2 in ['h', 'm', 's', 'ms', 'us']:
1032
+ dt2 = np.dtype(f'M8[{unit2}]')
1033
+ assert_equal(np.array('1945-03-12T18', dtype=dt1),
1034
+ np.array('1945-03-12T18', dtype=dt2))
1035
+ assert_equal(np.array('1970-03-12T18', dtype=dt1),
1036
+ np.array('1970-03-12T18', dtype=dt2))
1037
+ assert_equal(np.array('9999-03-12T18', dtype=dt1),
1038
+ np.array('9999-03-12T18', dtype=dt2))
1039
+ assert_equal(np.array('10000-01-01T00', dtype=dt1),
1040
+ np.array('10000-01-01T00', dtype=dt2))
1041
+ assert_equal(np.datetime64('1945-03-12T18', unit1),
1042
+ np.datetime64('1945-03-12T18', unit2))
1043
+ assert_equal(np.datetime64('1970-03-12T18', unit1),
1044
+ np.datetime64('1970-03-12T18', unit2))
1045
+ assert_equal(np.datetime64('9999-03-12T18', unit1),
1046
+ np.datetime64('9999-03-12T18', unit2))
1047
+ assert_equal(np.datetime64('10000-01-01T00', unit1),
1048
+ np.datetime64('10000-01-01T00', unit2))
1049
+ # Check some days with units that won't overflow
1050
+ for unit1 in ['D', '12h', 'h', 'm', 's', '4s', 'ms', 'us']:
1051
+ dt1 = np.dtype(f'M8[{unit1}]')
1052
+ for unit2 in ['D', 'h', 'm', 's', 'ms', 'us']:
1053
+ dt2 = np.dtype(f'M8[{unit2}]')
1054
+ assert_(np.equal(np.array('1932-02-17', dtype='M').astype(dt1),
1055
+ np.array('1932-02-17T00:00:00', dtype='M').astype(dt2),
1056
+ casting='unsafe'))
1057
+ assert_(np.equal(np.array('10000-04-27', dtype='M').astype(dt1),
1058
+ np.array('10000-04-27T00:00:00', dtype='M').astype(dt2),
1059
+ casting='unsafe'))
1060
+
1061
+ # Shouldn't be able to compare datetime and timedelta
1062
+ a = np.array('2012-12-21', dtype='M8[D]')
1063
+ b = np.array(3, dtype='m8[D]')
1064
+ assert_raises(TypeError, np.less, a, b)
1065
+ # not even if "unsafe"
1066
+ assert_raises(TypeError, np.less, a, b, casting='unsafe')
1067
+
1068
+ def test_datetime_like(self):
1069
+ a = np.array([3], dtype='m8[4D]')
1070
+ b = np.array(['2012-12-21'], dtype='M8[D]')
1071
+
1072
+ assert_equal(np.ones_like(a).dtype, a.dtype)
1073
+ assert_equal(np.zeros_like(a).dtype, a.dtype)
1074
+ assert_equal(np.empty_like(a).dtype, a.dtype)
1075
+ assert_equal(np.ones_like(b).dtype, b.dtype)
1076
+ assert_equal(np.zeros_like(b).dtype, b.dtype)
1077
+ assert_equal(np.empty_like(b).dtype, b.dtype)
1078
+
1079
+ def test_datetime_unary(self):
1080
+ for tda, tdb, tdzero, tdone, tdmone in \
1081
+ [
1082
+ # One-dimensional arrays
1083
+ (np.array([3], dtype='m8[D]'),
1084
+ np.array([-3], dtype='m8[D]'),
1085
+ np.array([0], dtype='m8[D]'),
1086
+ np.array([1], dtype='m8[D]'),
1087
+ np.array([-1], dtype='m8[D]')),
1088
+ # NumPy scalars
1089
+ (np.timedelta64(3, '[D]'),
1090
+ np.timedelta64(-3, '[D]'),
1091
+ np.timedelta64(0, '[D]'),
1092
+ np.timedelta64(1, '[D]'),
1093
+ np.timedelta64(-1, '[D]'))]:
1094
+ # negative ufunc
1095
+ assert_equal(-tdb, tda)
1096
+ assert_equal((-tdb).dtype, tda.dtype)
1097
+ assert_equal(np.negative(tdb), tda)
1098
+ assert_equal(np.negative(tdb).dtype, tda.dtype)
1099
+
1100
+ # positive ufunc
1101
+ assert_equal(np.positive(tda), tda)
1102
+ assert_equal(np.positive(tda).dtype, tda.dtype)
1103
+ assert_equal(np.positive(tdb), tdb)
1104
+ assert_equal(np.positive(tdb).dtype, tdb.dtype)
1105
+
1106
+ # absolute ufunc
1107
+ assert_equal(np.absolute(tdb), tda)
1108
+ assert_equal(np.absolute(tdb).dtype, tda.dtype)
1109
+
1110
+ # sign ufunc
1111
+ assert_equal(np.sign(tda), tdone)
1112
+ assert_equal(np.sign(tdb), tdmone)
1113
+ assert_equal(np.sign(tdzero), tdzero)
1114
+ assert_equal(np.sign(tda).dtype, tda.dtype)
1115
+
1116
+ # The ufuncs always produce native-endian results
1117
+ assert_
1118
+
1119
+ def test_datetime_add(self):
1120
+ for dta, dtb, dtc, dtnat, tda, tdb, tdc in \
1121
+ [
1122
+ # One-dimensional arrays
1123
+ (np.array(['2012-12-21'], dtype='M8[D]'),
1124
+ np.array(['2012-12-24'], dtype='M8[D]'),
1125
+ np.array(['2012-12-21T11'], dtype='M8[h]'),
1126
+ np.array(['NaT'], dtype='M8[D]'),
1127
+ np.array([3], dtype='m8[D]'),
1128
+ np.array([11], dtype='m8[h]'),
1129
+ np.array([3 * 24 + 11], dtype='m8[h]')),
1130
+ # NumPy scalars
1131
+ (np.datetime64('2012-12-21', '[D]'),
1132
+ np.datetime64('2012-12-24', '[D]'),
1133
+ np.datetime64('2012-12-21T11', '[h]'),
1134
+ np.datetime64('NaT', '[D]'),
1135
+ np.timedelta64(3, '[D]'),
1136
+ np.timedelta64(11, '[h]'),
1137
+ np.timedelta64(3 * 24 + 11, '[h]'))]:
1138
+ # m8 + m8
1139
+ assert_equal(tda + tdb, tdc)
1140
+ assert_equal((tda + tdb).dtype, np.dtype('m8[h]'))
1141
+ # m8 + bool
1142
+ assert_equal(tdb + True, tdb + 1)
1143
+ assert_equal((tdb + True).dtype, np.dtype('m8[h]'))
1144
+ # m8 + int
1145
+ assert_equal(tdb + 3 * 24, tdc)
1146
+ assert_equal((tdb + 3 * 24).dtype, np.dtype('m8[h]'))
1147
+ # bool + m8
1148
+ assert_equal(False + tdb, tdb)
1149
+ assert_equal((False + tdb).dtype, np.dtype('m8[h]'))
1150
+ # int + m8
1151
+ assert_equal(3 * 24 + tdb, tdc)
1152
+ assert_equal((3 * 24 + tdb).dtype, np.dtype('m8[h]'))
1153
+ # M8 + bool
1154
+ assert_equal(dta + True, dta + 1)
1155
+ assert_equal(dtnat + True, dtnat)
1156
+ assert_equal((dta + True).dtype, np.dtype('M8[D]'))
1157
+ # M8 + int
1158
+ assert_equal(dta + 3, dtb)
1159
+ assert_equal(dtnat + 3, dtnat)
1160
+ assert_equal((dta + 3).dtype, np.dtype('M8[D]'))
1161
+ # bool + M8
1162
+ assert_equal(False + dta, dta)
1163
+ assert_equal(False + dtnat, dtnat)
1164
+ assert_equal((False + dta).dtype, np.dtype('M8[D]'))
1165
+ # int + M8
1166
+ assert_equal(3 + dta, dtb)
1167
+ assert_equal(3 + dtnat, dtnat)
1168
+ assert_equal((3 + dta).dtype, np.dtype('M8[D]'))
1169
+ # M8 + m8
1170
+ assert_equal(dta + tda, dtb)
1171
+ assert_equal(dtnat + tda, dtnat)
1172
+ assert_equal((dta + tda).dtype, np.dtype('M8[D]'))
1173
+ # m8 + M8
1174
+ assert_equal(tda + dta, dtb)
1175
+ assert_equal(tda + dtnat, dtnat)
1176
+ assert_equal((tda + dta).dtype, np.dtype('M8[D]'))
1177
+
1178
+ # In M8 + m8, the result goes to higher precision
1179
+ assert_equal(np.add(dta, tdb, casting='unsafe'), dtc)
1180
+ assert_equal(np.add(dta, tdb, casting='unsafe').dtype,
1181
+ np.dtype('M8[h]'))
1182
+ assert_equal(np.add(tdb, dta, casting='unsafe'), dtc)
1183
+ assert_equal(np.add(tdb, dta, casting='unsafe').dtype,
1184
+ np.dtype('M8[h]'))
1185
+
1186
+ # M8 + M8
1187
+ assert_raises(TypeError, np.add, dta, dtb)
1188
+
1189
+ def test_datetime_subtract(self):
1190
+ for dta, dtb, dtc, dtd, dte, dtnat, tda, tdb, tdc in \
1191
+ [
1192
+ # One-dimensional arrays
1193
+ (np.array(['2012-12-21'], dtype='M8[D]'),
1194
+ np.array(['2012-12-24'], dtype='M8[D]'),
1195
+ np.array(['1940-12-24'], dtype='M8[D]'),
1196
+ np.array(['1940-12-24T00'], dtype='M8[h]'),
1197
+ np.array(['1940-12-23T13'], dtype='M8[h]'),
1198
+ np.array(['NaT'], dtype='M8[D]'),
1199
+ np.array([3], dtype='m8[D]'),
1200
+ np.array([11], dtype='m8[h]'),
1201
+ np.array([3 * 24 - 11], dtype='m8[h]')),
1202
+ # NumPy scalars
1203
+ (np.datetime64('2012-12-21', '[D]'),
1204
+ np.datetime64('2012-12-24', '[D]'),
1205
+ np.datetime64('1940-12-24', '[D]'),
1206
+ np.datetime64('1940-12-24T00', '[h]'),
1207
+ np.datetime64('1940-12-23T13', '[h]'),
1208
+ np.datetime64('NaT', '[D]'),
1209
+ np.timedelta64(3, '[D]'),
1210
+ np.timedelta64(11, '[h]'),
1211
+ np.timedelta64(3 * 24 - 11, '[h]'))]:
1212
+ # m8 - m8
1213
+ assert_equal(tda - tdb, tdc)
1214
+ assert_equal((tda - tdb).dtype, np.dtype('m8[h]'))
1215
+ assert_equal(tdb - tda, -tdc)
1216
+ assert_equal((tdb - tda).dtype, np.dtype('m8[h]'))
1217
+ # m8 - bool
1218
+ assert_equal(tdc - True, tdc - 1)
1219
+ assert_equal((tdc - True).dtype, np.dtype('m8[h]'))
1220
+ # m8 - int
1221
+ assert_equal(tdc - 3 * 24, -tdb)
1222
+ assert_equal((tdc - 3 * 24).dtype, np.dtype('m8[h]'))
1223
+ # int - m8
1224
+ assert_equal(False - tdb, -tdb)
1225
+ assert_equal((False - tdb).dtype, np.dtype('m8[h]'))
1226
+ # int - m8
1227
+ assert_equal(3 * 24 - tdb, tdc)
1228
+ assert_equal((3 * 24 - tdb).dtype, np.dtype('m8[h]'))
1229
+ # M8 - bool
1230
+ assert_equal(dtb - True, dtb - 1)
1231
+ assert_equal(dtnat - True, dtnat)
1232
+ assert_equal((dtb - True).dtype, np.dtype('M8[D]'))
1233
+ # M8 - int
1234
+ assert_equal(dtb - 3, dta)
1235
+ assert_equal(dtnat - 3, dtnat)
1236
+ assert_equal((dtb - 3).dtype, np.dtype('M8[D]'))
1237
+ # M8 - m8
1238
+ assert_equal(dtb - tda, dta)
1239
+ assert_equal(dtnat - tda, dtnat)
1240
+ assert_equal((dtb - tda).dtype, np.dtype('M8[D]'))
1241
+
1242
+ # In M8 - m8, the result goes to higher precision
1243
+ assert_equal(np.subtract(dtc, tdb, casting='unsafe'), dte)
1244
+ assert_equal(np.subtract(dtc, tdb, casting='unsafe').dtype,
1245
+ np.dtype('M8[h]'))
1246
+
1247
+ # M8 - M8 with different goes to higher precision
1248
+ assert_equal(np.subtract(dtc, dtd, casting='unsafe'),
1249
+ np.timedelta64(0, 'h'))
1250
+ assert_equal(np.subtract(dtc, dtd, casting='unsafe').dtype,
1251
+ np.dtype('m8[h]'))
1252
+ assert_equal(np.subtract(dtd, dtc, casting='unsafe'),
1253
+ np.timedelta64(0, 'h'))
1254
+ assert_equal(np.subtract(dtd, dtc, casting='unsafe').dtype,
1255
+ np.dtype('m8[h]'))
1256
+
1257
+ # m8 - M8
1258
+ assert_raises(TypeError, np.subtract, tda, dta)
1259
+ # bool - M8
1260
+ assert_raises(TypeError, np.subtract, False, dta)
1261
+ # int - M8
1262
+ assert_raises(TypeError, np.subtract, 3, dta)
1263
+
1264
+ def test_datetime_multiply(self):
1265
+ for dta, tda, tdb, tdc in \
1266
+ [
1267
+ # One-dimensional arrays
1268
+ (np.array(['2012-12-21'], dtype='M8[D]'),
1269
+ np.array([6], dtype='m8[h]'),
1270
+ np.array([9], dtype='m8[h]'),
1271
+ np.array([12], dtype='m8[h]')),
1272
+ # NumPy scalars
1273
+ (np.datetime64('2012-12-21', '[D]'),
1274
+ np.timedelta64(6, '[h]'),
1275
+ np.timedelta64(9, '[h]'),
1276
+ np.timedelta64(12, '[h]'))]:
1277
+ # m8 * int
1278
+ assert_equal(tda * 2, tdc)
1279
+ assert_equal((tda * 2).dtype, np.dtype('m8[h]'))
1280
+ # int * m8
1281
+ assert_equal(2 * tda, tdc)
1282
+ assert_equal((2 * tda).dtype, np.dtype('m8[h]'))
1283
+ # m8 * float
1284
+ assert_equal(tda * 1.5, tdb)
1285
+ assert_equal((tda * 1.5).dtype, np.dtype('m8[h]'))
1286
+ # float * m8
1287
+ assert_equal(1.5 * tda, tdb)
1288
+ assert_equal((1.5 * tda).dtype, np.dtype('m8[h]'))
1289
+
1290
+ # m8 * m8
1291
+ assert_raises(TypeError, np.multiply, tda, tdb)
1292
+ # m8 * M8
1293
+ assert_raises(TypeError, np.multiply, dta, tda)
1294
+ # M8 * m8
1295
+ assert_raises(TypeError, np.multiply, tda, dta)
1296
+ # M8 * int
1297
+ assert_raises(TypeError, np.multiply, dta, 2)
1298
+ # int * M8
1299
+ assert_raises(TypeError, np.multiply, 2, dta)
1300
+ # M8 * float
1301
+ assert_raises(TypeError, np.multiply, dta, 1.5)
1302
+ # float * M8
1303
+ assert_raises(TypeError, np.multiply, 1.5, dta)
1304
+
1305
+ # NaTs
1306
+ with suppress_warnings() as sup:
1307
+ sup.filter(RuntimeWarning, "invalid value encountered in multiply")
1308
+ nat = np.timedelta64('NaT')
1309
+
1310
+ def check(a, b, res):
1311
+ assert_equal(a * b, res)
1312
+ assert_equal(b * a, res)
1313
+ for tp in (int, float):
1314
+ check(nat, tp(2), nat)
1315
+ check(nat, tp(0), nat)
1316
+ for f in (float('inf'), float('nan')):
1317
+ check(np.timedelta64(1), f, nat)
1318
+ check(np.timedelta64(0), f, nat)
1319
+ check(nat, f, nat)
1320
+
1321
+ @pytest.mark.parametrize("op1, op2, exp", [
1322
+ # m8 same units round down
1323
+ (np.timedelta64(7, 's'),
1324
+ np.timedelta64(4, 's'),
1325
+ 1),
1326
+ # m8 same units round down with negative
1327
+ (np.timedelta64(7, 's'),
1328
+ np.timedelta64(-4, 's'),
1329
+ -2),
1330
+ # m8 same units negative no round down
1331
+ (np.timedelta64(8, 's'),
1332
+ np.timedelta64(-4, 's'),
1333
+ -2),
1334
+ # m8 different units
1335
+ (np.timedelta64(1, 'm'),
1336
+ np.timedelta64(31, 's'),
1337
+ 1),
1338
+ # m8 generic units
1339
+ (np.timedelta64(1890),
1340
+ np.timedelta64(31),
1341
+ 60),
1342
+ # Y // M works
1343
+ (np.timedelta64(2, 'Y'),
1344
+ np.timedelta64('13', 'M'),
1345
+ 1),
1346
+ # handle 1D arrays
1347
+ (np.array([1, 2, 3], dtype='m8'),
1348
+ np.array([2], dtype='m8'),
1349
+ np.array([0, 1, 1], dtype=np.int64)),
1350
+ ])
1351
+ def test_timedelta_floor_divide(self, op1, op2, exp):
1352
+ assert_equal(op1 // op2, exp)
1353
+
1354
+ @pytest.mark.skipif(IS_WASM, reason="fp errors don't work in wasm")
1355
+ @pytest.mark.parametrize("op1, op2", [
1356
+ # div by 0
1357
+ (np.timedelta64(10, 'us'),
1358
+ np.timedelta64(0, 'us')),
1359
+ # div with NaT
1360
+ (np.timedelta64('NaT'),
1361
+ np.timedelta64(50, 'us')),
1362
+ # special case for int64 min
1363
+ # in integer floor division
1364
+ (np.timedelta64(np.iinfo(np.int64).min),
1365
+ np.timedelta64(-1)),
1366
+ ])
1367
+ def test_timedelta_floor_div_warnings(self, op1, op2):
1368
+ with assert_warns(RuntimeWarning):
1369
+ actual = op1 // op2
1370
+ assert_equal(actual, 0)
1371
+ assert_equal(actual.dtype, np.int64)
1372
+
1373
+ @pytest.mark.parametrize("val1, val2", [
1374
+ # the smallest integer that can't be represented
1375
+ # exactly in a double should be preserved if we avoid
1376
+ # casting to double in floordiv operation
1377
+ (9007199254740993, 1),
1378
+ # stress the alternate floordiv code path where
1379
+ # operand signs don't match and remainder isn't 0
1380
+ (9007199254740999, -2),
1381
+ ])
1382
+ def test_timedelta_floor_div_precision(self, val1, val2):
1383
+ op1 = np.timedelta64(val1)
1384
+ op2 = np.timedelta64(val2)
1385
+ actual = op1 // op2
1386
+ # Python reference integer floor
1387
+ expected = val1 // val2
1388
+ assert_equal(actual, expected)
1389
+
1390
+ @pytest.mark.parametrize("val1, val2", [
1391
+ # years and months sometimes can't be unambiguously
1392
+ # divided for floor division operation
1393
+ (np.timedelta64(7, 'Y'),
1394
+ np.timedelta64(3, 's')),
1395
+ (np.timedelta64(7, 'M'),
1396
+ np.timedelta64(1, 'D')),
1397
+ ])
1398
+ def test_timedelta_floor_div_error(self, val1, val2):
1399
+ with assert_raises_regex(TypeError, "common metadata divisor"):
1400
+ val1 // val2
1401
+
1402
+ @pytest.mark.parametrize("op1, op2", [
1403
+ # reuse the test cases from floordiv
1404
+ (np.timedelta64(7, 's'),
1405
+ np.timedelta64(4, 's')),
1406
+ # m8 same units round down with negative
1407
+ (np.timedelta64(7, 's'),
1408
+ np.timedelta64(-4, 's')),
1409
+ # m8 same units negative no round down
1410
+ (np.timedelta64(8, 's'),
1411
+ np.timedelta64(-4, 's')),
1412
+ # m8 different units
1413
+ (np.timedelta64(1, 'm'),
1414
+ np.timedelta64(31, 's')),
1415
+ # m8 generic units
1416
+ (np.timedelta64(1890),
1417
+ np.timedelta64(31)),
1418
+ # Y // M works
1419
+ (np.timedelta64(2, 'Y'),
1420
+ np.timedelta64('13', 'M')),
1421
+ # handle 1D arrays
1422
+ (np.array([1, 2, 3], dtype='m8'),
1423
+ np.array([2], dtype='m8')),
1424
+ ])
1425
+ def test_timedelta_divmod(self, op1, op2):
1426
+ expected = (op1 // op2, op1 % op2)
1427
+ assert_equal(divmod(op1, op2), expected)
1428
+
1429
+ @pytest.mark.parametrize("op1, op2", [
1430
+ # Y and M are incompatible with all units except Y and M
1431
+ (np.timedelta64(1, 'Y'), np.timedelta64(1, 's')),
1432
+ (np.timedelta64(1, 'D'), np.timedelta64(1, 'M')),
1433
+ ])
1434
+ def test_timedelta_divmod_typeerror(self, op1, op2):
1435
+ assert_raises(TypeError, np.divmod, op1, op2)
1436
+
1437
+ @pytest.mark.skipif(IS_WASM, reason="does not work in wasm")
1438
+ @pytest.mark.parametrize("op1, op2", [
1439
+ # reuse cases from floordiv
1440
+ # div by 0
1441
+ (np.timedelta64(10, 'us'),
1442
+ np.timedelta64(0, 'us')),
1443
+ # div with NaT
1444
+ (np.timedelta64('NaT'),
1445
+ np.timedelta64(50, 'us')),
1446
+ # special case for int64 min
1447
+ # in integer floor division
1448
+ (np.timedelta64(np.iinfo(np.int64).min),
1449
+ np.timedelta64(-1)),
1450
+ ])
1451
+ def test_timedelta_divmod_warnings(self, op1, op2):
1452
+ with assert_warns(RuntimeWarning):
1453
+ expected = (op1 // op2, op1 % op2)
1454
+ with assert_warns(RuntimeWarning):
1455
+ actual = divmod(op1, op2)
1456
+ assert_equal(actual, expected)
1457
+
1458
+ def test_datetime_divide(self):
1459
+ for dta, tda, tdb, tdc, tdd in \
1460
+ [
1461
+ # One-dimensional arrays
1462
+ (np.array(['2012-12-21'], dtype='M8[D]'),
1463
+ np.array([6], dtype='m8[h]'),
1464
+ np.array([9], dtype='m8[h]'),
1465
+ np.array([12], dtype='m8[h]'),
1466
+ np.array([6], dtype='m8[m]')),
1467
+ # NumPy scalars
1468
+ (np.datetime64('2012-12-21', '[D]'),
1469
+ np.timedelta64(6, '[h]'),
1470
+ np.timedelta64(9, '[h]'),
1471
+ np.timedelta64(12, '[h]'),
1472
+ np.timedelta64(6, '[m]'))]:
1473
+ # m8 / int
1474
+ assert_equal(tdc / 2, tda)
1475
+ assert_equal((tdc / 2).dtype, np.dtype('m8[h]'))
1476
+ # m8 / float
1477
+ assert_equal(tda / 0.5, tdc)
1478
+ assert_equal((tda / 0.5).dtype, np.dtype('m8[h]'))
1479
+ # m8 / m8
1480
+ assert_equal(tda / tdb, 6 / 9)
1481
+ assert_equal(np.divide(tda, tdb), 6 / 9)
1482
+ assert_equal(np.true_divide(tda, tdb), 6 / 9)
1483
+ assert_equal(tdb / tda, 9 / 6)
1484
+ assert_equal((tda / tdb).dtype, np.dtype('f8'))
1485
+ assert_equal(tda / tdd, 60)
1486
+ assert_equal(tdd / tda, 1 / 60)
1487
+
1488
+ # int / m8
1489
+ assert_raises(TypeError, np.divide, 2, tdb)
1490
+ # float / m8
1491
+ assert_raises(TypeError, np.divide, 0.5, tdb)
1492
+ # m8 / M8
1493
+ assert_raises(TypeError, np.divide, dta, tda)
1494
+ # M8 / m8
1495
+ assert_raises(TypeError, np.divide, tda, dta)
1496
+ # M8 / int
1497
+ assert_raises(TypeError, np.divide, dta, 2)
1498
+ # int / M8
1499
+ assert_raises(TypeError, np.divide, 2, dta)
1500
+ # M8 / float
1501
+ assert_raises(TypeError, np.divide, dta, 1.5)
1502
+ # float / M8
1503
+ assert_raises(TypeError, np.divide, 1.5, dta)
1504
+
1505
+ # NaTs
1506
+ with suppress_warnings() as sup:
1507
+ sup.filter(RuntimeWarning, r".*encountered in divide")
1508
+ nat = np.timedelta64('NaT')
1509
+ for tp in (int, float):
1510
+ assert_equal(np.timedelta64(1) / tp(0), nat)
1511
+ assert_equal(np.timedelta64(0) / tp(0), nat)
1512
+ assert_equal(nat / tp(0), nat)
1513
+ assert_equal(nat / tp(2), nat)
1514
+ # Division by inf
1515
+ assert_equal(np.timedelta64(1) / float('inf'), np.timedelta64(0))
1516
+ assert_equal(np.timedelta64(0) / float('inf'), np.timedelta64(0))
1517
+ assert_equal(nat / float('inf'), nat)
1518
+ # Division by nan
1519
+ assert_equal(np.timedelta64(1) / float('nan'), nat)
1520
+ assert_equal(np.timedelta64(0) / float('nan'), nat)
1521
+ assert_equal(nat / float('nan'), nat)
1522
+
1523
+ def test_datetime_compare(self):
1524
+ # Test all the comparison operators
1525
+ a = np.datetime64('2000-03-12T18:00:00.000000')
1526
+ b = np.array(['2000-03-12T18:00:00.000000',
1527
+ '2000-03-12T17:59:59.999999',
1528
+ '2000-03-12T18:00:00.000001',
1529
+ '1970-01-11T12:00:00.909090',
1530
+ '2016-01-11T12:00:00.909090'],
1531
+ dtype='datetime64[us]')
1532
+ assert_equal(np.equal(a, b), [1, 0, 0, 0, 0])
1533
+ assert_equal(np.not_equal(a, b), [0, 1, 1, 1, 1])
1534
+ assert_equal(np.less(a, b), [0, 0, 1, 0, 1])
1535
+ assert_equal(np.less_equal(a, b), [1, 0, 1, 0, 1])
1536
+ assert_equal(np.greater(a, b), [0, 1, 0, 1, 0])
1537
+ assert_equal(np.greater_equal(a, b), [1, 1, 0, 1, 0])
1538
+
1539
+ def test_datetime_compare_nat(self):
1540
+ dt_nat = np.datetime64('NaT', 'D')
1541
+ dt_other = np.datetime64('2000-01-01')
1542
+ td_nat = np.timedelta64('NaT', 'h')
1543
+ td_other = np.timedelta64(1, 'h')
1544
+
1545
+ for op in [np.equal, np.less, np.less_equal,
1546
+ np.greater, np.greater_equal]:
1547
+ assert_(not op(dt_nat, dt_nat))
1548
+ assert_(not op(dt_nat, dt_other))
1549
+ assert_(not op(dt_other, dt_nat))
1550
+
1551
+ assert_(not op(td_nat, td_nat))
1552
+ assert_(not op(td_nat, td_other))
1553
+ assert_(not op(td_other, td_nat))
1554
+
1555
+ assert_(np.not_equal(dt_nat, dt_nat))
1556
+ assert_(np.not_equal(dt_nat, dt_other))
1557
+ assert_(np.not_equal(dt_other, dt_nat))
1558
+
1559
+ assert_(np.not_equal(td_nat, td_nat))
1560
+ assert_(np.not_equal(td_nat, td_other))
1561
+ assert_(np.not_equal(td_other, td_nat))
1562
+
1563
+ def test_datetime_minmax(self):
1564
+ # The metadata of the result should become the GCD
1565
+ # of the operand metadata
1566
+ a = np.array('1999-03-12T13', dtype='M8[2m]')
1567
+ b = np.array('1999-03-12T12', dtype='M8[s]')
1568
+ assert_equal(np.minimum(a, b), b)
1569
+ assert_equal(np.minimum(a, b).dtype, np.dtype('M8[s]'))
1570
+ assert_equal(np.fmin(a, b), b)
1571
+ assert_equal(np.fmin(a, b).dtype, np.dtype('M8[s]'))
1572
+ assert_equal(np.maximum(a, b), a)
1573
+ assert_equal(np.maximum(a, b).dtype, np.dtype('M8[s]'))
1574
+ assert_equal(np.fmax(a, b), a)
1575
+ assert_equal(np.fmax(a, b).dtype, np.dtype('M8[s]'))
1576
+ # Viewed as integers, the comparison is opposite because
1577
+ # of the units chosen
1578
+ assert_equal(np.minimum(a.view('i8'), b.view('i8')), a.view('i8'))
1579
+
1580
+ # Interaction with NaT
1581
+ a = np.array('1999-03-12T13', dtype='M8[2m]')
1582
+ dtnat = np.array('NaT', dtype='M8[h]')
1583
+ assert_equal(np.minimum(a, dtnat), dtnat)
1584
+ assert_equal(np.minimum(dtnat, a), dtnat)
1585
+ assert_equal(np.maximum(a, dtnat), dtnat)
1586
+ assert_equal(np.maximum(dtnat, a), dtnat)
1587
+ assert_equal(np.fmin(dtnat, a), a)
1588
+ assert_equal(np.fmin(a, dtnat), a)
1589
+ assert_equal(np.fmax(dtnat, a), a)
1590
+ assert_equal(np.fmax(a, dtnat), a)
1591
+
1592
+ # Also do timedelta
1593
+ a = np.array(3, dtype='m8[h]')
1594
+ b = np.array(3 * 3600 - 3, dtype='m8[s]')
1595
+ assert_equal(np.minimum(a, b), b)
1596
+ assert_equal(np.minimum(a, b).dtype, np.dtype('m8[s]'))
1597
+ assert_equal(np.fmin(a, b), b)
1598
+ assert_equal(np.fmin(a, b).dtype, np.dtype('m8[s]'))
1599
+ assert_equal(np.maximum(a, b), a)
1600
+ assert_equal(np.maximum(a, b).dtype, np.dtype('m8[s]'))
1601
+ assert_equal(np.fmax(a, b), a)
1602
+ assert_equal(np.fmax(a, b).dtype, np.dtype('m8[s]'))
1603
+ # Viewed as integers, the comparison is opposite because
1604
+ # of the units chosen
1605
+ assert_equal(np.minimum(a.view('i8'), b.view('i8')), a.view('i8'))
1606
+
1607
+ # should raise between datetime and timedelta
1608
+ #
1609
+ # TODO: Allowing unsafe casting by
1610
+ # default in ufuncs strikes again... :(
1611
+ a = np.array(3, dtype='m8[h]')
1612
+ b = np.array('1999-03-12T12', dtype='M8[s]')
1613
+ #assert_raises(TypeError, np.minimum, a, b)
1614
+ #assert_raises(TypeError, np.maximum, a, b)
1615
+ #assert_raises(TypeError, np.fmin, a, b)
1616
+ #assert_raises(TypeError, np.fmax, a, b)
1617
+ assert_raises(TypeError, np.minimum, a, b, casting='same_kind')
1618
+ assert_raises(TypeError, np.maximum, a, b, casting='same_kind')
1619
+ assert_raises(TypeError, np.fmin, a, b, casting='same_kind')
1620
+ assert_raises(TypeError, np.fmax, a, b, casting='same_kind')
1621
+
1622
+ def test_hours(self):
1623
+ t = np.ones(3, dtype='M8[s]')
1624
+ t[0] = 60 * 60 * 24 + 60 * 60 * 10
1625
+ assert_(t[0].item().hour == 10)
1626
+
1627
+ def test_divisor_conversion_year(self):
1628
+ assert_(np.dtype('M8[Y/4]') == np.dtype('M8[3M]'))
1629
+ assert_(np.dtype('M8[Y/13]') == np.dtype('M8[4W]'))
1630
+ assert_(np.dtype('M8[3Y/73]') == np.dtype('M8[15D]'))
1631
+
1632
+ def test_divisor_conversion_month(self):
1633
+ assert_(np.dtype('M8[M/2]') == np.dtype('M8[2W]'))
1634
+ assert_(np.dtype('M8[M/15]') == np.dtype('M8[2D]'))
1635
+ assert_(np.dtype('M8[3M/40]') == np.dtype('M8[54h]'))
1636
+
1637
+ def test_divisor_conversion_week(self):
1638
+ assert_(np.dtype('m8[W/7]') == np.dtype('m8[D]'))
1639
+ assert_(np.dtype('m8[3W/14]') == np.dtype('m8[36h]'))
1640
+ assert_(np.dtype('m8[5W/140]') == np.dtype('m8[360m]'))
1641
+
1642
+ def test_divisor_conversion_day(self):
1643
+ assert_(np.dtype('M8[D/12]') == np.dtype('M8[2h]'))
1644
+ assert_(np.dtype('M8[D/120]') == np.dtype('M8[12m]'))
1645
+ assert_(np.dtype('M8[3D/960]') == np.dtype('M8[270s]'))
1646
+
1647
+ def test_divisor_conversion_hour(self):
1648
+ assert_(np.dtype('m8[h/30]') == np.dtype('m8[2m]'))
1649
+ assert_(np.dtype('m8[3h/300]') == np.dtype('m8[36s]'))
1650
+
1651
+ def test_divisor_conversion_minute(self):
1652
+ assert_(np.dtype('m8[m/30]') == np.dtype('m8[2s]'))
1653
+ assert_(np.dtype('m8[3m/300]') == np.dtype('m8[600ms]'))
1654
+
1655
+ def test_divisor_conversion_second(self):
1656
+ assert_(np.dtype('m8[s/100]') == np.dtype('m8[10ms]'))
1657
+ assert_(np.dtype('m8[3s/10000]') == np.dtype('m8[300us]'))
1658
+
1659
+ def test_divisor_conversion_fs(self):
1660
+ assert_(np.dtype('M8[fs/100]') == np.dtype('M8[10as]'))
1661
+ assert_raises(ValueError, lambda: np.dtype('M8[3fs/10000]'))
1662
+
1663
+ def test_divisor_conversion_as(self):
1664
+ assert_raises(ValueError, lambda: np.dtype('M8[as/10]'))
1665
+
1666
+ def test_string_parser_variants(self):
1667
+ msg = "no explicit representation of timezones available for " \
1668
+ "np.datetime64"
1669
+ # Allow space instead of 'T' between date and time
1670
+ assert_equal(np.array(['1980-02-29T01:02:03'], np.dtype('M8[s]')),
1671
+ np.array(['1980-02-29 01:02:03'], np.dtype('M8[s]')))
1672
+ # Allow positive years
1673
+ assert_equal(np.array(['+1980-02-29T01:02:03'], np.dtype('M8[s]')),
1674
+ np.array(['+1980-02-29 01:02:03'], np.dtype('M8[s]')))
1675
+ # Allow negative years
1676
+ assert_equal(np.array(['-1980-02-29T01:02:03'], np.dtype('M8[s]')),
1677
+ np.array(['-1980-02-29 01:02:03'], np.dtype('M8[s]')))
1678
+ # UTC specifier
1679
+ with pytest.warns(UserWarning, match=msg):
1680
+ assert_equal(
1681
+ np.array(['+1980-02-29T01:02:03'], np.dtype('M8[s]')),
1682
+ np.array(['+1980-02-29 01:02:03Z'], np.dtype('M8[s]')))
1683
+ with pytest.warns(UserWarning, match=msg):
1684
+ assert_equal(
1685
+ np.array(['-1980-02-29T01:02:03'], np.dtype('M8[s]')),
1686
+ np.array(['-1980-02-29 01:02:03Z'], np.dtype('M8[s]')))
1687
+ # Time zone offset
1688
+ with pytest.warns(UserWarning, match=msg):
1689
+ assert_equal(
1690
+ np.array(['1980-02-29T02:02:03'], np.dtype('M8[s]')),
1691
+ np.array(['1980-02-29 00:32:03-0130'], np.dtype('M8[s]')))
1692
+ with pytest.warns(UserWarning, match=msg):
1693
+ assert_equal(
1694
+ np.array(['1980-02-28T22:32:03'], np.dtype('M8[s]')),
1695
+ np.array(['1980-02-29 00:02:03+01:30'], np.dtype('M8[s]')))
1696
+ with pytest.warns(UserWarning, match=msg):
1697
+ assert_equal(
1698
+ np.array(['1980-02-29T02:32:03.506'], np.dtype('M8[s]')),
1699
+ np.array(['1980-02-29 00:32:03.506-02'], np.dtype('M8[s]')))
1700
+ with pytest.warns(UserWarning, match=msg):
1701
+ assert_equal(np.datetime64('1977-03-02T12:30-0230'),
1702
+ np.datetime64('1977-03-02T15:00'))
1703
+
1704
+ def test_string_parser_error_check(self):
1705
+ msg = "no explicit representation of timezones available for " \
1706
+ "np.datetime64"
1707
+ # Arbitrary bad string
1708
+ assert_raises(ValueError, np.array, ['badvalue'], np.dtype('M8[us]'))
1709
+ # Character after year must be '-'
1710
+ assert_raises(ValueError, np.array, ['1980X'], np.dtype('M8[us]'))
1711
+ # Cannot have trailing '-'
1712
+ assert_raises(ValueError, np.array, ['1980-'], np.dtype('M8[us]'))
1713
+ # Month must be in range [1,12]
1714
+ assert_raises(ValueError, np.array, ['1980-00'], np.dtype('M8[us]'))
1715
+ assert_raises(ValueError, np.array, ['1980-13'], np.dtype('M8[us]'))
1716
+ # Month must have two digits
1717
+ assert_raises(ValueError, np.array, ['1980-1'], np.dtype('M8[us]'))
1718
+ assert_raises(ValueError, np.array, ['1980-1-02'], np.dtype('M8[us]'))
1719
+ # 'Mor' is not a valid month
1720
+ assert_raises(ValueError, np.array, ['1980-Mor'], np.dtype('M8[us]'))
1721
+ # Cannot have trailing '-'
1722
+ assert_raises(ValueError, np.array, ['1980-01-'], np.dtype('M8[us]'))
1723
+ # Day must be in range [1,len(month)]
1724
+ assert_raises(ValueError, np.array, ['1980-01-0'], np.dtype('M8[us]'))
1725
+ assert_raises(ValueError, np.array, ['1980-01-00'], np.dtype('M8[us]'))
1726
+ assert_raises(ValueError, np.array, ['1980-01-32'], np.dtype('M8[us]'))
1727
+ assert_raises(ValueError, np.array, ['1979-02-29'], np.dtype('M8[us]'))
1728
+ assert_raises(ValueError, np.array, ['1980-02-30'], np.dtype('M8[us]'))
1729
+ assert_raises(ValueError, np.array, ['1980-03-32'], np.dtype('M8[us]'))
1730
+ assert_raises(ValueError, np.array, ['1980-04-31'], np.dtype('M8[us]'))
1731
+ assert_raises(ValueError, np.array, ['1980-05-32'], np.dtype('M8[us]'))
1732
+ assert_raises(ValueError, np.array, ['1980-06-31'], np.dtype('M8[us]'))
1733
+ assert_raises(ValueError, np.array, ['1980-07-32'], np.dtype('M8[us]'))
1734
+ assert_raises(ValueError, np.array, ['1980-08-32'], np.dtype('M8[us]'))
1735
+ assert_raises(ValueError, np.array, ['1980-09-31'], np.dtype('M8[us]'))
1736
+ assert_raises(ValueError, np.array, ['1980-10-32'], np.dtype('M8[us]'))
1737
+ assert_raises(ValueError, np.array, ['1980-11-31'], np.dtype('M8[us]'))
1738
+ assert_raises(ValueError, np.array, ['1980-12-32'], np.dtype('M8[us]'))
1739
+ # Cannot have trailing characters
1740
+ assert_raises(ValueError, np.array, ['1980-02-03%'],
1741
+ np.dtype('M8[us]'))
1742
+ assert_raises(ValueError, np.array, ['1980-02-03 q'],
1743
+ np.dtype('M8[us]'))
1744
+
1745
+ # Hours must be in range [0, 23]
1746
+ assert_raises(ValueError, np.array, ['1980-02-03 25'],
1747
+ np.dtype('M8[us]'))
1748
+ assert_raises(ValueError, np.array, ['1980-02-03T25'],
1749
+ np.dtype('M8[us]'))
1750
+ assert_raises(ValueError, np.array, ['1980-02-03 24:01'],
1751
+ np.dtype('M8[us]'))
1752
+ assert_raises(ValueError, np.array, ['1980-02-03T24:01'],
1753
+ np.dtype('M8[us]'))
1754
+ assert_raises(ValueError, np.array, ['1980-02-03 -1'],
1755
+ np.dtype('M8[us]'))
1756
+ # No trailing ':'
1757
+ assert_raises(ValueError, np.array, ['1980-02-03 01:'],
1758
+ np.dtype('M8[us]'))
1759
+ # Minutes must be in range [0, 59]
1760
+ assert_raises(ValueError, np.array, ['1980-02-03 01:-1'],
1761
+ np.dtype('M8[us]'))
1762
+ assert_raises(ValueError, np.array, ['1980-02-03 01:60'],
1763
+ np.dtype('M8[us]'))
1764
+ # No trailing ':'
1765
+ assert_raises(ValueError, np.array, ['1980-02-03 01:60:'],
1766
+ np.dtype('M8[us]'))
1767
+ # Seconds must be in range [0, 59]
1768
+ assert_raises(ValueError, np.array, ['1980-02-03 01:10:-1'],
1769
+ np.dtype('M8[us]'))
1770
+ assert_raises(ValueError, np.array, ['1980-02-03 01:01:60'],
1771
+ np.dtype('M8[us]'))
1772
+ # Timezone offset must within a reasonable range
1773
+ with pytest.warns(UserWarning, match=msg):
1774
+ assert_raises(ValueError, np.array, ['1980-02-03 01:01:00+0661'],
1775
+ np.dtype('M8[us]'))
1776
+ with pytest.warns(UserWarning, match=msg):
1777
+ assert_raises(ValueError, np.array, ['1980-02-03 01:01:00+2500'],
1778
+ np.dtype('M8[us]'))
1779
+ with pytest.warns(UserWarning, match=msg):
1780
+ assert_raises(ValueError, np.array, ['1980-02-03 01:01:00-0070'],
1781
+ np.dtype('M8[us]'))
1782
+ with pytest.warns(UserWarning, match=msg):
1783
+ assert_raises(ValueError, np.array, ['1980-02-03 01:01:00-3000'],
1784
+ np.dtype('M8[us]'))
1785
+ with pytest.warns(UserWarning, match=msg):
1786
+ assert_raises(ValueError, np.array, ['1980-02-03 01:01:00-25:00'],
1787
+ np.dtype('M8[us]'))
1788
+
1789
+ def test_creation_overflow(self):
1790
+ date = '1980-03-23 20:00:00'
1791
+ timesteps = np.array([date], dtype='datetime64[s]')[0].astype(np.int64)
1792
+ for unit in ['ms', 'us', 'ns']:
1793
+ timesteps *= 1000
1794
+ x = np.array([date], dtype=f'datetime64[{unit}]')
1795
+
1796
+ assert_equal(timesteps, x[0].astype(np.int64),
1797
+ err_msg=f'Datetime conversion error for unit {unit}')
1798
+
1799
+ assert_equal(x[0].astype(np.int64), 322689600000000000)
1800
+
1801
+ # gh-13062
1802
+ with pytest.raises(OverflowError):
1803
+ np.datetime64(2**64, 'D')
1804
+ with pytest.raises(OverflowError):
1805
+ np.timedelta64(2**64, 'D')
1806
+
1807
+ def test_datetime_as_string(self):
1808
+ # Check all the units with default string conversion
1809
+ date = '1959-10-13'
1810
+ datetime = '1959-10-13T12:34:56.789012345678901234'
1811
+
1812
+ assert_equal(np.datetime_as_string(np.datetime64(date, 'Y')),
1813
+ '1959')
1814
+ assert_equal(np.datetime_as_string(np.datetime64(date, 'M')),
1815
+ '1959-10')
1816
+ assert_equal(np.datetime_as_string(np.datetime64(date, 'D')),
1817
+ '1959-10-13')
1818
+ assert_equal(np.datetime_as_string(np.datetime64(datetime, 'h')),
1819
+ '1959-10-13T12')
1820
+ assert_equal(np.datetime_as_string(np.datetime64(datetime, 'm')),
1821
+ '1959-10-13T12:34')
1822
+ assert_equal(np.datetime_as_string(np.datetime64(datetime, 's')),
1823
+ '1959-10-13T12:34:56')
1824
+ assert_equal(np.datetime_as_string(np.datetime64(datetime, 'ms')),
1825
+ '1959-10-13T12:34:56.789')
1826
+ for us in ['us', 'μs', b'us']: # check non-ascii and bytes too
1827
+ assert_equal(np.datetime_as_string(np.datetime64(datetime, us)),
1828
+ '1959-10-13T12:34:56.789012')
1829
+
1830
+ datetime = '1969-12-31T23:34:56.789012345678901234'
1831
+
1832
+ assert_equal(np.datetime_as_string(np.datetime64(datetime, 'ns')),
1833
+ '1969-12-31T23:34:56.789012345')
1834
+ assert_equal(np.datetime_as_string(np.datetime64(datetime, 'ps')),
1835
+ '1969-12-31T23:34:56.789012345678')
1836
+ assert_equal(np.datetime_as_string(np.datetime64(datetime, 'fs')),
1837
+ '1969-12-31T23:34:56.789012345678901')
1838
+
1839
+ datetime = '1969-12-31T23:59:57.789012345678901234'
1840
+
1841
+ assert_equal(np.datetime_as_string(np.datetime64(datetime, 'as')),
1842
+ datetime)
1843
+ datetime = '1970-01-01T00:34:56.789012345678901234'
1844
+
1845
+ assert_equal(np.datetime_as_string(np.datetime64(datetime, 'ns')),
1846
+ '1970-01-01T00:34:56.789012345')
1847
+ assert_equal(np.datetime_as_string(np.datetime64(datetime, 'ps')),
1848
+ '1970-01-01T00:34:56.789012345678')
1849
+ assert_equal(np.datetime_as_string(np.datetime64(datetime, 'fs')),
1850
+ '1970-01-01T00:34:56.789012345678901')
1851
+
1852
+ datetime = '1970-01-01T00:00:05.789012345678901234'
1853
+
1854
+ assert_equal(np.datetime_as_string(np.datetime64(datetime, 'as')),
1855
+ datetime)
1856
+
1857
+ # String conversion with the unit= parameter
1858
+ a = np.datetime64('2032-07-18T12:23:34.123456', 'us')
1859
+ assert_equal(np.datetime_as_string(a, unit='Y', casting='unsafe'),
1860
+ '2032')
1861
+ assert_equal(np.datetime_as_string(a, unit='M', casting='unsafe'),
1862
+ '2032-07')
1863
+ assert_equal(np.datetime_as_string(a, unit='W', casting='unsafe'),
1864
+ '2032-07-18')
1865
+ assert_equal(np.datetime_as_string(a, unit='D', casting='unsafe'),
1866
+ '2032-07-18')
1867
+ assert_equal(np.datetime_as_string(a, unit='h'), '2032-07-18T12')
1868
+ assert_equal(np.datetime_as_string(a, unit='m'),
1869
+ '2032-07-18T12:23')
1870
+ assert_equal(np.datetime_as_string(a, unit='s'),
1871
+ '2032-07-18T12:23:34')
1872
+ assert_equal(np.datetime_as_string(a, unit='ms'),
1873
+ '2032-07-18T12:23:34.123')
1874
+ assert_equal(np.datetime_as_string(a, unit='us'),
1875
+ '2032-07-18T12:23:34.123456')
1876
+ assert_equal(np.datetime_as_string(a, unit='ns'),
1877
+ '2032-07-18T12:23:34.123456000')
1878
+ assert_equal(np.datetime_as_string(a, unit='ps'),
1879
+ '2032-07-18T12:23:34.123456000000')
1880
+ assert_equal(np.datetime_as_string(a, unit='fs'),
1881
+ '2032-07-18T12:23:34.123456000000000')
1882
+ assert_equal(np.datetime_as_string(a, unit='as'),
1883
+ '2032-07-18T12:23:34.123456000000000000')
1884
+
1885
+ # unit='auto' parameter
1886
+ assert_equal(np.datetime_as_string(
1887
+ np.datetime64('2032-07-18T12:23:34.123456', 'us'), unit='auto'),
1888
+ '2032-07-18T12:23:34.123456')
1889
+ assert_equal(np.datetime_as_string(
1890
+ np.datetime64('2032-07-18T12:23:34.12', 'us'), unit='auto'),
1891
+ '2032-07-18T12:23:34.120')
1892
+ assert_equal(np.datetime_as_string(
1893
+ np.datetime64('2032-07-18T12:23:34', 'us'), unit='auto'),
1894
+ '2032-07-18T12:23:34')
1895
+ assert_equal(np.datetime_as_string(
1896
+ np.datetime64('2032-07-18T12:23:00', 'us'), unit='auto'),
1897
+ '2032-07-18T12:23')
1898
+ # 'auto' doesn't split up hour and minute
1899
+ assert_equal(np.datetime_as_string(
1900
+ np.datetime64('2032-07-18T12:00:00', 'us'), unit='auto'),
1901
+ '2032-07-18T12:00')
1902
+ assert_equal(np.datetime_as_string(
1903
+ np.datetime64('2032-07-18T00:00:00', 'us'), unit='auto'),
1904
+ '2032-07-18')
1905
+ # 'auto' doesn't split up the date
1906
+ assert_equal(np.datetime_as_string(
1907
+ np.datetime64('2032-07-01T00:00:00', 'us'), unit='auto'),
1908
+ '2032-07-01')
1909
+ assert_equal(np.datetime_as_string(
1910
+ np.datetime64('2032-01-01T00:00:00', 'us'), unit='auto'),
1911
+ '2032-01-01')
1912
+
1913
+ @pytest.mark.skipif(not _has_pytz, reason="The pytz module is not available.")
1914
+ def test_datetime_as_string_timezone(self):
1915
+ # timezone='local' vs 'UTC'
1916
+ a = np.datetime64('2010-03-15T06:30', 'm')
1917
+ assert_equal(np.datetime_as_string(a),
1918
+ '2010-03-15T06:30')
1919
+ assert_equal(np.datetime_as_string(a, timezone='naive'),
1920
+ '2010-03-15T06:30')
1921
+ assert_equal(np.datetime_as_string(a, timezone='UTC'),
1922
+ '2010-03-15T06:30Z')
1923
+ assert_(np.datetime_as_string(a, timezone='local') !=
1924
+ '2010-03-15T06:30')
1925
+
1926
+ b = np.datetime64('2010-02-15T06:30', 'm')
1927
+
1928
+ assert_equal(np.datetime_as_string(a, timezone=tz('US/Central')),
1929
+ '2010-03-15T01:30-0500')
1930
+ assert_equal(np.datetime_as_string(a, timezone=tz('US/Eastern')),
1931
+ '2010-03-15T02:30-0400')
1932
+ assert_equal(np.datetime_as_string(a, timezone=tz('US/Pacific')),
1933
+ '2010-03-14T23:30-0700')
1934
+
1935
+ assert_equal(np.datetime_as_string(b, timezone=tz('US/Central')),
1936
+ '2010-02-15T00:30-0600')
1937
+ assert_equal(np.datetime_as_string(b, timezone=tz('US/Eastern')),
1938
+ '2010-02-15T01:30-0500')
1939
+ assert_equal(np.datetime_as_string(b, timezone=tz('US/Pacific')),
1940
+ '2010-02-14T22:30-0800')
1941
+
1942
+ # Dates to strings with a timezone attached is disabled by default
1943
+ assert_raises(TypeError, np.datetime_as_string, a, unit='D',
1944
+ timezone=tz('US/Pacific'))
1945
+ # Check that we can print out the date in the specified time zone
1946
+ assert_equal(np.datetime_as_string(a, unit='D',
1947
+ timezone=tz('US/Pacific'), casting='unsafe'),
1948
+ '2010-03-14')
1949
+ assert_equal(np.datetime_as_string(b, unit='D',
1950
+ timezone=tz('US/Central'), casting='unsafe'),
1951
+ '2010-02-15')
1952
+
1953
+ def test_datetime_arange(self):
1954
+ # With two datetimes provided as strings
1955
+ a = np.arange('2010-01-05', '2010-01-10', dtype='M8[D]')
1956
+ assert_equal(a.dtype, np.dtype('M8[D]'))
1957
+ assert_equal(a,
1958
+ np.array(['2010-01-05', '2010-01-06', '2010-01-07',
1959
+ '2010-01-08', '2010-01-09'], dtype='M8[D]'))
1960
+
1961
+ a = np.arange('1950-02-10', '1950-02-06', -1, dtype='M8[D]')
1962
+ assert_equal(a.dtype, np.dtype('M8[D]'))
1963
+ assert_equal(a,
1964
+ np.array(['1950-02-10', '1950-02-09', '1950-02-08',
1965
+ '1950-02-07'], dtype='M8[D]'))
1966
+
1967
+ # Unit should be detected as months here
1968
+ a = np.arange('1969-05', '1970-05', 2, dtype='M8')
1969
+ assert_equal(a.dtype, np.dtype('M8[M]'))
1970
+ assert_equal(a,
1971
+ np.datetime64('1969-05') + np.arange(12, step=2))
1972
+
1973
+ # datetime, integer|timedelta works as well
1974
+ # produces arange (start, start + stop) in this case
1975
+ a = np.arange('1969', 18, 3, dtype='M8')
1976
+ assert_equal(a.dtype, np.dtype('M8[Y]'))
1977
+ assert_equal(a,
1978
+ np.datetime64('1969') + np.arange(18, step=3))
1979
+ a = np.arange('1969-12-19', 22, np.timedelta64(2), dtype='M8')
1980
+ assert_equal(a.dtype, np.dtype('M8[D]'))
1981
+ assert_equal(a,
1982
+ np.datetime64('1969-12-19') + np.arange(22, step=2))
1983
+
1984
+ # Step of 0 is disallowed
1985
+ assert_raises(ValueError, np.arange, np.datetime64('today'),
1986
+ np.datetime64('today') + 3, 0)
1987
+ # Promotion across nonlinear unit boundaries is disallowed
1988
+ assert_raises(TypeError, np.arange, np.datetime64('2011-03-01', 'D'),
1989
+ np.timedelta64(5, 'M'))
1990
+ assert_raises(TypeError, np.arange,
1991
+ np.datetime64('2012-02-03T14', 's'),
1992
+ np.timedelta64(5, 'Y'))
1993
+
1994
+ def test_datetime_arange_no_dtype(self):
1995
+ d = np.array('2010-01-04', dtype="M8[D]")
1996
+ assert_equal(np.arange(d, d + 1), d)
1997
+ assert_raises(ValueError, np.arange, d)
1998
+
1999
+ def test_timedelta_arange(self):
2000
+ a = np.arange(3, 10, dtype='m8')
2001
+ assert_equal(a.dtype, np.dtype('m8'))
2002
+ assert_equal(a, np.timedelta64(0) + np.arange(3, 10))
2003
+
2004
+ a = np.arange(np.timedelta64(3, 's'), 10, 2, dtype='m8')
2005
+ assert_equal(a.dtype, np.dtype('m8[s]'))
2006
+ assert_equal(a, np.timedelta64(0, 's') + np.arange(3, 10, 2))
2007
+
2008
+ # Step of 0 is disallowed
2009
+ assert_raises(ValueError, np.arange, np.timedelta64(0),
2010
+ np.timedelta64(5), 0)
2011
+ # Promotion across nonlinear unit boundaries is disallowed
2012
+ assert_raises(TypeError, np.arange, np.timedelta64(0, 'D'),
2013
+ np.timedelta64(5, 'M'))
2014
+ assert_raises(TypeError, np.arange, np.timedelta64(0, 'Y'),
2015
+ np.timedelta64(5, 'D'))
2016
+
2017
+ @pytest.mark.parametrize("val1, val2, expected", [
2018
+ # case from gh-12092
2019
+ (np.timedelta64(7, 's'),
2020
+ np.timedelta64(3, 's'),
2021
+ np.timedelta64(1, 's')),
2022
+ # negative value cases
2023
+ (np.timedelta64(3, 's'),
2024
+ np.timedelta64(-2, 's'),
2025
+ np.timedelta64(-1, 's')),
2026
+ (np.timedelta64(-3, 's'),
2027
+ np.timedelta64(2, 's'),
2028
+ np.timedelta64(1, 's')),
2029
+ # larger value cases
2030
+ (np.timedelta64(17, 's'),
2031
+ np.timedelta64(22, 's'),
2032
+ np.timedelta64(17, 's')),
2033
+ (np.timedelta64(22, 's'),
2034
+ np.timedelta64(17, 's'),
2035
+ np.timedelta64(5, 's')),
2036
+ # different units
2037
+ (np.timedelta64(1, 'm'),
2038
+ np.timedelta64(57, 's'),
2039
+ np.timedelta64(3, 's')),
2040
+ (np.timedelta64(1, 'us'),
2041
+ np.timedelta64(727, 'ns'),
2042
+ np.timedelta64(273, 'ns')),
2043
+ # NaT is propagated
2044
+ (np.timedelta64('NaT'),
2045
+ np.timedelta64(50, 'ns'),
2046
+ np.timedelta64('NaT')),
2047
+ # Y % M works
2048
+ (np.timedelta64(2, 'Y'),
2049
+ np.timedelta64(22, 'M'),
2050
+ np.timedelta64(2, 'M')),
2051
+ ])
2052
+ def test_timedelta_modulus(self, val1, val2, expected):
2053
+ assert_equal(val1 % val2, expected)
2054
+
2055
+ @pytest.mark.parametrize("val1, val2", [
2056
+ # years and months sometimes can't be unambiguously
2057
+ # divided for modulus operation
2058
+ (np.timedelta64(7, 'Y'),
2059
+ np.timedelta64(3, 's')),
2060
+ (np.timedelta64(7, 'M'),
2061
+ np.timedelta64(1, 'D')),
2062
+ ])
2063
+ def test_timedelta_modulus_error(self, val1, val2):
2064
+ with assert_raises_regex(TypeError, "common metadata divisor"):
2065
+ val1 % val2
2066
+
2067
+ @pytest.mark.skipif(IS_WASM, reason="fp errors don't work in wasm")
2068
+ def test_timedelta_modulus_div_by_zero(self):
2069
+ with assert_warns(RuntimeWarning):
2070
+ actual = np.timedelta64(10, 's') % np.timedelta64(0, 's')
2071
+ assert_equal(actual, np.timedelta64('NaT'))
2072
+
2073
+ @pytest.mark.parametrize("val1, val2", [
2074
+ # cases where one operand is not
2075
+ # timedelta64
2076
+ (np.timedelta64(7, 'Y'),
2077
+ 15,),
2078
+ (7.5,
2079
+ np.timedelta64(1, 'D')),
2080
+ ])
2081
+ def test_timedelta_modulus_type_resolution(self, val1, val2):
2082
+ # NOTE: some of the operations may be supported
2083
+ # in the future
2084
+ with assert_raises_regex(TypeError,
2085
+ "'remainder' cannot use operands with types"):
2086
+ val1 % val2
2087
+
2088
+ def test_timedelta_arange_no_dtype(self):
2089
+ d = np.array(5, dtype="m8[D]")
2090
+ assert_equal(np.arange(d, d + 1), d)
2091
+ assert_equal(np.arange(d), np.arange(0, d))
2092
+
2093
+ def test_datetime_maximum_reduce(self):
2094
+ a = np.array(['2010-01-02', '1999-03-14', '1833-03'], dtype='M8[D]')
2095
+ assert_equal(np.maximum.reduce(a).dtype, np.dtype('M8[D]'))
2096
+ assert_equal(np.maximum.reduce(a),
2097
+ np.datetime64('2010-01-02'))
2098
+
2099
+ a = np.array([1, 4, 0, 7, 2], dtype='m8[s]')
2100
+ assert_equal(np.maximum.reduce(a).dtype, np.dtype('m8[s]'))
2101
+ assert_equal(np.maximum.reduce(a),
2102
+ np.timedelta64(7, 's'))
2103
+
2104
+ def test_timedelta_correct_mean(self):
2105
+ # test mainly because it worked only via a bug in that allowed:
2106
+ # `timedelta.sum(dtype="f8")` to ignore the dtype request.
2107
+ a = np.arange(1000, dtype="m8[s]")
2108
+ assert_array_equal(a.mean(), a.sum() / len(a))
2109
+
2110
+ def test_datetime_no_subtract_reducelike(self):
2111
+ # subtracting two datetime64 works, but we cannot reduce it, since
2112
+ # the result of that subtraction will have a different dtype.
2113
+ arr = np.array(["2021-12-02", "2019-05-12"], dtype="M8[ms]")
2114
+ msg = r"the resolved dtypes are not compatible"
2115
+
2116
+ with pytest.raises(TypeError, match=msg):
2117
+ np.subtract.reduce(arr)
2118
+
2119
+ with pytest.raises(TypeError, match=msg):
2120
+ np.subtract.accumulate(arr)
2121
+
2122
+ with pytest.raises(TypeError, match=msg):
2123
+ np.subtract.reduceat(arr, [0])
2124
+
2125
+ def test_datetime_busday_offset(self):
2126
+ # First Monday in June
2127
+ assert_equal(
2128
+ np.busday_offset('2011-06', 0, roll='forward', weekmask='Mon'),
2129
+ np.datetime64('2011-06-06'))
2130
+ # Last Monday in June
2131
+ assert_equal(
2132
+ np.busday_offset('2011-07', -1, roll='forward', weekmask='Mon'),
2133
+ np.datetime64('2011-06-27'))
2134
+ assert_equal(
2135
+ np.busday_offset('2011-07', -1, roll='forward', weekmask='Mon'),
2136
+ np.datetime64('2011-06-27'))
2137
+
2138
+ # Default M-F business days, different roll modes
2139
+ assert_equal(np.busday_offset('2010-08', 0, roll='backward'),
2140
+ np.datetime64('2010-07-30'))
2141
+ assert_equal(np.busday_offset('2010-08', 0, roll='preceding'),
2142
+ np.datetime64('2010-07-30'))
2143
+ assert_equal(np.busday_offset('2010-08', 0, roll='modifiedpreceding'),
2144
+ np.datetime64('2010-08-02'))
2145
+ assert_equal(np.busday_offset('2010-08', 0, roll='modifiedfollowing'),
2146
+ np.datetime64('2010-08-02'))
2147
+ assert_equal(np.busday_offset('2010-08', 0, roll='forward'),
2148
+ np.datetime64('2010-08-02'))
2149
+ assert_equal(np.busday_offset('2010-08', 0, roll='following'),
2150
+ np.datetime64('2010-08-02'))
2151
+ assert_equal(np.busday_offset('2010-10-30', 0, roll='following'),
2152
+ np.datetime64('2010-11-01'))
2153
+ assert_equal(
2154
+ np.busday_offset('2010-10-30', 0, roll='modifiedfollowing'),
2155
+ np.datetime64('2010-10-29'))
2156
+ assert_equal(
2157
+ np.busday_offset('2010-10-30', 0, roll='modifiedpreceding'),
2158
+ np.datetime64('2010-10-29'))
2159
+ assert_equal(
2160
+ np.busday_offset('2010-10-16', 0, roll='modifiedfollowing'),
2161
+ np.datetime64('2010-10-18'))
2162
+ assert_equal(
2163
+ np.busday_offset('2010-10-16', 0, roll='modifiedpreceding'),
2164
+ np.datetime64('2010-10-15'))
2165
+ # roll='raise' by default
2166
+ assert_raises(ValueError, np.busday_offset, '2011-06-04', 0)
2167
+
2168
+ # Bigger offset values
2169
+ assert_equal(np.busday_offset('2006-02-01', 25),
2170
+ np.datetime64('2006-03-08'))
2171
+ assert_equal(np.busday_offset('2006-03-08', -25),
2172
+ np.datetime64('2006-02-01'))
2173
+ assert_equal(np.busday_offset('2007-02-25', 11, weekmask='SatSun'),
2174
+ np.datetime64('2007-04-07'))
2175
+ assert_equal(np.busday_offset('2007-04-07', -11, weekmask='SatSun'),
2176
+ np.datetime64('2007-02-25'))
2177
+
2178
+ # NaT values when roll is not raise
2179
+ assert_equal(np.busday_offset(np.datetime64('NaT'), 1, roll='nat'),
2180
+ np.datetime64('NaT'))
2181
+ assert_equal(np.busday_offset(np.datetime64('NaT'), 1, roll='following'),
2182
+ np.datetime64('NaT'))
2183
+ assert_equal(np.busday_offset(np.datetime64('NaT'), 1, roll='preceding'),
2184
+ np.datetime64('NaT'))
2185
+
2186
+ def test_datetime_busdaycalendar(self):
2187
+ # Check that it removes NaT, duplicates, and weekends
2188
+ # and sorts the result.
2189
+ bdd = np.busdaycalendar(
2190
+ holidays=['NaT', '2011-01-17', '2011-03-06', 'NaT',
2191
+ '2011-12-26', '2011-05-30', '2011-01-17'])
2192
+ assert_equal(bdd.holidays,
2193
+ np.array(['2011-01-17', '2011-05-30', '2011-12-26'], dtype='M8'))
2194
+ # Default M-F weekmask
2195
+ assert_equal(bdd.weekmask, np.array([1, 1, 1, 1, 1, 0, 0], dtype='?'))
2196
+
2197
+ # Check string weekmask with varying whitespace.
2198
+ bdd = np.busdaycalendar(weekmask="Sun TueWed Thu\tFri")
2199
+ assert_equal(bdd.weekmask, np.array([0, 1, 1, 1, 1, 0, 1], dtype='?'))
2200
+
2201
+ # Check length 7 0/1 string
2202
+ bdd = np.busdaycalendar(weekmask="0011001")
2203
+ assert_equal(bdd.weekmask, np.array([0, 0, 1, 1, 0, 0, 1], dtype='?'))
2204
+
2205
+ # Check length 7 string weekmask.
2206
+ bdd = np.busdaycalendar(weekmask="Mon Tue")
2207
+ assert_equal(bdd.weekmask, np.array([1, 1, 0, 0, 0, 0, 0], dtype='?'))
2208
+
2209
+ # All-zeros weekmask should raise
2210
+ assert_raises(ValueError, np.busdaycalendar, weekmask=[0, 0, 0, 0, 0, 0, 0])
2211
+ # weekday names must be correct case
2212
+ assert_raises(ValueError, np.busdaycalendar, weekmask="satsun")
2213
+ # All-zeros weekmask should raise
2214
+ assert_raises(ValueError, np.busdaycalendar, weekmask="")
2215
+ # Invalid weekday name codes should raise
2216
+ assert_raises(ValueError, np.busdaycalendar, weekmask="Mon Tue We")
2217
+ assert_raises(ValueError, np.busdaycalendar, weekmask="Max")
2218
+ assert_raises(ValueError, np.busdaycalendar, weekmask="Monday Tue")
2219
+
2220
+ def test_datetime_busday_holidays_offset(self):
2221
+ # With exactly one holiday
2222
+ assert_equal(
2223
+ np.busday_offset('2011-11-10', 1, holidays=['2011-11-11']),
2224
+ np.datetime64('2011-11-14'))
2225
+ assert_equal(
2226
+ np.busday_offset('2011-11-04', 5, holidays=['2011-11-11']),
2227
+ np.datetime64('2011-11-14'))
2228
+ assert_equal(
2229
+ np.busday_offset('2011-11-10', 5, holidays=['2011-11-11']),
2230
+ np.datetime64('2011-11-18'))
2231
+ assert_equal(
2232
+ np.busday_offset('2011-11-14', -1, holidays=['2011-11-11']),
2233
+ np.datetime64('2011-11-10'))
2234
+ assert_equal(
2235
+ np.busday_offset('2011-11-18', -5, holidays=['2011-11-11']),
2236
+ np.datetime64('2011-11-10'))
2237
+ assert_equal(
2238
+ np.busday_offset('2011-11-14', -5, holidays=['2011-11-11']),
2239
+ np.datetime64('2011-11-04'))
2240
+ # With the holiday appearing twice
2241
+ assert_equal(
2242
+ np.busday_offset('2011-11-10', 1,
2243
+ holidays=['2011-11-11', '2011-11-11']),
2244
+ np.datetime64('2011-11-14'))
2245
+ assert_equal(
2246
+ np.busday_offset('2011-11-14', -1,
2247
+ holidays=['2011-11-11', '2011-11-11']),
2248
+ np.datetime64('2011-11-10'))
2249
+ # With a NaT holiday
2250
+ assert_equal(
2251
+ np.busday_offset('2011-11-10', 1,
2252
+ holidays=['2011-11-11', 'NaT']),
2253
+ np.datetime64('2011-11-14'))
2254
+ assert_equal(
2255
+ np.busday_offset('2011-11-14', -1,
2256
+ holidays=['NaT', '2011-11-11']),
2257
+ np.datetime64('2011-11-10'))
2258
+ # With another holiday after
2259
+ assert_equal(
2260
+ np.busday_offset('2011-11-10', 1,
2261
+ holidays=['2011-11-11', '2011-11-24']),
2262
+ np.datetime64('2011-11-14'))
2263
+ assert_equal(
2264
+ np.busday_offset('2011-11-14', -1,
2265
+ holidays=['2011-11-11', '2011-11-24']),
2266
+ np.datetime64('2011-11-10'))
2267
+ # With another holiday before
2268
+ assert_equal(
2269
+ np.busday_offset('2011-11-10', 1,
2270
+ holidays=['2011-10-10', '2011-11-11']),
2271
+ np.datetime64('2011-11-14'))
2272
+ assert_equal(
2273
+ np.busday_offset('2011-11-14', -1,
2274
+ holidays=['2011-10-10', '2011-11-11']),
2275
+ np.datetime64('2011-11-10'))
2276
+ # With another holiday before and after
2277
+ assert_equal(
2278
+ np.busday_offset('2011-11-10', 1,
2279
+ holidays=['2011-10-10', '2011-11-11', '2011-11-24']),
2280
+ np.datetime64('2011-11-14'))
2281
+ assert_equal(
2282
+ np.busday_offset('2011-11-14', -1,
2283
+ holidays=['2011-10-10', '2011-11-11', '2011-11-24']),
2284
+ np.datetime64('2011-11-10'))
2285
+
2286
+ # A bigger forward jump across more than one week/holiday
2287
+ holidays = ['2011-10-10', '2011-11-11', '2011-11-24',
2288
+ '2011-12-25', '2011-05-30', '2011-02-21',
2289
+ '2011-12-26', '2012-01-02']
2290
+ bdd = np.busdaycalendar(weekmask='1111100', holidays=holidays)
2291
+ assert_equal(
2292
+ np.busday_offset('2011-10-03', 4, holidays=holidays),
2293
+ np.busday_offset('2011-10-03', 4))
2294
+ assert_equal(
2295
+ np.busday_offset('2011-10-03', 5, holidays=holidays),
2296
+ np.busday_offset('2011-10-03', 5 + 1))
2297
+ assert_equal(
2298
+ np.busday_offset('2011-10-03', 27, holidays=holidays),
2299
+ np.busday_offset('2011-10-03', 27 + 1))
2300
+ assert_equal(
2301
+ np.busday_offset('2011-10-03', 28, holidays=holidays),
2302
+ np.busday_offset('2011-10-03', 28 + 2))
2303
+ assert_equal(
2304
+ np.busday_offset('2011-10-03', 35, holidays=holidays),
2305
+ np.busday_offset('2011-10-03', 35 + 2))
2306
+ assert_equal(
2307
+ np.busday_offset('2011-10-03', 36, holidays=holidays),
2308
+ np.busday_offset('2011-10-03', 36 + 3))
2309
+ assert_equal(
2310
+ np.busday_offset('2011-10-03', 56, holidays=holidays),
2311
+ np.busday_offset('2011-10-03', 56 + 3))
2312
+ assert_equal(
2313
+ np.busday_offset('2011-10-03', 57, holidays=holidays),
2314
+ np.busday_offset('2011-10-03', 57 + 4))
2315
+ assert_equal(
2316
+ np.busday_offset('2011-10-03', 60, holidays=holidays),
2317
+ np.busday_offset('2011-10-03', 60 + 4))
2318
+ assert_equal(
2319
+ np.busday_offset('2011-10-03', 61, holidays=holidays),
2320
+ np.busday_offset('2011-10-03', 61 + 5))
2321
+ assert_equal(
2322
+ np.busday_offset('2011-10-03', 61, busdaycal=bdd),
2323
+ np.busday_offset('2011-10-03', 61 + 5))
2324
+ # A bigger backward jump across more than one week/holiday
2325
+ assert_equal(
2326
+ np.busday_offset('2012-01-03', -1, holidays=holidays),
2327
+ np.busday_offset('2012-01-03', -1 - 1))
2328
+ assert_equal(
2329
+ np.busday_offset('2012-01-03', -4, holidays=holidays),
2330
+ np.busday_offset('2012-01-03', -4 - 1))
2331
+ assert_equal(
2332
+ np.busday_offset('2012-01-03', -5, holidays=holidays),
2333
+ np.busday_offset('2012-01-03', -5 - 2))
2334
+ assert_equal(
2335
+ np.busday_offset('2012-01-03', -25, holidays=holidays),
2336
+ np.busday_offset('2012-01-03', -25 - 2))
2337
+ assert_equal(
2338
+ np.busday_offset('2012-01-03', -26, holidays=holidays),
2339
+ np.busday_offset('2012-01-03', -26 - 3))
2340
+ assert_equal(
2341
+ np.busday_offset('2012-01-03', -33, holidays=holidays),
2342
+ np.busday_offset('2012-01-03', -33 - 3))
2343
+ assert_equal(
2344
+ np.busday_offset('2012-01-03', -34, holidays=holidays),
2345
+ np.busday_offset('2012-01-03', -34 - 4))
2346
+ assert_equal(
2347
+ np.busday_offset('2012-01-03', -56, holidays=holidays),
2348
+ np.busday_offset('2012-01-03', -56 - 4))
2349
+ assert_equal(
2350
+ np.busday_offset('2012-01-03', -57, holidays=holidays),
2351
+ np.busday_offset('2012-01-03', -57 - 5))
2352
+ assert_equal(
2353
+ np.busday_offset('2012-01-03', -57, busdaycal=bdd),
2354
+ np.busday_offset('2012-01-03', -57 - 5))
2355
+
2356
+ # Can't supply both a weekmask/holidays and busdaycal
2357
+ assert_raises(ValueError, np.busday_offset, '2012-01-03', -15,
2358
+ weekmask='1111100', busdaycal=bdd)
2359
+ assert_raises(ValueError, np.busday_offset, '2012-01-03', -15,
2360
+ holidays=holidays, busdaycal=bdd)
2361
+
2362
+ # Roll with the holidays
2363
+ assert_equal(
2364
+ np.busday_offset('2011-12-25', 0,
2365
+ roll='forward', holidays=holidays),
2366
+ np.datetime64('2011-12-27'))
2367
+ assert_equal(
2368
+ np.busday_offset('2011-12-26', 0,
2369
+ roll='forward', holidays=holidays),
2370
+ np.datetime64('2011-12-27'))
2371
+ assert_equal(
2372
+ np.busday_offset('2011-12-26', 0,
2373
+ roll='backward', holidays=holidays),
2374
+ np.datetime64('2011-12-23'))
2375
+ assert_equal(
2376
+ np.busday_offset('2012-02-27', 0,
2377
+ roll='modifiedfollowing',
2378
+ holidays=['2012-02-27', '2012-02-26', '2012-02-28',
2379
+ '2012-03-01', '2012-02-29']),
2380
+ np.datetime64('2012-02-24'))
2381
+ assert_equal(
2382
+ np.busday_offset('2012-03-06', 0,
2383
+ roll='modifiedpreceding',
2384
+ holidays=['2012-03-02', '2012-03-03', '2012-03-01',
2385
+ '2012-03-05', '2012-03-07', '2012-03-06']),
2386
+ np.datetime64('2012-03-08'))
2387
+
2388
+ def test_datetime_busday_holidays_count(self):
2389
+ holidays = ['2011-01-01', '2011-10-10', '2011-11-11', '2011-11-24',
2390
+ '2011-12-25', '2011-05-30', '2011-02-21', '2011-01-17',
2391
+ '2011-12-26', '2012-01-02', '2011-02-21', '2011-05-30',
2392
+ '2011-07-01', '2011-07-04', '2011-09-05', '2011-10-10']
2393
+ bdd = np.busdaycalendar(weekmask='1111100', holidays=holidays)
2394
+
2395
+ # Validate against busday_offset broadcast against
2396
+ # a range of offsets
2397
+ dates = np.busday_offset('2011-01-01', np.arange(366),
2398
+ roll='forward', busdaycal=bdd)
2399
+ assert_equal(np.busday_count('2011-01-01', dates, busdaycal=bdd),
2400
+ np.arange(366))
2401
+ # Returns negative value when reversed
2402
+ # -1 since the '2011-01-01' is not a busday
2403
+ assert_equal(np.busday_count(dates, '2011-01-01', busdaycal=bdd),
2404
+ -np.arange(366) - 1)
2405
+
2406
+ # 2011-12-31 is a saturday
2407
+ dates = np.busday_offset('2011-12-31', -np.arange(366),
2408
+ roll='forward', busdaycal=bdd)
2409
+ # only the first generated date is in the future of 2011-12-31
2410
+ expected = np.arange(366)
2411
+ expected[0] = -1
2412
+ assert_equal(np.busday_count(dates, '2011-12-31', busdaycal=bdd),
2413
+ expected)
2414
+ # Returns negative value when reversed
2415
+ expected = -np.arange(366) + 1
2416
+ expected[0] = 0
2417
+ assert_equal(np.busday_count('2011-12-31', dates, busdaycal=bdd),
2418
+ expected)
2419
+
2420
+ # Can't supply both a weekmask/holidays and busdaycal
2421
+ assert_raises(ValueError, np.busday_offset, '2012-01-03', '2012-02-03',
2422
+ weekmask='1111100', busdaycal=bdd)
2423
+ assert_raises(ValueError, np.busday_offset, '2012-01-03', '2012-02-03',
2424
+ holidays=holidays, busdaycal=bdd)
2425
+
2426
+ # Number of Mondays in March 2011
2427
+ assert_equal(np.busday_count('2011-03', '2011-04', weekmask='Mon'), 4)
2428
+ # Returns negative value when reversed
2429
+ assert_equal(np.busday_count('2011-04', '2011-03', weekmask='Mon'), -4)
2430
+
2431
+ sunday = np.datetime64('2023-03-05')
2432
+ monday = sunday + 1
2433
+ friday = sunday + 5
2434
+ saturday = sunday + 6
2435
+ assert_equal(np.busday_count(sunday, monday), 0)
2436
+ assert_equal(np.busday_count(monday, sunday), -1)
2437
+
2438
+ assert_equal(np.busday_count(friday, saturday), 1)
2439
+ assert_equal(np.busday_count(saturday, friday), 0)
2440
+
2441
+ def test_datetime_is_busday(self):
2442
+ holidays = ['2011-01-01', '2011-10-10', '2011-11-11', '2011-11-24',
2443
+ '2011-12-25', '2011-05-30', '2011-02-21', '2011-01-17',
2444
+ '2011-12-26', '2012-01-02', '2011-02-21', '2011-05-30',
2445
+ '2011-07-01', '2011-07-04', '2011-09-05', '2011-10-10',
2446
+ 'NaT']
2447
+ bdd = np.busdaycalendar(weekmask='1111100', holidays=holidays)
2448
+
2449
+ # Weekend/weekday tests
2450
+ assert_equal(np.is_busday('2011-01-01'), False)
2451
+ assert_equal(np.is_busday('2011-01-02'), False)
2452
+ assert_equal(np.is_busday('2011-01-03'), True)
2453
+
2454
+ # All the holidays are not business days
2455
+ assert_equal(np.is_busday(holidays, busdaycal=bdd),
2456
+ np.zeros(len(holidays), dtype='?'))
2457
+
2458
+ def test_datetime_y2038(self):
2459
+ msg = "no explicit representation of timezones available for " \
2460
+ "np.datetime64"
2461
+ # Test parsing on either side of the Y2038 boundary
2462
+ a = np.datetime64('2038-01-19T03:14:07')
2463
+ assert_equal(a.view(np.int64), 2**31 - 1)
2464
+ a = np.datetime64('2038-01-19T03:14:08')
2465
+ assert_equal(a.view(np.int64), 2**31)
2466
+
2467
+ # Test parsing on either side of the Y2038 boundary with
2468
+ # a manually specified timezone offset
2469
+ with pytest.warns(UserWarning, match=msg):
2470
+ a = np.datetime64('2038-01-19T04:14:07+0100')
2471
+ assert_equal(a.view(np.int64), 2**31 - 1)
2472
+ with pytest.warns(UserWarning, match=msg):
2473
+ a = np.datetime64('2038-01-19T04:14:08+0100')
2474
+ assert_equal(a.view(np.int64), 2**31)
2475
+
2476
+ # Test parsing a date after Y2038
2477
+ a = np.datetime64('2038-01-20T13:21:14')
2478
+ assert_equal(str(a), '2038-01-20T13:21:14')
2479
+
2480
+ def test_isnat(self):
2481
+ assert_(np.isnat(np.datetime64('NaT', 'ms')))
2482
+ assert_(np.isnat(np.datetime64('NaT', 'ns')))
2483
+ assert_(not np.isnat(np.datetime64('2038-01-19T03:14:07')))
2484
+
2485
+ assert_(np.isnat(np.timedelta64('NaT', "ms")))
2486
+ assert_(not np.isnat(np.timedelta64(34, "ms")))
2487
+
2488
+ res = np.array([False, False, True])
2489
+ for unit in ['Y', 'M', 'W', 'D',
2490
+ 'h', 'm', 's', 'ms', 'us',
2491
+ 'ns', 'ps', 'fs', 'as']:
2492
+ arr = np.array([123, -321, "NaT"], dtype=f'<datetime64[{unit}]')
2493
+ assert_equal(np.isnat(arr), res)
2494
+ arr = np.array([123, -321, "NaT"], dtype=f'>datetime64[{unit}]')
2495
+ assert_equal(np.isnat(arr), res)
2496
+ arr = np.array([123, -321, "NaT"], dtype=f'<timedelta64[{unit}]')
2497
+ assert_equal(np.isnat(arr), res)
2498
+ arr = np.array([123, -321, "NaT"], dtype=f'>timedelta64[{unit}]')
2499
+ assert_equal(np.isnat(arr), res)
2500
+
2501
+ def test_isnat_error(self):
2502
+ # Test that only datetime dtype arrays are accepted
2503
+ for t in np.typecodes["All"]:
2504
+ if t in np.typecodes["Datetime"]:
2505
+ continue
2506
+ assert_raises(TypeError, np.isnat, np.zeros(10, t))
2507
+
2508
+ def test_isfinite_scalar(self):
2509
+ assert_(not np.isfinite(np.datetime64('NaT', 'ms')))
2510
+ assert_(not np.isfinite(np.datetime64('NaT', 'ns')))
2511
+ assert_(np.isfinite(np.datetime64('2038-01-19T03:14:07')))
2512
+
2513
+ assert_(not np.isfinite(np.timedelta64('NaT', "ms")))
2514
+ assert_(np.isfinite(np.timedelta64(34, "ms")))
2515
+
2516
+ @pytest.mark.parametrize('unit', ['Y', 'M', 'W', 'D', 'h', 'm', 's', 'ms',
2517
+ 'us', 'ns', 'ps', 'fs', 'as'])
2518
+ @pytest.mark.parametrize('dstr', ['<datetime64[%s]', '>datetime64[%s]',
2519
+ '<timedelta64[%s]', '>timedelta64[%s]'])
2520
+ def test_isfinite_isinf_isnan_units(self, unit, dstr):
2521
+ '''check isfinite, isinf, isnan for all units of <M, >M, <m, >m dtypes
2522
+ '''
2523
+ arr_val = [123, -321, "NaT"]
2524
+ arr = np.array(arr_val, dtype=(dstr % unit))
2525
+ pos = np.array([True, True, False])
2526
+ neg = np.array([False, False, True])
2527
+ false = np.array([False, False, False])
2528
+ assert_equal(np.isfinite(arr), pos)
2529
+ assert_equal(np.isinf(arr), false)
2530
+ assert_equal(np.isnan(arr), neg)
2531
+
2532
+ def test_assert_equal(self):
2533
+ assert_raises(AssertionError, assert_equal,
2534
+ np.datetime64('nat'), np.timedelta64('nat'))
2535
+
2536
+ def test_corecursive_input(self):
2537
+ # construct a co-recursive list
2538
+ a, b = [], []
2539
+ a.append(b)
2540
+ b.append(a)
2541
+ obj_arr = np.array([None])
2542
+ obj_arr[0] = a
2543
+
2544
+ # At some point this caused a stack overflow (gh-11154). Now raises
2545
+ # ValueError since the nested list cannot be converted to a datetime.
2546
+ assert_raises(ValueError, obj_arr.astype, 'M8')
2547
+ assert_raises(ValueError, obj_arr.astype, 'm8')
2548
+
2549
+ @pytest.mark.parametrize("shape", [(), (1,)])
2550
+ def test_discovery_from_object_array(self, shape):
2551
+ arr = np.array("2020-10-10", dtype=object).reshape(shape)
2552
+ res = np.array("2020-10-10", dtype="M8").reshape(shape)
2553
+ assert res.dtype == np.dtype("M8[D]")
2554
+ assert_equal(arr.astype("M8"), res)
2555
+ arr[...] = np.bytes_("2020-10-10") # try a numpy string type
2556
+ assert_equal(arr.astype("M8"), res)
2557
+ arr = arr.astype("S")
2558
+ assert_equal(arr.astype("S").astype("M8"), res)
2559
+
2560
+ @pytest.mark.parametrize("time_unit", [
2561
+ "Y", "M", "W", "D", "h", "m", "s", "ms", "us", "ns", "ps", "fs", "as",
2562
+ # compound units
2563
+ "10D", "2M",
2564
+ ])
2565
+ def test_limit_symmetry(self, time_unit):
2566
+ """
2567
+ Dates should have symmetric limits around the unix epoch at +/-np.int64
2568
+ """
2569
+ epoch = np.datetime64(0, time_unit)
2570
+ latest = np.datetime64(np.iinfo(np.int64).max, time_unit)
2571
+ earliest = np.datetime64(-np.iinfo(np.int64).max, time_unit)
2572
+
2573
+ # above should not have overflowed
2574
+ assert earliest < epoch < latest
2575
+
2576
+ @pytest.mark.parametrize("time_unit", [
2577
+ "Y", "M",
2578
+ pytest.param("W", marks=pytest.mark.xfail(reason="gh-13197")),
2579
+ "D", "h", "m",
2580
+ "s", "ms", "us", "ns", "ps", "fs", "as",
2581
+ pytest.param("10D", marks=pytest.mark.xfail(reason="similar to gh-13197")),
2582
+ ])
2583
+ @pytest.mark.parametrize("sign", [-1, 1])
2584
+ def test_limit_str_roundtrip(self, time_unit, sign):
2585
+ """
2586
+ Limits should roundtrip when converted to strings.
2587
+
2588
+ This tests the conversion to and from npy_datetimestruct.
2589
+ """
2590
+ # TODO: add absolute (gold standard) time span limit strings
2591
+ limit = np.datetime64(np.iinfo(np.int64).max * sign, time_unit)
2592
+
2593
+ # Convert to string and back. Explicit unit needed since the day and
2594
+ # week reprs are not distinguishable.
2595
+ limit_via_str = np.datetime64(str(limit), time_unit)
2596
+ assert limit_via_str == limit
2597
+
2598
+ def test_datetime_hash_nat(self):
2599
+ nat1 = np.datetime64()
2600
+ nat2 = np.datetime64()
2601
+ assert nat1 is not nat2
2602
+ assert nat1 != nat2
2603
+ assert hash(nat1) != hash(nat2)
2604
+
2605
+ @pytest.mark.parametrize('unit', ('Y', 'M', 'W', 'D', 'h', 'm', 's', 'ms', 'us'))
2606
+ def test_datetime_hash_weeks(self, unit):
2607
+ dt = np.datetime64(2348, 'W') # 2015-01-01
2608
+ dt2 = np.datetime64(dt, unit)
2609
+ _assert_equal_hash(dt, dt2)
2610
+
2611
+ dt3 = np.datetime64(int(dt2.astype(int)) + 1, unit)
2612
+ assert hash(dt) != hash(dt3) # doesn't collide
2613
+
2614
+ @pytest.mark.parametrize('unit', ('h', 'm', 's', 'ms', 'us'))
2615
+ def test_datetime_hash_weeks_vs_pydatetime(self, unit):
2616
+ dt = np.datetime64(2348, 'W') # 2015-01-01
2617
+ dt2 = np.datetime64(dt, unit)
2618
+ pydt = dt2.astype(datetime.datetime)
2619
+ assert isinstance(pydt, datetime.datetime)
2620
+ _assert_equal_hash(pydt, dt2)
2621
+
2622
+ @pytest.mark.parametrize('unit', ('Y', 'M', 'W', 'D', 'h', 'm', 's', 'ms', 'us'))
2623
+ def test_datetime_hash_big_negative(self, unit):
2624
+ dt = np.datetime64(-102894, 'W') # -002-01-01
2625
+ dt2 = np.datetime64(dt, unit)
2626
+ _assert_equal_hash(dt, dt2)
2627
+
2628
+ # can only go down to "fs" before integer overflow
2629
+ @pytest.mark.parametrize('unit', ('m', 's', 'ms', 'us', 'ns', 'ps', 'fs'))
2630
+ def test_datetime_hash_minutes(self, unit):
2631
+ dt = np.datetime64(3, 'm')
2632
+ dt2 = np.datetime64(dt, unit)
2633
+ _assert_equal_hash(dt, dt2)
2634
+
2635
+ @pytest.mark.parametrize('unit', ('ns', 'ps', 'fs', 'as'))
2636
+ def test_datetime_hash_ns(self, unit):
2637
+ dt = np.datetime64(3, 'ns')
2638
+ dt2 = np.datetime64(dt, unit)
2639
+ _assert_equal_hash(dt, dt2)
2640
+
2641
+ dt3 = np.datetime64(int(dt2.astype(int)) + 1, unit)
2642
+ assert hash(dt) != hash(dt3) # doesn't collide
2643
+
2644
+ @pytest.mark.parametrize('wk', range(500000, 500010)) # 11552-09-04
2645
+ @pytest.mark.parametrize('unit', ('W', 'D', 'h', 'm', 's', 'ms', 'us'))
2646
+ def test_datetime_hash_big_positive(self, wk, unit):
2647
+ dt = np.datetime64(wk, 'W')
2648
+ dt2 = np.datetime64(dt, unit)
2649
+ _assert_equal_hash(dt, dt2)
2650
+
2651
+ def test_timedelta_hash_generic(self):
2652
+ assert_raises(ValueError, hash, np.timedelta64(123)) # generic
2653
+
2654
+ @pytest.mark.parametrize('unit', ('Y', 'M'))
2655
+ def test_timedelta_hash_year_month(self, unit):
2656
+ td = np.timedelta64(45, 'Y')
2657
+ td2 = np.timedelta64(td, unit)
2658
+ _assert_equal_hash(td, td2)
2659
+
2660
+ @pytest.mark.parametrize('unit', ('W', 'D', 'h', 'm', 's', 'ms', 'us'))
2661
+ def test_timedelta_hash_weeks(self, unit):
2662
+ td = np.timedelta64(10, 'W')
2663
+ td2 = np.timedelta64(td, unit)
2664
+ _assert_equal_hash(td, td2)
2665
+
2666
+ td3 = np.timedelta64(int(td2.astype(int)) + 1, unit)
2667
+ assert hash(td) != hash(td3) # doesn't collide
2668
+
2669
+ @pytest.mark.parametrize('unit', ('W', 'D', 'h', 'm', 's', 'ms', 'us'))
2670
+ def test_timedelta_hash_weeks_vs_pydelta(self, unit):
2671
+ td = np.timedelta64(10, 'W')
2672
+ td2 = np.timedelta64(td, unit)
2673
+ pytd = td2.astype(datetime.timedelta)
2674
+ assert isinstance(pytd, datetime.timedelta)
2675
+ _assert_equal_hash(pytd, td2)
2676
+
2677
+ @pytest.mark.parametrize('unit', ('ms', 'us', 'ns', 'ps', 'fs', 'as'))
2678
+ def test_timedelta_hash_ms(self, unit):
2679
+ td = np.timedelta64(3, 'ms')
2680
+ td2 = np.timedelta64(td, unit)
2681
+ _assert_equal_hash(td, td2)
2682
+
2683
+ td3 = np.timedelta64(int(td2.astype(int)) + 1, unit)
2684
+ assert hash(td) != hash(td3) # doesn't collide
2685
+
2686
+ @pytest.mark.parametrize('wk', range(500000, 500010))
2687
+ @pytest.mark.parametrize('unit', ('W', 'D', 'h', 'm', 's', 'ms', 'us'))
2688
+ def test_timedelta_hash_big_positive(self, wk, unit):
2689
+ td = np.timedelta64(wk, 'W')
2690
+ td2 = np.timedelta64(td, unit)
2691
+ _assert_equal_hash(td, td2)
2692
+
2693
+
2694
+ class TestDateTimeData:
2695
+
2696
+ def test_basic(self):
2697
+ a = np.array(['1980-03-23'], dtype=np.datetime64)
2698
+ assert_equal(np.datetime_data(a.dtype), ('D', 1))
2699
+
2700
+ def test_bytes(self):
2701
+ # byte units are converted to unicode
2702
+ dt = np.datetime64('2000', (b'ms', 5))
2703
+ assert np.datetime_data(dt.dtype) == ('ms', 5)
2704
+
2705
+ dt = np.datetime64('2000', b'5ms')
2706
+ assert np.datetime_data(dt.dtype) == ('ms', 5)
2707
+
2708
+ def test_non_ascii(self):
2709
+ # μs is normalized to μ
2710
+ dt = np.datetime64('2000', ('μs', 5))
2711
+ assert np.datetime_data(dt.dtype) == ('us', 5)
2712
+
2713
+ dt = np.datetime64('2000', '5μs')
2714
+ assert np.datetime_data(dt.dtype) == ('us', 5)
2715
+
2716
+
2717
+ def test_comparisons_return_not_implemented():
2718
+ # GH#17017
2719
+
2720
+ class custom:
2721
+ __array_priority__ = 10000
2722
+
2723
+ obj = custom()
2724
+
2725
+ dt = np.datetime64('2000', 'ns')
2726
+ td = dt - dt
2727
+
2728
+ for item in [dt, td]:
2729
+ assert item.__eq__(obj) is NotImplemented
2730
+ assert item.__ne__(obj) is NotImplemented
2731
+ assert item.__le__(obj) is NotImplemented
2732
+ assert item.__lt__(obj) is NotImplemented
2733
+ assert item.__ge__(obj) is NotImplemented
2734
+ assert item.__gt__(obj) is NotImplemented