5
5
import operator
6
6
import warnings
7
7
import nose
8
-
9
8
import numpy as np
10
- randn = np .random .randn
11
-
9
+ import pandas .tseries .frequencies as frequencies
10
+ import pandas .lib as lib
11
+ import pandas .tslib as tslib
12
+ import pandas .index as _index
13
+ import pandas as pd
12
14
from pandas import (Index , Series , DataFrame ,
13
15
isnull , date_range , Timestamp , Period , DatetimeIndex ,
14
16
Int64Index , to_datetime , bdate_range , Float64Index ,
15
- TimedeltaIndex , NaT , timedelta_range , Timedelta )
17
+ NaT , timedelta_range , Timedelta )
16
18
17
19
import pandas .core .datetools as datetools
18
20
import pandas .tseries .offsets as offsets
19
21
import pandas .tseries .tools as tools
20
- import pandas .tseries .frequencies as frequencies
21
- import pandas as pd
22
22
23
- from pandas .util .testing import assert_series_equal , assert_almost_equal
24
- import pandas .util .testing as tm
25
23
26
- from pandas .tslib import NaT , iNaT
27
- import pandas . lib as lib
28
- import pandas .tslib as tslib
24
+ from pandas .util . testing import assert_series_equal , assert_almost_equal ,\
25
+ _skip_if_has_locale
26
+ import pandas .util . testing as tm
29
27
30
- import pandas .index as _index
28
+ from pandas .tslib import iNaT
31
29
32
30
from pandas .compat import range , long , StringIO , lrange , lmap , zip , product
33
31
from numpy .random import rand
40
38
41
39
from numpy .testing .decorators import slow
42
40
43
-
44
- def _skip_if_has_locale ():
45
- import locale
46
- lang , _ = locale .getlocale ()
47
- if lang is not None :
48
- raise nose .SkipTest ("Specific locale is set {0}" .format (lang ))
41
+ randn = np .random .randn
49
42
50
43
51
44
class TestTimeSeriesDuplicates (tm .TestCase ):
@@ -93,7 +86,8 @@ def test_index_unique(self):
93
86
self .assertEqual (idx .nunique (), 20 )
94
87
self .assertEqual (idx .nunique (dropna = False ), 21 )
95
88
96
- arr = [ Timestamp ('2013-06-09 02:42:28' ) + timedelta (seconds = t ) for t in range (20 ) ] + [NaT ]
89
+ arr = [Timestamp ('2013-06-09 02:42:28' ) + timedelta (seconds = t ) for
90
+ t in range (20 ) ] + [NaT ]
97
91
idx = DatetimeIndex (arr * 3 )
98
92
self .assertTrue (idx .unique ().equals (DatetimeIndex (arr )))
99
93
self .assertEqual (idx .nunique (), 20 )
@@ -258,23 +252,29 @@ def test_indexing(self):
258
252
assert_series_equal (expected , result )
259
253
260
254
# GH3546 (not including times on the last day)
261
- idx = date_range (start = '2013-05-31 00:00' , end = '2013-05-31 23:00' , freq = 'H' )
255
+ idx = date_range (start = '2013-05-31 00:00' , end = '2013-05-31 23:00' ,
256
+ freq = 'H' )
262
257
ts = Series (lrange (len (idx )), index = idx )
263
258
expected = ts ['2013-05' ]
264
259
assert_series_equal (expected , ts )
265
260
266
- idx = date_range (start = '2013-05-31 00:00' , end = '2013-05-31 23:59' , freq = 'S' )
261
+ idx = date_range (start = '2013-05-31 00:00' , end = '2013-05-31 23:59' ,
262
+ freq = 'S' )
267
263
ts = Series (lrange (len (idx )), index = idx )
268
264
expected = ts ['2013-05' ]
269
265
assert_series_equal (expected ,ts )
270
266
271
- idx = [ Timestamp ('2013-05-31 00:00' ), Timestamp (datetime (2013 ,5 ,31 ,23 ,59 ,59 ,999999 ))]
272
- ts = Series (lrange (len (idx )), index = idx )
267
+ idx = [Timestamp ('2013-05-31 00:00' ),
268
+ Timestamp (datetime (2013 ,5 ,31 ,23 ,59 ,59 ,999999 ))]
269
+ ts = Series (lrange (len (idx )), index = idx )
273
270
expected = ts ['2013' ]
274
271
assert_series_equal (expected ,ts )
275
272
276
273
# GH 3925, indexing with a seconds resolution string / datetime object
277
- df = DataFrame (randn (5 ,5 ),columns = ['open' ,'high' ,'low' ,'close' ,'volume' ],index = date_range ('2012-01-02 18:01:00' ,periods = 5 ,tz = 'US/Central' ,freq = 's' ))
274
+ df = DataFrame (randn (5 ,5 ),
275
+ columns = ['open' , 'high' , 'low' , 'close' , 'volume' ],
276
+ index = date_range ('2012-01-02 18:01:00' ,
277
+ periods = 5 , tz = 'US/Central' , freq = 's' ))
278
278
expected = df .loc [[df .index [2 ]]]
279
279
result = df ['2012-01-02 18:01:02' ]
280
280
assert_frame_equal (result ,expected )
@@ -283,14 +283,16 @@ def test_indexing(self):
283
283
self .assertRaises (KeyError , df .__getitem__ , df .index [2 ],)
284
284
285
285
def test_recreate_from_data (self ):
286
- freqs = ['M' , 'Q' , 'A' , 'D' , 'B' , 'BH' , 'T' , 'S' , 'L' , 'U' , 'H' , 'N' , 'C' ]
286
+ freqs = ['M' , 'Q' , 'A' , 'D' , 'B' , 'BH' , 'T' ,
287
+ 'S' , 'L' , 'U' , 'H' , 'N' , 'C' ]
287
288
288
289
for f in freqs :
289
290
org = DatetimeIndex (start = '2001/02/01 09:00' , freq = f , periods = 1 )
290
291
idx = DatetimeIndex (org , freq = f )
291
292
self .assertTrue (idx .equals (org ))
292
293
293
- org = DatetimeIndex (start = '2001/02/01 09:00' , freq = f , tz = 'US/Pacific' , periods = 1 )
294
+ org = DatetimeIndex (start = '2001/02/01 09:00' , freq = f ,
295
+ tz = 'US/Pacific' , periods = 1 )
294
296
idx = DatetimeIndex (org , freq = f , tz = 'US/Pacific' )
295
297
self .assertTrue (idx .equals (org ))
296
298
@@ -459,7 +461,8 @@ def _check_rng(rng):
459
461
self .assertEqual (x .tzinfo , stamp .tzinfo )
460
462
461
463
rng = date_range ('20090415' , '20090519' )
462
- rng_eastern = date_range ('20090415' , '20090519' , tz = pytz .timezone ('US/Eastern' ))
464
+ rng_eastern = date_range ('20090415' , '20090519' ,
465
+ tz = pytz .timezone ('US/Eastern' ))
463
466
rng_utc = date_range ('20090415' , '20090519' , tz = pytz .utc )
464
467
465
468
_check_rng (rng )
@@ -479,7 +482,8 @@ def _check_rng(rng):
479
482
self .assertEqual (x .tzinfo , stamp .tzinfo )
480
483
481
484
rng = date_range ('20090415' , '20090519' )
482
- rng_eastern = date_range ('20090415' , '20090519' , tz = 'dateutil/US/Eastern' )
485
+ rng_eastern = date_range ('20090415' , '20090519' ,
486
+ tz = 'dateutil/US/Eastern' )
483
487
rng_utc = date_range ('20090415' , '20090519' , tz = dateutil .tz .tzutc ())
484
488
485
489
_check_rng (rng )
@@ -1524,6 +1528,38 @@ def test_between_time_frame(self):
1524
1528
else :
1525
1529
self .assertTrue ((t < etime ) or (t >= stime ))
1526
1530
1531
+ def test_between_time_types (self ):
1532
+ # GH11818
1533
+ rng = date_range ('1/1/2000' , '1/5/2000' , freq = '5min' )
1534
+ self .assertRaises (ValueError , rng .indexer_between_time ,
1535
+ datetime (2010 , 1 , 2 , 1 ), datetime (2010 , 1 , 2 , 5 ))
1536
+
1537
+ frame = DataFrame ({'A' : 0 }, index = rng )
1538
+ self .assertRaises (ValueError , frame .between_time ,
1539
+ datetime (2010 , 1 , 2 , 1 ), datetime (2010 , 1 , 2 , 5 ))
1540
+
1541
+ series = Series (0 , index = rng )
1542
+ self .assertRaises (ValueError , series .between_time ,
1543
+ datetime (2010 , 1 , 2 , 1 ), datetime (2010 , 1 , 2 , 5 ))
1544
+
1545
+ def test_between_time_formats (self ):
1546
+ # GH11818
1547
+ _skip_if_has_locale ()
1548
+
1549
+ rng = date_range ('1/1/2000' , '1/5/2000' , freq = '5min' )
1550
+ ts = DataFrame (np .random .randn (len (rng ), 2 ), index = rng )
1551
+
1552
+ strings = [("2:00" , "2:30" ), ("0200" , "0230" ),
1553
+ ("2:00am" , "2:30am" ), ("0200am" , "0230am" ),
1554
+ ("2:00:00" , "2:30:00" ), ("020000" , "023000" ),
1555
+ ("2:00:00am" , "2:30:00am" ), ("020000am" , "023000am" )]
1556
+ expected_length = 28
1557
+
1558
+ for time_string in strings :
1559
+ self .assertEqual (len (ts .between_time (* time_string )),
1560
+ expected_length ,
1561
+ "%s - %s" % time_string )
1562
+
1527
1563
def test_dti_constructor_preserve_dti_freq (self ):
1528
1564
rng = date_range ('1/1/2000' , '1/2/2000' , freq = '5min' )
1529
1565
0 commit comments