This repository has been archived by the owner on May 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
arrays.datetime.html
500 lines (489 loc) · 62.8 KB
/
arrays.datetime.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
<span id="arrays-datetime"></span><h1><span class="yiyi-st" id="yiyi-25">Datetimes and Timedeltas</span></h1>
<blockquote>
<p>原文:<a href="https://docs.scipy.org/doc/numpy/reference/arrays.datetime.html">https://docs.scipy.org/doc/numpy/reference/arrays.datetime.html</a></p>
<p>译者:<a href="https://github.com/wizardforcel">飞龙</a> <a href="http://usyiyi.cn/">UsyiyiCN</a></p>
<p>校对:(虚位以待)</p>
</blockquote>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-26"><span class="versionmodified">版本1.7.0中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-27">从NumPy 1.7开始,有核心数组数据类型本身支持日期时间功能。</span><span class="yiyi-st" id="yiyi-28">数据类型称为“datetime64”,因此命名是因为“datetime”已经由Python中包含的datetime库采用。</span></p>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-29">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-30">datetime API在1.7.0中是<em>实验</em>,并且可能在未来版本的NumPy中进行更改。</span></p>
</div>
<div class="section" id="basic-datetimes">
<h2><span class="yiyi-st" id="yiyi-31">Basic Datetimes</span></h2>
<p><span class="yiyi-st" id="yiyi-32">创建数据时间的最基本的方法是使用ISO 8601日期或日期时间格式的字符串。</span><span class="yiyi-st" id="yiyi-33">内部存储单元是从字符串的形式自动选择的,可以是<a class="reference internal" href="#arrays-dtypes-dateunits"><span class="std std-ref">date unit</span></a>或<a class="reference internal" href="#arrays-dtypes-timeunits"><span class="std std-ref">time unit</span></a>。</span><span class="yiyi-st" id="yiyi-34">日期单位是年('Y'),月('M'),星期('W')和日('D'),而时间单位是小时),秒('s'),毫秒('ms')和一些附加的基于秒前缀的单位。</span></p>
<div class="admonition-example admonition">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-35">例</span></p>
<p><span class="yiyi-st" id="yiyi-36">简单的ISO日期:</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'2005-02-25'</span><span class="p">)</span>
<span class="go">numpy.datetime64('2005-02-25')</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-37">使用月份为单位:</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'2005-02'</span><span class="p">)</span>
<span class="go">numpy.datetime64('2005-02')</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-38">仅指定月份,但强制使用“天”单位:</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'2005-02'</span><span class="p">,</span> <span class="s1">'D'</span><span class="p">)</span>
<span class="go">numpy.datetime64('2005-02-01')</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-39">从日期和时间:</span></p>
<div class="last highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'2005-02-25T03:30'</span><span class="p">)</span>
<span class="go">numpy.datetime64('2005-02-25T03:30')</span>
</pre></div>
</div>
</div>
<p><span class="yiyi-st" id="yiyi-40">从字符串创建数据集的数组时,仍然可以使用带有通用单位的日期时间类型从输入中自动选择单位。</span></p>
<div class="admonition-example admonition">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-41">例</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="s1">'2007-07-13'</span><span class="p">,</span> <span class="s1">'2006-01-13'</span><span class="p">,</span> <span class="s1">'2010-08-13'</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'datetime64'</span><span class="p">)</span>
<span class="go">array(['2007-07-13', '2006-01-13', '2010-08-13'], dtype='datetime64[D]')</span>
</pre></div>
</div>
<div class="last highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="s1">'2001-01-01T12:00'</span><span class="p">,</span> <span class="s1">'2002-02-03T13:56:03.172'</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'datetime64'</span><span class="p">)</span>
<span class="go">array(['2001-01-01T12:00:00.000-0600', '2002-02-03T13:56:03.172-0600'], dtype='datetime64[ms]')</span>
</pre></div>
</div>
</div>
<p><span class="yiyi-st" id="yiyi-42">datetime类型适用于许多常见的NumPy函数,例如<a class="reference internal" href="generated/numpy.arange.html#numpy.arange" title="numpy.arange"><code class="xref py py-func docutils literal"><span class="pre">arange</span></code></a>可用于生成日期范围。</span></p>
<div class="admonition-example admonition">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-43">例</span></p>
<p><span class="yiyi-st" id="yiyi-44">所有日期一个月:</span></p>
<div class="last highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="s1">'2005-02'</span><span class="p">,</span> <span class="s1">'2005-03'</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'datetime64[D]'</span><span class="p">)</span>
<span class="go">array(['2005-02-01', '2005-02-02', '2005-02-03', '2005-02-04',</span>
<span class="go"> '2005-02-05', '2005-02-06', '2005-02-07', '2005-02-08',</span>
<span class="go"> '2005-02-09', '2005-02-10', '2005-02-11', '2005-02-12',</span>
<span class="go"> '2005-02-13', '2005-02-14', '2005-02-15', '2005-02-16',</span>
<span class="go"> '2005-02-17', '2005-02-18', '2005-02-19', '2005-02-20',</span>
<span class="go"> '2005-02-21', '2005-02-22', '2005-02-23', '2005-02-24',</span>
<span class="go"> '2005-02-25', '2005-02-26', '2005-02-27', '2005-02-28'],</span>
<span class="go"> dtype='datetime64[D]')</span>
</pre></div>
</div>
</div>
<p><span class="yiyi-st" id="yiyi-45">datetime对象表示一个时间点。</span><span class="yiyi-st" id="yiyi-46">如果两个数据时间具有不同的单位,它们可能仍然表示相同的时刻,并且从诸如月份的较大单位转换为类似天的较小单位被认为是“安全的”投射,因为时间时刻仍然被精确地表示。</span></p>
<div class="admonition-example admonition">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-47">例</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'2005'</span><span class="p">)</span> <span class="o">==</span> <span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'2005-01-01'</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<div class="last highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'2010-03-14T15Z'</span><span class="p">)</span> <span class="o">==</span> <span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'2010-03-14T15:00:00.00Z'</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="datetime-and-timedelta-arithmetic">
<h2><span class="yiyi-st" id="yiyi-48">Datetime and Timedelta Arithmetic</span></h2>
<p><span class="yiyi-st" id="yiyi-49">NumPy允许减去两个日期时间值,一个产生具有时间单位的数字的操作。</span><span class="yiyi-st" id="yiyi-50">由于NumPy在其核心中没有物理量系统,因此创建了timedelta64数据类型以补充datetime64。</span></p>
<p><span class="yiyi-st" id="yiyi-51">Datetimes和Timedeltas一起工作,为简单的日期时间计算提供方法。</span></p>
<div class="admonition-example admonition">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-52">例</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'2009-01-01'</span><span class="p">)</span> <span class="o">-</span> <span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'2008-01-01'</span><span class="p">)</span>
<span class="go">numpy.timedelta64(366,'D')</span>
</pre></div>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'2009'</span><span class="p">)</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">timedelta64</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span> <span class="s1">'D'</span><span class="p">)</span>
<span class="go">numpy.datetime64('2009-01-21')</span>
</pre></div>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'2011-06-15T00:00'</span><span class="p">)</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">timedelta64</span><span class="p">(</span><span class="mi">12</span><span class="p">,</span> <span class="s1">'h'</span><span class="p">)</span>
<span class="go">numpy.datetime64('2011-06-15T12:00-0500')</span>
</pre></div>
</div>
<div class="last highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">timedelta64</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="s1">'W'</span><span class="p">)</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">timedelta64</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="s1">'D'</span><span class="p">)</span>
<span class="go">7.0</span>
</pre></div>
</div>
</div>
<p><span class="yiyi-st" id="yiyi-53">有两个Timedelta单位('Y',年和'M',月),它们被特别处理,因为它们代表的变化取决于它们被使用的时间。</span><span class="yiyi-st" id="yiyi-54">虽然timedelta天单位相当于24小时,但没有办法将月单位转换为天,因为不同的月份有不同的天数。</span></p>
<div class="admonition-example admonition">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-55">例</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">timedelta64</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="s1">'Y'</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">timedelta64</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="s1">'M'</span><span class="p">)</span>
<span class="go">numpy.timedelta64(12,'M')</span>
</pre></div>
</div>
<div class="last highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">timedelta64</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="s1">'D'</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n"><module></span>
<span class="gr">TypeError</span>: <span class="n">Cannot cast NumPy timedelta64 scalar from metadata [Y] to [D] according to the rule 'same_kind'</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="datetime-units">
<h2><span class="yiyi-st" id="yiyi-56">Datetime Units</span></h2>
<p><span class="yiyi-st" id="yiyi-57">Datetime和Timedelta数据类型支持大量的时间单位,以及可以基于输入数据强制转换为任何其他单位的通用单位。</span></p>
<p><span class="yiyi-st" id="yiyi-58">数据时间总是基于POSIX时间存储(虽然具有允许计算闰秒的TAI模式),具有1970-01-01T00:00Z的时期。</span><span class="yiyi-st" id="yiyi-59">这意味着支持的日期总是围绕时期的对称间隔,在下表中称为“时间跨度”。</span></p>
<p><span class="yiyi-st" id="yiyi-60">跨度的长度是64位整数乘以日期或单位长度的范围。</span><span class="yiyi-st" id="yiyi-61">例如,“W”(周)的时间跨度正好是“D”(天)的时间跨度的7倍,“D”(天)的时间跨度恰好是时间跨度的24倍为'h'(小时)。</span></p>
<p><span class="yiyi-st" id="yiyi-62">以下是日期单位:</span></p>
<table border="1" class="docutils" id="arrays-dtypes-dateunits">
<colgroup>
<col width="11%">
<col width="22%">
<col width="32%">
<col width="36%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-63">码</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-64">含义</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-65">时间跨度(相对)</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-66">时间跨度(绝对)</span></th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-67">Y</span></td>
<td><span class="yiyi-st" id="yiyi-68">年</span></td>
<td><span class="yiyi-st" id="yiyi-69">+/- 9.2e18年</span></td>
<td><span class="yiyi-st" id="yiyi-70">[公元前9.2e18,公元9.2e18]</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-71">M</span></td>
<td><span class="yiyi-st" id="yiyi-72">月</span></td>
<td><span class="yiyi-st" id="yiyi-73">+/- 7.6e17年</span></td>
<td><span class="yiyi-st" id="yiyi-74">[7.6e17 BC,7.6e17 AD]</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-75">W</span></td>
<td><span class="yiyi-st" id="yiyi-76">周</span></td>
<td><span class="yiyi-st" id="yiyi-77">+/- 1.7e17年</span></td>
<td><span class="yiyi-st" id="yiyi-78">[1.7e17 BC,1.7e17 AD]</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-79">D</span></td>
<td><span class="yiyi-st" id="yiyi-80">天</span></td>
<td><span class="yiyi-st" id="yiyi-81">+/- 2.5e16年</span></td>
<td><span class="yiyi-st" id="yiyi-82">[2.5e16 BC,2.5e16 AD]</span></td>
</tr>
</tbody>
</table>
<p><span class="yiyi-st" id="yiyi-83">这里是时间单位:</span></p>
<table border="1" class="docutils" id="arrays-dtypes-timeunits">
<colgroup>
<col width="11%">
<col width="22%">
<col width="32%">
<col width="36%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-84">码</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-85">含义</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-86">时间跨度(相对)</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-87">时间跨度(绝对)</span></th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-88">H</span></td>
<td><span class="yiyi-st" id="yiyi-89">小时</span></td>
<td><span class="yiyi-st" id="yiyi-90">+/- 1.0e15年</span></td>
<td><span class="yiyi-st" id="yiyi-91">[1.0e15 BC,1.0e15 AD]</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-92">m</span></td>
<td><span class="yiyi-st" id="yiyi-93">分钟</span></td>
<td><span class="yiyi-st" id="yiyi-94">+/- 1.7e13年</span></td>
<td><span class="yiyi-st" id="yiyi-95">[1.7e13 BC,1.7e13 AD]</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-96">s</span></td>
<td><span class="yiyi-st" id="yiyi-97">第二</span></td>
<td><span class="yiyi-st" id="yiyi-98">+/- 2.9e11年</span></td>
<td><span class="yiyi-st" id="yiyi-99">[公元前2.9e11,公元2.9e11]</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-100">女士</span></td>
<td><span class="yiyi-st" id="yiyi-101">毫秒</span></td>
<td><span class="yiyi-st" id="yiyi-102">+/- 2.9e8年</span></td>
<td><span class="yiyi-st" id="yiyi-103">[公元前2.9e8年,公元2.9e8]</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-104">我们</span></td>
<td><span class="yiyi-st" id="yiyi-105">微秒</span></td>
<td><span class="yiyi-st" id="yiyi-106">+/- 2.9e5年</span></td>
<td><span class="yiyi-st" id="yiyi-107">[290301 BC,294241 AD]</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-108">ns</span></td>
<td><span class="yiyi-st" id="yiyi-109">纳秒</span></td>
<td><span class="yiyi-st" id="yiyi-110">+/- 292年</span></td>
<td><span class="yiyi-st" id="yiyi-111">[1678 AD,2262 AD]</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-112">ps</span></td>
<td><span class="yiyi-st" id="yiyi-113">皮秒</span></td>
<td><span class="yiyi-st" id="yiyi-114">+/- 106天</span></td>
<td><span class="yiyi-st" id="yiyi-115">[1969 AD,1970 AD]</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-116">fs</span></td>
<td><span class="yiyi-st" id="yiyi-117">飞秒</span></td>
<td><span class="yiyi-st" id="yiyi-118">+/- 2.6小时</span></td>
<td><span class="yiyi-st" id="yiyi-119">[1969 AD,1970 AD]</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-120">如</span></td>
<td><span class="yiyi-st" id="yiyi-121">阿特秒</span></td>
<td><span class="yiyi-st" id="yiyi-122">+/- 9.2秒</span></td>
<td><span class="yiyi-st" id="yiyi-123">[1969 AD,1970 AD]</span></td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="business-day-functionality">
<h2><span class="yiyi-st" id="yiyi-124">Business Day Functionality</span></h2>
<p><span class="yiyi-st" id="yiyi-125">为了允许在仅一周中的某些天有效的上下文中使用datetime,NumPy包括一组“busday”(工作日)函数。</span></p>
<p><span class="yiyi-st" id="yiyi-126">busday函数的默认值是,唯一有效的天数是周一到周五(通常的工作日)。</span><span class="yiyi-st" id="yiyi-127">该实现基于包含7个布尔标志以指示有效天的“周掩模”;可以指定其他组的有效天数的自定义周边掩码。</span></p>
<p><span class="yiyi-st" id="yiyi-128">“busday”功能还可以检查“假日”日期的列表,不是有效日期的特定日期。</span></p>
<p><span class="yiyi-st" id="yiyi-129">函数<a class="reference internal" href="generated/numpy.busday_offset.html#numpy.busday_offset" title="numpy.busday_offset"><code class="xref py py-func docutils literal"><span class="pre">busday_offset</span></code></a>允许您将以工作日指定的偏移量应用于以“D”(天)为单位的数据时间。</span></p>
<div class="admonition-example admonition">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-130">例</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">busday_offset</span><span class="p">(</span><span class="s1">'2011-06-23'</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="go">numpy.datetime64('2011-06-24')</span>
</pre></div>
</div>
<div class="last highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">busday_offset</span><span class="p">(</span><span class="s1">'2011-06-23'</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="go">numpy.datetime64('2011-06-27')</span>
</pre></div>
</div>
</div>
<p><span class="yiyi-st" id="yiyi-131">当输入日期落在周末或节假日时,<a class="reference internal" href="generated/numpy.busday_offset.html#numpy.busday_offset" title="numpy.busday_offset"><code class="xref py py-func docutils literal"><span class="pre">busday_offset</span></code></a>首先应用规则将日期滚动到有效工作日,然后应用偏移量。</span><span class="yiyi-st" id="yiyi-132">默认规则是'raise',它只是引发一个异常。</span><span class="yiyi-st" id="yiyi-133">最常用的规则是“向前”和“向后”。</span></p>
<div class="admonition-example admonition">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-134">例</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">busday_offset</span><span class="p">(</span><span class="s1">'2011-06-25'</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n"><module></span>
<span class="gr">ValueError</span>: <span class="n">Non-business day date in busday_offset</span>
</pre></div>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">busday_offset</span><span class="p">(</span><span class="s1">'2011-06-25'</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">roll</span><span class="o">=</span><span class="s1">'forward'</span><span class="p">)</span>
<span class="go">numpy.datetime64('2011-06-27')</span>
</pre></div>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">busday_offset</span><span class="p">(</span><span class="s1">'2011-06-25'</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="n">roll</span><span class="o">=</span><span class="s1">'forward'</span><span class="p">)</span>
<span class="go">numpy.datetime64('2011-06-29')</span>
</pre></div>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">busday_offset</span><span class="p">(</span><span class="s1">'2011-06-25'</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">roll</span><span class="o">=</span><span class="s1">'backward'</span><span class="p">)</span>
<span class="go">numpy.datetime64('2011-06-24')</span>
</pre></div>
</div>
<div class="last highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">busday_offset</span><span class="p">(</span><span class="s1">'2011-06-25'</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="n">roll</span><span class="o">=</span><span class="s1">'backward'</span><span class="p">)</span>
<span class="go">numpy.datetime64('2011-06-28')</span>
</pre></div>
</div>
</div>
<p><span class="yiyi-st" id="yiyi-135">在一些情况下,合适地使用卷和偏移量是获得期望的答案所必需的。</span></p>
<div class="admonition-example admonition">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-136">例</span></p>
<p><span class="yiyi-st" id="yiyi-137">日期当天或之后的第一个工作日:</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">busday_offset</span><span class="p">(</span><span class="s1">'2011-03-20'</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">roll</span><span class="o">=</span><span class="s1">'forward'</span><span class="p">)</span>
<span class="go">numpy.datetime64('2011-03-21','D')</span>
<span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">busday_offset</span><span class="p">(</span><span class="s1">'2011-03-22'</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">roll</span><span class="o">=</span><span class="s1">'forward'</span><span class="p">)</span>
<span class="go">numpy.datetime64('2011-03-22','D')</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-138">第一个工作日严格日期后:</span></p>
<div class="last highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">busday_offset</span><span class="p">(</span><span class="s1">'2011-03-20'</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">roll</span><span class="o">=</span><span class="s1">'backward'</span><span class="p">)</span>
<span class="go">numpy.datetime64('2011-03-21','D')</span>
<span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">busday_offset</span><span class="p">(</span><span class="s1">'2011-03-22'</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">roll</span><span class="o">=</span><span class="s1">'backward'</span><span class="p">)</span>
<span class="go">numpy.datetime64('2011-03-23','D')</span>
</pre></div>
</div>
</div>
<p><span class="yiyi-st" id="yiyi-139">该函数还可用于计算某些类型的假日。</span><span class="yiyi-st" id="yiyi-140">在加拿大和美国,母亲节在5月的第二个星期日,可以用定制的周密计算。</span></p>
<div class="admonition-example admonition">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-141">例</span></p>
<div class="last highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">busday_offset</span><span class="p">(</span><span class="s1">'2012-05'</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">roll</span><span class="o">=</span><span class="s1">'forward'</span><span class="p">,</span> <span class="n">weekmask</span><span class="o">=</span><span class="s1">'Sun'</span><span class="p">)</span>
<span class="go">numpy.datetime64('2012-05-13','D')</span>
</pre></div>
</div>
</div>
<p><span class="yiyi-st" id="yiyi-142">当性能对于操作许多商业日期很重要,通过一个特定的周末掩码和节假日选择,有一个对象<a class="reference internal" href="generated/numpy.busdaycalendar.html#numpy.busdaycalendar" title="numpy.busdaycalendar"><code class="xref py py-class docutils literal"><span class="pre">busdaycalendar</span></code></a>以优化的形式存储必要的数据。</span></p>
<div class="section" id="np-is-busday">
<h3><span class="yiyi-st" id="yiyi-143">np.is_busday():</span></h3>
<p><span class="yiyi-st" id="yiyi-144">要测试datetime64值以查看它是否是有效的一天,请使用<a class="reference internal" href="generated/numpy.is_busday.html#numpy.is_busday" title="numpy.is_busday"><code class="xref py py-func docutils literal"><span class="pre">is_busday</span></code></a>。</span></p>
<div class="admonition-example admonition">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-145">例</span></p>
<div class="last highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">is_busday</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'2011-07-15'</span><span class="p">))</span> <span class="c1"># a Friday</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">is_busday</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'2011-07-16'</span><span class="p">))</span> <span class="c1"># a Saturday</span>
<span class="go">False</span>
<span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">is_busday</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'2011-07-16'</span><span class="p">),</span> <span class="n">weekmask</span><span class="o">=</span><span class="s2">"Sat Sun"</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'2011-07-11'</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'2011-07-18'</span><span class="p">))</span>
<span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">is_busday</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="go">array([ True, True, True, True, True, False, False], dtype='bool')</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="np-busday-count">
<h3><span class="yiyi-st" id="yiyi-146">np.busday_count():</span></h3>
<p><span class="yiyi-st" id="yiyi-147">要查找datetime64日期的指定范围内有多少有效天数,请使用<a class="reference internal" href="generated/numpy.busday_count.html#numpy.busday_count" title="numpy.busday_count"><code class="xref py py-func docutils literal"><span class="pre">busday_count</span></code></a>:</span></p>
<div class="admonition-example admonition">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-148">例</span></p>
<div class="last highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">busday_count</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'2011-07-11'</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'2011-07-18'</span><span class="p">))</span>
<span class="go">5</span>
<span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">busday_count</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'2011-07-18'</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'2011-07-11'</span><span class="p">))</span>
<span class="go">-5</span>
</pre></div>
</div>
</div>
<p><span class="yiyi-st" id="yiyi-149">如果你有一个datetime64 day值的数组,并且你想要一个有多少是有效日期的计数,你可以这样做:</span></p>
<div class="admonition-example admonition">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-150">例</span></p>
<div class="last highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'2011-07-11'</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'2011-07-18'</span><span class="p">))</span>
<span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">count_nonzero</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">is_busday</span><span class="p">(</span><span class="n">a</span><span class="p">))</span>
<span class="go">5</span>
</pre></div>
</div>
</div>
<div class="section" id="custom-weekmasks">
<h4><span class="yiyi-st" id="yiyi-151">Custom Weekmasks</span></h4>
<p><span class="yiyi-st" id="yiyi-152">以下是自定义周掩码值的几个示例。</span><span class="yiyi-st" id="yiyi-153">这些示例指定“busday”默认值为星期一到星期五是有效日期。</span></p>
<p><span class="yiyi-st" id="yiyi-154">一些例子:</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="c1"># Positional sequences; positions are Monday through Sunday.</span>
<span class="c1"># Length of the sequence must be exactly 7.</span>
<span class="n">weekmask</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span>
<span class="c1"># list or other sequence; 0 == invalid day, 1 == valid day</span>
<span class="n">weekmask</span> <span class="o">=</span> <span class="s2">"1111100"</span>
<span class="c1"># string '0' == invalid day, '1' == valid day</span>
<span class="c1"># string abbreviations from this list: Mon Tue Wed Thu Fri Sat Sun</span>
<span class="n">weekmask</span> <span class="o">=</span> <span class="s2">"Mon Tue Wed Thu Fri"</span>
<span class="c1"># any amount of whitespace is allowed; abbreviations are case-sensitive.</span>
<span class="n">weekmask</span> <span class="o">=</span> <span class="s2">"MonTue Wed Thu</span><span class="se">\t</span><span class="s2">Fri"</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="section" id="changes-with-numpy-1-11">
<h2><span class="yiyi-st" id="yiyi-155">Changes with NumPy 1.11</span></h2>
<p><span class="yiyi-st" id="yiyi-156">在以前的NumPy版本中,datetime64类型总是存储UTC时间。</span><span class="yiyi-st" id="yiyi-157">默认情况下,从字符串创建datetime64对象或打印datetime64对象将从或转换为本地时间:</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="c1"># old behavior</span>
<span class="o">>>>></span> <span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'2000-01-01T00:00:00'</span><span class="p">)</span>
<span class="n">numpy</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'2000-01-01T00:00:00-0800'</span><span class="p">)</span> <span class="c1"># note the timezone offset -08:00</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-158">datetime64用户的一致性同意这种行为是不可取的,并且与datetime64通常使用(例如,通过<a href="#id1"><span class="problematic" id="id2">pandas_</span></a>)不一致。</span><span class="yiyi-st" id="yiyi-159">对于大多数使用情况,首选的时区naive datetime类型,类似于Python标准库中的<code class="docutils literal"><span class="pre">datetime.datetime</span></code>类型。</span><span class="yiyi-st" id="yiyi-160">因此,datetime64不再假设输入在本地时间,也不打印本地时间:</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="go">>>>> np.datetime64('2000-01-01T00:00:00')</span>
<span class="go">numpy.datetime64('2000-01-01T00:00:00')</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-161">为了向后兼容,datetime64仍然解析时区偏移,它通过转换为UTC来处理。</span><span class="yiyi-st" id="yiyi-162">但是,生成的datetime是时区naive:</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'2000-01-01T00:00:00-08'</span><span class="p">)</span>
<span class="go">DeprecationWarning: parsing timezone aware datetimes is deprecated; this will raise an error in the future</span>
<span class="go">numpy.datetime64('2000-01-01T08:00:00')</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-163">作为此更改的推论,我们不再禁止在数据时间之间使用日期单位和数据时间与时间单位进行转换。</span><span class="yiyi-st" id="yiyi-164">使用时区天真数据时,从日期到时间的转换规则不再含糊不清。</span></p>
<p><span class="yiyi-st" id="yiyi-165"><a href="#id3"><span class="problematic" id="id4">pandas_</span></a>:<a class="reference external" href="http://pandas.pydata.org">http://pandas.pydata.org</a></span></p>
</div>
<div class="section" id="differences-between-1-6-and-1-7-datetimes">
<h2><span class="yiyi-st" id="yiyi-166">Differences Between 1.6 and 1.7 Datetimes</span></h2>
<p><span class="yiyi-st" id="yiyi-167">NumPy 1.6版本包括比1.7更原始的datetime数据类型。</span><span class="yiyi-st" id="yiyi-168">本节记录了已发生的许多变化。</span></p>
<div class="section" id="string-parsing">
<h3><span class="yiyi-st" id="yiyi-169">String Parsing</span></h3>
<p><span class="yiyi-st" id="yiyi-170">NumPy 1.6中的datetime字符串解析器在它接受的内容非常自由,并且默默地允许无效输入,而不会引起错误。</span><span class="yiyi-st" id="yiyi-171">在NumPy 1.7中的解析器是非常严格的只接受ISO 8601日期,有一些方便的扩展。</span><span class="yiyi-st" id="yiyi-172">1.6总是默认创建微秒(us)单位,而1.7检测基于字符串格式的单位。</span><span class="yiyi-st" id="yiyi-173">这里是一个比较。</span><span class="yiyi-st" id="yiyi-174">:</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="c1"># NumPy 1.6.1</span>
<span class="o">>>></span> <span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'1979-03-22'</span><span class="p">)</span>
<span class="mi">1979</span><span class="o">-</span><span class="mi">03</span><span class="o">-</span><span class="mi">22</span> <span class="mi">00</span><span class="p">:</span><span class="mi">00</span><span class="p">:</span><span class="mi">00</span>
<span class="c1"># NumPy 1.7.0</span>
<span class="o">>>></span> <span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'1979-03-22'</span><span class="p">)</span>
<span class="n">numpy</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'1979-03-22'</span><span class="p">)</span>
<span class="c1"># NumPy 1.6.1, unit default microseconds</span>
<span class="o">>>></span> <span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'1979-03-22'</span><span class="p">)</span><span class="o">.</span><span class="n">dtype</span>
<span class="n">dtype</span><span class="p">(</span><span class="s1">'datetime64[us]'</span><span class="p">)</span>
<span class="c1"># NumPy 1.7.0, unit of days detected from string</span>
<span class="o">>>></span> <span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'1979-03-22'</span><span class="p">)</span><span class="o">.</span><span class="n">dtype</span>
<span class="n">dtype</span><span class="p">(</span><span class="s1">'<M8[D]'</span><span class="p">)</span>
<span class="c1"># NumPy 1.6.1, ignores invalid part of string</span>
<span class="o">>>></span> <span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'1979-03-2corruptedstring'</span><span class="p">)</span>
<span class="mi">1979</span><span class="o">-</span><span class="mi">03</span><span class="o">-</span><span class="mi">02</span> <span class="mi">00</span><span class="p">:</span><span class="mi">00</span><span class="p">:</span><span class="mi">00</span>
<span class="c1"># NumPy 1.7.0, raises error for invalid input</span>
<span class="o">>>></span> <span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'1979-03-2corruptedstring'</span><span class="p">)</span>
<span class="n">Traceback</span> <span class="p">(</span><span class="n">most</span> <span class="n">recent</span> <span class="n">call</span> <span class="n">last</span><span class="p">):</span>
<span class="n">File</span> <span class="s2">"<stdin>"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1</span><span class="p">,</span> <span class="ow">in</span> <span class="o"><</span><span class="n">module</span><span class="o">></span>
<span class="ne">ValueError</span><span class="p">:</span> <span class="n">Error</span> <span class="n">parsing</span> <span class="n">datetime</span> <span class="n">string</span> <span class="s2">"1979-03-2corruptedstring"</span> <span class="n">at</span> <span class="n">position</span> <span class="mi">8</span>
<span class="c1"># NumPy 1.6.1, 'nat' produces today's date</span>
<span class="o">>>></span> <span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'nat'</span><span class="p">)</span>
<span class="mi">2012</span><span class="o">-</span><span class="mi">04</span><span class="o">-</span><span class="mi">30</span> <span class="mi">00</span><span class="p">:</span><span class="mi">00</span><span class="p">:</span><span class="mi">00</span>
<span class="c1"># NumPy 1.7.0, 'nat' produces not-a-time</span>
<span class="o">>>></span> <span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'nat'</span><span class="p">)</span>
<span class="n">numpy</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'NaT'</span><span class="p">)</span>
<span class="c1"># NumPy 1.6.1, 'garbage' produces today's date</span>
<span class="o">>>></span> <span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'garbage'</span><span class="p">)</span>
<span class="mi">2012</span><span class="o">-</span><span class="mi">04</span><span class="o">-</span><span class="mi">30</span> <span class="mi">00</span><span class="p">:</span><span class="mi">00</span><span class="p">:</span><span class="mi">00</span>
<span class="c1"># NumPy 1.7.0, 'garbage' raises an exception</span>
<span class="o">>>></span> <span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'garbage'</span><span class="p">)</span>
<span class="n">Traceback</span> <span class="p">(</span><span class="n">most</span> <span class="n">recent</span> <span class="n">call</span> <span class="n">last</span><span class="p">):</span>
<span class="n">File</span> <span class="s2">"<stdin>"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1</span><span class="p">,</span> <span class="ow">in</span> <span class="o"><</span><span class="n">module</span><span class="o">></span>
<span class="ne">ValueError</span><span class="p">:</span> <span class="n">Error</span> <span class="n">parsing</span> <span class="n">datetime</span> <span class="n">string</span> <span class="s2">"garbage"</span> <span class="n">at</span> <span class="n">position</span> <span class="mi">0</span>
<span class="c1"># NumPy 1.6.1, can't specify unit in scalar constructor</span>
<span class="o">>>></span> <span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'1979-03-22T19:00'</span><span class="p">,</span> <span class="s1">'h'</span><span class="p">)</span>
<span class="n">Traceback</span> <span class="p">(</span><span class="n">most</span> <span class="n">recent</span> <span class="n">call</span> <span class="n">last</span><span class="p">):</span>
<span class="n">File</span> <span class="s2">"<stdin>"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1</span><span class="p">,</span> <span class="ow">in</span> <span class="o"><</span><span class="n">module</span><span class="o">></span>
<span class="ne">TypeError</span><span class="p">:</span> <span class="n">function</span> <span class="n">takes</span> <span class="n">at</span> <span class="n">most</span> <span class="mi">1</span> <span class="n">argument</span> <span class="p">(</span><span class="mi">2</span> <span class="n">given</span><span class="p">)</span>
<span class="c1"># NumPy 1.7.0, unit in scalar constructor</span>
<span class="o">>>></span> <span class="n">np</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'1979-03-22T19:00'</span><span class="p">,</span> <span class="s1">'h'</span><span class="p">)</span>
<span class="n">numpy</span><span class="o">.</span><span class="n">datetime64</span><span class="p">(</span><span class="s1">'1979-03-22T19:00-0500'</span><span class="p">,</span><span class="s1">'h'</span><span class="p">)</span>
<span class="c1"># NumPy 1.6.1, reads ISO 8601 strings w/o TZ as UTC</span>
<span class="o">>>></span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="s1">'1979-03-22T19:00'</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'M8[h]'</span><span class="p">)</span>
<span class="n">array</span><span class="p">([</span><span class="mi">1979</span><span class="o">-</span><span class="mi">03</span><span class="o">-</span><span class="mi">22</span> <span class="mi">19</span><span class="p">:</span><span class="mi">00</span><span class="p">:</span><span class="mi">00</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">datetime64</span><span class="p">[</span><span class="n">h</span><span class="p">])</span>
<span class="c1"># NumPy 1.7.0, reads ISO 8601 strings w/o TZ as local (ISO specifies this)</span>
<span class="o">>>></span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="s1">'1979-03-22T19:00'</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'M8[h]'</span><span class="p">)</span>
<span class="n">array</span><span class="p">([</span><span class="s1">'1979-03-22T19-0500'</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'datetime64[h]'</span><span class="p">)</span>
<span class="c1"># NumPy 1.6.1, doesn't parse all ISO 8601 strings correctly</span>
<span class="o">>>></span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="s1">'1979-03-22T12'</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'M8[h]'</span><span class="p">)</span>
<span class="n">array</span><span class="p">([</span><span class="mi">1979</span><span class="o">-</span><span class="mi">03</span><span class="o">-</span><span class="mi">22</span> <span class="mi">00</span><span class="p">:</span><span class="mi">00</span><span class="p">:</span><span class="mi">00</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">datetime64</span><span class="p">[</span><span class="n">h</span><span class="p">])</span>
<span class="o">>>></span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="s1">'1979-03-22T12:00'</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'M8[h]'</span><span class="p">)</span>
<span class="n">array</span><span class="p">([</span><span class="mi">1979</span><span class="o">-</span><span class="mi">03</span><span class="o">-</span><span class="mi">22</span> <span class="mi">12</span><span class="p">:</span><span class="mi">00</span><span class="p">:</span><span class="mi">00</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">datetime64</span><span class="p">[</span><span class="n">h</span><span class="p">])</span>
<span class="c1"># NumPy 1.7.0, handles this case correctly</span>
<span class="o">>>></span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="s1">'1979-03-22T12'</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'M8[h]'</span><span class="p">)</span>
<span class="n">array</span><span class="p">([</span><span class="s1">'1979-03-22T12-0500'</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'datetime64[h]'</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="s1">'1979-03-22T12:00'</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'M8[h]'</span><span class="p">)</span>
<span class="n">array</span><span class="p">([</span><span class="s1">'1979-03-22T12-0500'</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'datetime64[h]'</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="unit-conversion">
<h3><span class="yiyi-st" id="yiyi-175">Unit Conversion</span></h3>
<p><span class="yiyi-st" id="yiyi-176">日期时间的1.6实现不能正确地在单位之间转换。</span><span class="yiyi-st" id="yiyi-177">:</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="c1"># NumPy 1.6.1, the representation value is untouched</span>
<span class="o">>>></span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="s1">'1979-03-22'</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'M8[D]'</span><span class="p">)</span>
<span class="n">array</span><span class="p">([</span><span class="mi">1979</span><span class="o">-</span><span class="mi">03</span><span class="o">-</span><span class="mi">22</span> <span class="mi">00</span><span class="p">:</span><span class="mi">00</span><span class="p">:</span><span class="mi">00</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">datetime64</span><span class="p">[</span><span class="n">D</span><span class="p">])</span>
<span class="o">>>></span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="s1">'1979-03-22'</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'M8[D]'</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s1">'M8[M]'</span><span class="p">)</span>
<span class="n">array</span><span class="p">([</span><span class="mi">2250</span><span class="o">-</span><span class="mi">08</span><span class="o">-</span><span class="mi">01</span> <span class="mi">00</span><span class="p">:</span><span class="mi">00</span><span class="p">:</span><span class="mi">00</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">datetime64</span><span class="p">[</span><span class="n">M</span><span class="p">])</span>
<span class="c1"># NumPy 1.7.0, the representation is scaled accordingly</span>
<span class="o">>>></span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="s1">'1979-03-22'</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'M8[D]'</span><span class="p">)</span>
<span class="n">array</span><span class="p">([</span><span class="s1">'1979-03-22'</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'datetime64[D]'</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="s1">'1979-03-22'</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'M8[D]'</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s1">'M8[M]'</span><span class="p">)</span>
<span class="n">array</span><span class="p">([</span><span class="s1">'1979-03'</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'datetime64[M]'</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="datetime-arithmetic">
<h3><span class="yiyi-st" id="yiyi-178">Datetime Arithmetic</span></h3>
<p><span class="yiyi-st" id="yiyi-179">日期时间的1.6实现仅对算术运算的小子集正确工作。</span><span class="yiyi-st" id="yiyi-180">这里我们展示一些简单的案例。</span><span class="yiyi-st" id="yiyi-181">:</span></p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="c1"># NumPy 1.6.1, produces invalid results if units are incompatible</span>
<span class="o">>>></span> <span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="s1">'1979-03-22T12'</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'M8[h]'</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">3</span><span class="o">*</span><span class="mi">60</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'m8[m]'</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span>
<span class="n">array</span><span class="p">([</span><span class="mi">1970</span><span class="o">-</span><span class="mi">01</span><span class="o">-</span><span class="mi">01</span> <span class="mi">00</span><span class="p">:</span><span class="mi">00</span><span class="p">:</span><span class="mf">00.080988</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">datetime64</span><span class="p">[</span><span class="n">us</span><span class="p">])</span>
<span class="c1"># NumPy 1.7.0, promotes to higher-resolution unit</span>
<span class="o">>>></span> <span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="s1">'1979-03-22T12'</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'M8[h]'</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">3</span><span class="o">*</span><span class="mi">60</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'m8[m]'</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span>
<span class="n">array</span><span class="p">([</span><span class="s1">'1979-03-22T15:00-0500'</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'datetime64[m]'</span><span class="p">)</span>
<span class="c1"># NumPy 1.6.1, arithmetic works if everything is microseconds</span>
<span class="o">>>></span> <span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="s1">'1979-03-22T12:00'</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'M8[us]'</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">3</span><span class="o">*</span><span class="mi">60</span><span class="o">*</span><span class="mi">60</span><span class="o">*</span><span class="mi">1000000</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'m8[us]'</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span>
<span class="n">array</span><span class="p">([</span><span class="mi">1979</span><span class="o">-</span><span class="mi">03</span><span class="o">-</span><span class="mi">22</span> <span class="mi">15</span><span class="p">:</span><span class="mi">00</span><span class="p">:</span><span class="mi">00</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="n">datetime64</span><span class="p">[</span><span class="n">us</span><span class="p">])</span>
<span class="c1"># NumPy 1.7.0</span>
<span class="o">>>></span> <span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="s1">'1979-03-22T12:00'</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'M8[us]'</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">3</span><span class="o">*</span><span class="mi">60</span><span class="o">*</span><span class="mi">60</span><span class="o">*</span><span class="mi">1000000</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'m8[us]'</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span>
<span class="n">array</span><span class="p">([</span><span class="s1">'1979-03-22T15:00:00.000000-0500'</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'datetime64[us]'</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>