4
4
import numpy as np
5
5
6
6
from pandas import tslib
7
- from datetime import datetime
7
+ import datetime
8
8
9
9
from pandas .core .api import Timestamp
10
10
15
15
from pandas import _np_version_under1p7
16
16
17
17
18
- class TestDatetimeParsingWrappers (unittest .TestCase ):
19
- def test_verify_datetime_bounds (self ):
20
- for year in (1 , 1000 , 1677 , 2262 , 5000 ):
21
- dt = datetime (year , 1 , 1 )
22
- self .assertRaises (
23
- ValueError ,
24
- tslib .verify_datetime_bounds ,
25
- dt
26
- )
18
+ class TestTimestamp (unittest .TestCase ):
19
+ def test_bounds_with_different_units (self ):
20
+ out_of_bounds_dates = (
21
+ '1677-09-21' ,
22
+ '2262-04-12' ,
23
+ )
24
+
25
+ time_units = ('D' , 'h' , 'm' , 's' , 'ms' , 'us' )
27
26
28
- for year in (1678 , 2000 , 2261 ):
29
- tslib .verify_datetime_bounds (datetime (year , 1 , 1 ))
27
+ for date_string in out_of_bounds_dates :
28
+ for unit in time_units :
29
+ self .assertRaises (
30
+ ValueError ,
31
+ tslib .Timestamp ,
32
+ np .datetime64 (date_string , dtype = 'M8[%s]' % unit )
33
+ )
34
+
35
+ in_bounds_dates = (
36
+ '1677-09-23' ,
37
+ '2262-04-11' ,
38
+ )
30
39
40
+ for date_string in in_bounds_dates :
41
+ for unit in time_units :
42
+ tslib .Timestamp (
43
+ np .datetime64 (date_string , dtype = 'M8[%s]' % unit )
44
+ )
45
+
46
+ def test_barely_oob_dts (self ):
47
+ one_us = np .timedelta64 (1 )
48
+
49
+ # By definition we can't go out of bounds in [ns], so we
50
+ # convert the datetime64s to [us] so we can go out of bounds
51
+ min_ts_us = np .datetime64 (tslib .Timestamp .min ).astype ('M8[us]' )
52
+ max_ts_us = np .datetime64 (tslib .Timestamp .max ).astype ('M8[us]' )
53
+
54
+ # No error for the min/max datetimes
55
+ tslib .Timestamp (min_ts_us )
56
+ tslib .Timestamp (max_ts_us )
57
+
58
+ # One us less than the minimum is an error
59
+ self .assertRaises (ValueError , tslib .Timestamp , min_ts_us - one_us )
60
+
61
+ # One us more than the maximum is an error
62
+ self .assertRaises (ValueError , tslib .Timestamp , max_ts_us + one_us )
63
+
64
+ class TestDatetimeParsingWrappers (unittest .TestCase ):
31
65
def test_does_not_convert_mixed_integer (self ):
32
66
bad_date_strings = (
33
67
'-50000' ,
@@ -97,15 +131,45 @@ def test_number_looking_strings_not_into_datetime(self):
97
131
arr = np .array (['1' , '2' , '3' , '4' , '5' ], dtype = object )
98
132
self .assert_ (np .array_equal (tslib .array_to_datetime (arr ), arr ))
99
133
100
- def test_dates_outside_of_datetime64_ns_bounds (self ):
101
- # These datetimes are outside of the bounds of the
102
- # datetime64[ns] bounds, so they cannot be converted to
103
- # datetimes
104
- arr = np .array (['1/1/1676' , '1/2/1676' ], dtype = object )
105
- self .assert_ (np .array_equal (tslib .array_to_datetime (arr ), arr ))
134
+ def test_coercing_dates_outside_of_datetime64_ns_bounds (self ):
135
+ invalid_dates = [
136
+ datetime .date (1000 , 1 , 1 ),
137
+ datetime .datetime (1000 , 1 , 1 ),
138
+ '1000-01-01' ,
139
+ 'Jan 1, 1000' ,
140
+ np .datetime64 ('1000-01-01' ),
141
+ ]
106
142
107
- arr = np .array (['1/1/2263' , '1/2/2263' ], dtype = object )
108
- self .assert_ (np .array_equal (tslib .array_to_datetime (arr ), arr ))
143
+ for invalid_date in invalid_dates :
144
+ self .assertRaises (
145
+ ValueError ,
146
+ tslib .array_to_datetime ,
147
+ np .array ([invalid_date ], dtype = 'object' ),
148
+ coerce = False ,
149
+ raise_ = True ,
150
+ )
151
+ self .assert_ (
152
+ np .array_equal (
153
+ tslib .array_to_datetime (
154
+ np .array ([invalid_date ], dtype = 'object' ), coerce = True
155
+ ),
156
+ np .array ([tslib .iNaT ], dtype = 'M8[ns]' )
157
+ )
158
+ )
159
+
160
+ arr = np .array (['1/1/1000' , '1/1/2000' ], dtype = object )
161
+ self .assert_ (
162
+ np .array_equal (
163
+ tslib .array_to_datetime (arr , coerce = True ),
164
+ np .array (
165
+ [
166
+ tslib .iNaT ,
167
+ '2000-01-01T00:00:00.000000000-0000'
168
+ ],
169
+ dtype = 'M8[ns]'
170
+ )
171
+ )
172
+ )
109
173
110
174
def test_coerce_of_invalid_datetimes (self ):
111
175
arr = np .array (['01-01-2013' , 'not_a_date' , '1' ], dtype = object )
@@ -130,11 +194,11 @@ def test_coerce_of_invalid_datetimes(self):
130
194
)
131
195
132
196
133
- class TestTimestamp (unittest .TestCase ):
197
+ class TestTimestampNsOperations (unittest .TestCase ):
134
198
def setUp (self ):
135
199
if _np_version_under1p7 :
136
200
raise nose .SkipTest ('numpy >= 1.7 required' )
137
- self .timestamp = Timestamp (datetime .utcnow ())
201
+ self .timestamp = Timestamp (datetime .datetime . utcnow ())
138
202
139
203
def assert_ns_timedelta (self , modified_timestamp , expected_value ):
140
204
value = self .timestamp .value
0 commit comments