@@ -357,9 +357,13 @@ def test_drange():
357
357
# dates from an half open interval [start, end)
358
358
assert len (mdates .drange (start , end , delta )) == 24
359
359
360
+ # Same if interval ends slightly earlier
361
+ end = end - datetime .timedelta (microseconds = 1 )
362
+ assert len (mdates .drange (start , end , delta )) == 24
363
+
360
364
# if end is a little bit later, we expect the range to contain one element
361
365
# more
362
- end = end + datetime .timedelta (microseconds = 1 )
366
+ end = end + datetime .timedelta (microseconds = 2 )
363
367
assert len (mdates .drange (start , end , delta )) == 25
364
368
365
369
# reset end
@@ -998,7 +1002,6 @@ def _test_rrulewrapper(attach_tz, get_tz):
998
1002
dtend = attach_tz (datetime .datetime (2017 , 4 , 4 , 0 ), SYD )
999
1003
1000
1004
rule = mdates .rrulewrapper (freq = dateutil .rrule .DAILY , dtstart = dtstart )
1001
-
1002
1005
act = rule .between (dtstart , dtend )
1003
1006
exp = [datetime .datetime (2017 , 4 , 1 , 13 , tzinfo = dateutil .tz .tzutc ()),
1004
1007
datetime .datetime (2017 , 4 , 2 , 14 , tzinfo = dateutil .tz .tzutc ())]
@@ -1012,6 +1015,20 @@ def attach_tz(dt, zi):
1012
1015
1013
1016
_test_rrulewrapper (attach_tz , dateutil .tz .gettz )
1014
1017
1018
+ SYD = dateutil .tz .gettz ('Australia/Sydney' )
1019
+ dtstart = datetime .datetime (2017 , 4 , 1 , 0 )
1020
+ dtend = datetime .datetime (2017 , 4 , 4 , 0 )
1021
+ rule = mdates .rrulewrapper (freq = dateutil .rrule .DAILY , dtstart = dtstart ,
1022
+ tzinfo = SYD , until = dtend )
1023
+ assert rule .after (dtstart ) == datetime .datetime (2017 , 4 , 2 , 0 , 0 ,
1024
+ tzinfo = SYD )
1025
+ assert rule .before (dtend ) == datetime .datetime (2017 , 4 , 3 , 0 , 0 ,
1026
+ tzinfo = SYD )
1027
+
1028
+ # Test parts of __getattr__
1029
+ assert rule ._base_tzinfo == SYD
1030
+ assert rule ._interval == 1
1031
+
1015
1032
1016
1033
@pytest .mark .pytz
1017
1034
def test_rrulewrapper_pytz ():
@@ -1046,6 +1063,15 @@ def test_yearlocator_pytz():
1046
1063
'2014-01-01 00:00:00-05:00' , '2015-01-01 00:00:00-05:00' ]
1047
1064
st = list (map (str , mdates .num2date (locator (), tz = tz )))
1048
1065
assert st == expected
1066
+ assert np .allclose (locator .tick_values (x [0 ], x [1 ]), np .array (
1067
+ [14610.20833333 , 14610.33333333 , 14610.45833333 , 14610.58333333 ,
1068
+ 14610.70833333 , 14610.83333333 , 14610.95833333 , 14611.08333333 ,
1069
+ 14611.20833333 ]))
1070
+ assert np .allclose (locator .get_locator (x [1 ], x [0 ]).tick_values (x [0 ], x [1 ]),
1071
+ np .array (
1072
+ [14610.20833333 , 14610.33333333 , 14610.45833333 , 14610.58333333 ,
1073
+ 14610.70833333 , 14610.83333333 , 14610.95833333 , 14611.08333333 ,
1074
+ 14611.20833333 ]))
1049
1075
1050
1076
1051
1077
def test_YearLocator ():
@@ -1290,18 +1316,14 @@ def test_datestr2num():
1290
1316
month = 1 , day = 10 )).size == 0
1291
1317
1292
1318
1293
- def test_concise_formatter_exceptions ():
1319
+ @pytest .mark .parametrize ('kwarg' ,
1320
+ ('formats' , 'zero_formats' , 'offset_formats' ))
1321
+ def test_concise_formatter_exceptions (kwarg ):
1294
1322
locator = mdates .AutoDateLocator ()
1295
- with pytest .raises (ValueError , match = "formats argument must be a list" ):
1296
- mdates .ConciseDateFormatter (locator , formats = ['' , '%Y' ])
1297
-
1298
- with pytest .raises (ValueError ,
1299
- match = "zero_formats argument must be a list" ):
1300
- mdates .ConciseDateFormatter (locator , zero_formats = ['' , '%Y' ])
1301
-
1302
- with pytest .raises (ValueError ,
1303
- match = "offset_formats argument must be a list" ):
1304
- mdates .ConciseDateFormatter (locator , offset_formats = ['' , '%Y' ])
1323
+ kwargs = {kwarg : ['' , '%Y' ]}
1324
+ match = f"{ kwarg } argument must be a list"
1325
+ with pytest .raises (ValueError , match = match ):
1326
+ mdates .ConciseDateFormatter (locator , ** kwargs )
1305
1327
1306
1328
1307
1329
def test_concise_formatter_call ():
@@ -1312,7 +1334,10 @@ def test_concise_formatter_call():
1312
1334
1313
1335
1314
1336
@pytest .mark .parametrize ('span, expected_locator' ,
1315
- ((0.02 , mdates .MinuteLocator ),
1337
+ ((0 , mdates .MinuteLocator ),
1338
+ (0.00001 , mdates .MicrosecondLocator ),
1339
+ (0.001 , mdates .SecondLocator ),
1340
+ (0.02 , mdates .MinuteLocator ),
1316
1341
(1 , mdates .HourLocator ),
1317
1342
(19 , mdates .DayLocator ),
1318
1343
(40 , mdates .WeekdayLocator ),
@@ -1327,3 +1352,29 @@ def test_usetex_newline():
1327
1352
fig , ax = plt .subplots ()
1328
1353
ax .xaxis .set_major_formatter (mdates .DateFormatter ('%d/%m\n %Y' ))
1329
1354
fig .canvas .draw ()
1355
+
1356
+
1357
+ @pytest .mark .parametrize ('val' , (- 1000000 , 10000000 ))
1358
+ def test_num2date_error (val ):
1359
+ with pytest .raises (ValueError , match = f"Date ordinal { val } converts" ):
1360
+ mdates .num2date (val )
1361
+
1362
+
1363
+ def test_num2date_roundoff ():
1364
+ assert mdates .num2date (100000.0000578702 ) == datetime .datetime (
1365
+ 2243 , 10 , 17 , 0 , 0 , 4 , 999980 , tzinfo = datetime .timezone .utc )
1366
+ # Slightly larger, steps of 20 microseconds
1367
+ assert mdates .num2date (100000.0000578703 ) == datetime .datetime (
1368
+ 2243 , 10 , 17 , 0 , 0 , 5 , tzinfo = datetime .timezone .utc )
1369
+
1370
+
1371
+ def test_DateFormatter_settz ():
1372
+ time = mdates .date2num (datetime .datetime (2011 , 1 , 1 , 0 , 0 ,
1373
+ tzinfo = mdates .UTC ))
1374
+ formatter = mdates .DateFormatter ('%Y-%b-%d %H:%M' )
1375
+ # Default UTC
1376
+ assert formatter (time ) == '2011-Jan-01 00:00'
1377
+
1378
+ # Set tzinfo
1379
+ formatter .set_tzinfo ('Pacific/Kiritimati' )
1380
+ assert formatter (time ) == '2011-Jan-01 14:00'
0 commit comments