-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathNumPy_datetime.tests.cs
More file actions
306 lines (274 loc) · 13.1 KB
/
NumPy_datetime.tests.cs
File metadata and controls
306 lines (274 loc) · 13.1 KB
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
// Copyright (c) 2019 by the SciSharp Team
// Code generated by CodeMinion: https://github.com/SciSharp/CodeMinion
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Numpy.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Assert = NUnit.Framework.Assert;
namespace Numpy.UnitTest
{
[TestClass]
public class NumPy_datetimeTest : BaseTestCase
{
[TestMethod]
public void datetime_as_stringTest()
{
// >>> d = np.arange('2002-10-27T04:30', 4*60, 60, dtype='M8[m]')
// >>> d
// array(['2002-10-27T04:30', '2002-10-27T05:30', '2002-10-27T06:30',
// '2002-10-27T07:30'], dtype='datetime64[m]')
//
#if TODO
var given= d = np.arange('2002-10-27T04:30', 4*60, 60, dtype='M8{m}');
given= d;
var expected=
"array(['2002-10-27T04:30', '2002-10-27T05:30', '2002-10-27T06:30',\n" +
" '2002-10-27T07:30'], dtype='datetime64[m]')";
Assert.AreEqual(expected, given.repr);
#endif
// Setting the timezone to UTC shows the same information, but with a Z suffix
// >>> np.datetime_as_string(d, timezone='UTC')
// array(['2002-10-27T04:30Z', '2002-10-27T05:30Z', '2002-10-27T06:30Z',
// '2002-10-27T07:30Z'], dtype='<U35')
//
#if TODO
given= np.datetime_as_string(d, timezone='UTC');
expected=
"array(['2002-10-27T04:30Z', '2002-10-27T05:30Z', '2002-10-27T06:30Z',\n" +
" '2002-10-27T07:30Z'], dtype='<U35')";
Assert.AreEqual(expected, given.repr);
#endif
// Note that we picked datetimes that cross a DST boundary. Passing in a
// pytz timezone object will print the appropriate offset
// >>> np.datetime_as_string(d, timezone=pytz.timezone('US/Eastern'))
// array(['2002-10-27T00:30-0400', '2002-10-27T01:30-0400',
// '2002-10-27T01:30-0500', '2002-10-27T02:30-0500'], dtype='<U39')
//
#if TODO
given= np.datetime_as_string(d, timezone=pytz.timezone('US/Eastern'));
expected=
"array(['2002-10-27T00:30-0400', '2002-10-27T01:30-0400',\n" +
" '2002-10-27T01:30-0500', '2002-10-27T02:30-0500'], dtype='<U39')";
Assert.AreEqual(expected, given.repr);
#endif
// Passing in a unit will change the precision
// >>> np.datetime_as_string(d, unit='h')
// array(['2002-10-27T04', '2002-10-27T05', '2002-10-27T06', '2002-10-27T07'],
// dtype='<U32')
// >>> np.datetime_as_string(d, unit='s')
// array(['2002-10-27T04:30:00', '2002-10-27T05:30:00', '2002-10-27T06:30:00',
// '2002-10-27T07:30:00'], dtype='<U38')
//
#if TODO
given= np.datetime_as_string(d, unit='h');
expected=
"array(['2002-10-27T04', '2002-10-27T05', '2002-10-27T06', '2002-10-27T07'],\n" +
" dtype='<U32')";
Assert.AreEqual(expected, given.repr);
given= np.datetime_as_string(d, unit='s');
expected=
"array(['2002-10-27T04:30:00', '2002-10-27T05:30:00', '2002-10-27T06:30:00',\n" +
" '2002-10-27T07:30:00'], dtype='<U38')";
Assert.AreEqual(expected, given.repr);
#endif
// ‘casting’ can be used to specify whether precision can be changed
// >>> np.datetime_as_string(d, unit='h', casting='safe')
// TypeError: Cannot create a datetime string as units 'h' from a NumPy
// datetime with units 'm' according to the rule 'safe'
//
#if TODO
given= np.datetime_as_string(d, unit='h', casting='safe');
expected=
"TypeError: Cannot create a datetime string as units 'h' from a NumPy\n" +
"datetime with units 'm' according to the rule 'safe'";
Assert.AreEqual(expected, given.repr);
#endif
}
[TestMethod]
public void datetime_dataTest()
{
// >>> dt_25s = np.dtype('timedelta64[25s]')
// >>> np.datetime_data(dt_25s)
// ('s', 25)
// >>> np.array(10, dt_25s).astype('timedelta64[s]')
// array(250, dtype='timedelta64[s]')
//
#if TODO
var given= dt_25s = np.dtype('timedelta64{25s}');
given= np.datetime_data(dt_25s);
var expected=
"('s', 25)";
Assert.AreEqual(expected, given.repr);
given= np.array(10, dt_25s).astype('timedelta64{s}');
expected=
"array(250, dtype='timedelta64[s]')";
Assert.AreEqual(expected, given.repr);
#endif
// The result can be used to construct a datetime that uses the same units
// as a timedelta
// >>> np.datetime64('2010', np.datetime_data(dt_25s))
// numpy.datetime64('2010-01-01T00:00:00', '25s')
//
#if TODO
given= np.datetime64('2010', np.datetime_data(dt_25s));
expected=
"numpy.datetime64('2010-01-01T00:00:00', '25s')";
Assert.AreEqual(expected, given.repr);
#endif
}
[TestMethod]
public void busdaycalendarTest()
{
// >>> # Some important days in July
// ... bdd = np.busdaycalendar(
// ... holidays=['2011-07-01', '2011-07-04', '2011-07-17'])
// >>> # Default is Monday to Friday weekdays
// ... bdd.weekmask
// array([ True, True, True, True, True, False, False], dtype='bool')
// >>> # Any holidays already on the weekend are removed
// ... bdd.holidays
// array(['2011-07-01', '2011-07-04'], dtype='datetime64[D]')
//
#if TODO
var given= # Some important days in July;
var expected=
"... bdd = np.busdaycalendar(\n" +
"... holidays=['2011-07-01', '2011-07-04', '2011-07-17'])";
Assert.AreEqual(expected, given.repr);
given= # Default is Monday to Friday weekdays;
expected=
"... bdd.weekmask\n" +
"array([ True, True, True, True, True, False, False], dtype='bool')";
Assert.AreEqual(expected, given.repr);
given= # Any holidays already on the weekend are removed;
expected=
"... bdd.holidays\n" +
"array(['2011-07-01', '2011-07-04'], dtype='datetime64[D]')";
Assert.AreEqual(expected, given.repr);
#endif
}
[TestMethod]
public void is_busdayTest()
{
// >>> # The weekdays are Friday, Saturday, and Monday
// ... np.is_busday(['2011-07-01', '2011-07-02', '2011-07-18'],
// ... holidays=['2011-07-01', '2011-07-04', '2011-07-17'])
// array([False, False, True], dtype='bool')
//
#if TODO
var given= # The weekdays are Friday, Saturday, and Monday;
var expected=
"... np.is_busday(['2011-07-01', '2011-07-02', '2011-07-18'],\n" +
"... holidays=['2011-07-01', '2011-07-04', '2011-07-17'])\n" +
"array([False, False, True], dtype='bool')";
Assert.AreEqual(expected, given.repr);
#endif
}
[TestMethod]
public void busday_offsetTest()
{
// >>> # First business day in October 2011 (not accounting for holidays)
// ... np.busday_offset('2011-10', 0, roll='forward')
// numpy.datetime64('2011-10-03','D')
// >>> # Last business day in February 2012 (not accounting for holidays)
// ... np.busday_offset('2012-03', -1, roll='forward')
// numpy.datetime64('2012-02-29','D')
// >>> # Third Wednesday in January 2011
// ... np.busday_offset('2011-01', 2, roll='forward', weekmask='Wed')
// numpy.datetime64('2011-01-19','D')
// >>> # 2012 Mother's Day in Canada and the U.S.
// ... np.busday_offset('2012-05', 1, roll='forward', weekmask='Sun')
// numpy.datetime64('2012-05-13','D')
//
#if TODO
var given= # First business day in October 2011 (not accounting for holidays);
var expected=
"... np.busday_offset('2011-10', 0, roll='forward')\n" +
"numpy.datetime64('2011-10-03','D')";
Assert.AreEqual(expected, given.repr);
given= # Last business day in February 2012 (not accounting for holidays);
expected=
"... np.busday_offset('2012-03', -1, roll='forward')\n" +
"numpy.datetime64('2012-02-29','D')";
Assert.AreEqual(expected, given.repr);
given= # Third Wednesday in January 2011;
expected=
"... np.busday_offset('2011-01', 2, roll='forward', weekmask='Wed')\n" +
"numpy.datetime64('2011-01-19','D')";
Assert.AreEqual(expected, given.repr);
given= # 2012 Mother's Day in Canada and the U.S.;
expected=
"... np.busday_offset('2012-05', 1, roll='forward', weekmask='Sun')\n" +
"numpy.datetime64('2012-05-13','D')";
Assert.AreEqual(expected, given.repr);
#endif
// >>> # First business day on or after a date
// ... np.busday_offset('2011-03-20', 0, roll='forward')
// numpy.datetime64('2011-03-21','D')
// >>> np.busday_offset('2011-03-22', 0, roll='forward')
// numpy.datetime64('2011-03-22','D')
// >>> # First business day after a date
// ... np.busday_offset('2011-03-20', 1, roll='backward')
// numpy.datetime64('2011-03-21','D')
// >>> np.busday_offset('2011-03-22', 1, roll='backward')
// numpy.datetime64('2011-03-23','D')
//
#if TODO
given= # First business day on or after a date;
expected=
"... np.busday_offset('2011-03-20', 0, roll='forward')\n" +
"numpy.datetime64('2011-03-21','D')";
Assert.AreEqual(expected, given.repr);
given= np.busday_offset('2011-03-22', 0, roll='forward');
expected=
"numpy.datetime64('2011-03-22','D')";
Assert.AreEqual(expected, given.repr);
given= # First business day after a date;
expected=
"... np.busday_offset('2011-03-20', 1, roll='backward')\n" +
"numpy.datetime64('2011-03-21','D')";
Assert.AreEqual(expected, given.repr);
given= np.busday_offset('2011-03-22', 1, roll='backward');
expected=
"numpy.datetime64('2011-03-23','D')";
Assert.AreEqual(expected, given.repr);
#endif
}
[TestMethod]
public void busday_countTest()
{
// >>> # Number of weekdays in January 2011
// ... np.busday_count('2011-01', '2011-02')
// 21
// >>> # Number of weekdays in 2011
// ... np.busday_count('2011', '2012')
// 260
// >>> # Number of Saturdays in 2011
// ... np.busday_count('2011', '2012', weekmask='Sat')
// 53
//
#if TODO
var given= # Number of weekdays in January 2011;
var expected=
"... np.busday_count('2011-01', '2011-02')\n" +
"21";
Assert.AreEqual(expected, given.repr);
given= # Number of weekdays in 2011;
expected=
"... np.busday_count('2011', '2012')\n" +
"260";
Assert.AreEqual(expected, given.repr);
given= # Number of Saturdays in 2011;
expected=
"... np.busday_count('2011', '2012', weekmask='Sat')\n" +
"53";
Assert.AreEqual(expected, given.repr);
#endif
}
}
}