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