6
6
import numpy as np
7
7
import pytest
8
8
9
- import pandas as pd
10
9
from pandas import (
11
10
DataFrame ,
12
11
DatetimeIndex ,
21
20
22
21
23
22
class TestSlicing :
24
- def test_slice_with_negative_step (self ):
25
- ts = Series (np .arange (20 ), date_range ("2014-01-01" , periods = 20 , freq = "MS" ))
26
- SLC = pd .IndexSlice
27
-
28
- def assert_slices_equivalent (l_slc , i_slc ):
29
- expected = ts .iloc [i_slc ]
30
-
31
- tm .assert_series_equal (ts [l_slc ], expected )
32
- tm .assert_series_equal (ts .loc [l_slc ], expected )
33
- tm .assert_series_equal (ts .loc [l_slc ], expected )
34
-
35
- assert_slices_equivalent (SLC [Timestamp ("2014-10-01" ) :: - 1 ], SLC [9 ::- 1 ])
36
- assert_slices_equivalent (SLC ["2014-10-01" ::- 1 ], SLC [9 ::- 1 ])
37
-
38
- assert_slices_equivalent (SLC [: Timestamp ("2014-10-01" ) : - 1 ], SLC [:8 :- 1 ])
39
- assert_slices_equivalent (SLC [:"2014-10-01" :- 1 ], SLC [:8 :- 1 ])
40
-
41
- assert_slices_equivalent (SLC ["2015-02-01" :"2014-10-01" :- 1 ], SLC [13 :8 :- 1 ])
42
- assert_slices_equivalent (
43
- SLC [Timestamp ("2015-02-01" ) : Timestamp ("2014-10-01" ) : - 1 ], SLC [13 :8 :- 1 ]
44
- )
45
- assert_slices_equivalent (
46
- SLC ["2015-02-01" : Timestamp ("2014-10-01" ) : - 1 ], SLC [13 :8 :- 1 ]
47
- )
48
- assert_slices_equivalent (
49
- SLC [Timestamp ("2015-02-01" ) : "2014-10-01" : - 1 ], SLC [13 :8 :- 1 ]
50
- )
51
-
52
- assert_slices_equivalent (SLC ["2014-10-01" :"2015-02-01" :- 1 ], SLC [0 :0 :- 1 ])
53
-
54
- def test_slice_with_zero_step_raises (self ):
55
- ts = Series (np .arange (20 ), date_range ("2014-01-01" , periods = 20 , freq = "MS" ))
56
- with pytest .raises (ValueError , match = "slice step cannot be zero" ):
57
- ts [::0 ]
58
- with pytest .raises (ValueError , match = "slice step cannot be zero" ):
59
- ts .loc [::0 ]
60
- with pytest .raises (ValueError , match = "slice step cannot be zero" ):
61
- ts .loc [::0 ]
62
-
63
23
def test_monotone_DTI_indexing_bug (self ):
64
24
# GH 19362
65
25
# Testing accessing the first element in a monotonic descending
66
26
# partial string indexing.
67
27
68
- df = pd . DataFrame (list (range (5 )))
28
+ df = DataFrame (list (range (5 )))
69
29
date_list = [
70
30
"2018-01-02" ,
71
31
"2017-02-10" ,
72
32
"2016-03-10" ,
73
33
"2015-03-15" ,
74
34
"2014-03-16" ,
75
35
]
76
- date_index = pd . to_datetime (date_list )
36
+ date_index = DatetimeIndex (date_list )
77
37
df ["date" ] = date_index
78
- expected = pd . DataFrame ({0 : list (range (5 )), "date" : date_index })
38
+ expected = DataFrame ({0 : list (range (5 )), "date" : date_index })
79
39
tm .assert_frame_equal (df , expected )
80
40
81
- df = pd .DataFrame (
82
- {"A" : [1 , 2 , 3 ]}, index = pd .date_range ("20170101" , periods = 3 )[::- 1 ]
83
- )
84
- expected = pd .DataFrame (
85
- {"A" : 1 }, index = pd .date_range ("20170103" , periods = 1 )[::- 1 ]
86
- )
41
+ df = DataFrame ({"A" : [1 , 2 , 3 ]}, index = date_range ("20170101" , periods = 3 )[::- 1 ])
42
+ expected = DataFrame ({"A" : 1 }, index = date_range ("20170103" , periods = 1 )[::- 1 ])
87
43
tm .assert_frame_equal (df .loc ["2017-01-03" ], expected )
88
44
89
45
def test_slice_year (self ):
@@ -120,7 +76,7 @@ def test_slice_end_of_period_resolution(self, partial_dtime):
120
76
# GH#31064
121
77
dti = date_range ("2019-12-31 23:59:55.999999999" , periods = 10 , freq = "s" )
122
78
123
- ser = pd . Series (range (10 ), index = dti )
79
+ ser = Series (range (10 ), index = dti )
124
80
result = ser [partial_dtime ]
125
81
expected = ser .iloc [:5 ]
126
82
tm .assert_series_equal (result , expected )
@@ -321,7 +277,7 @@ def test_partial_slicing_with_multiindex(self):
321
277
tm .assert_frame_equal (result , expected )
322
278
323
279
expected = df_multi .loc [
324
- (pd . Timestamp ("2013-06-19 09:30:00" , tz = None ), "ACCT1" , "ABC" )
280
+ (Timestamp ("2013-06-19 09:30:00" , tz = None ), "ACCT1" , "ABC" )
325
281
]
326
282
result = df_multi .loc [("2013-06-19 09:30:00" , "ACCT1" , "ABC" )]
327
283
tm .assert_series_equal (result , expected )
@@ -334,31 +290,31 @@ def test_partial_slicing_with_multiindex(self):
334
290
335
291
# GH 4294
336
292
# partial slice on a series mi
337
- s = pd . DataFrame (
338
- np .random .rand (1000 , 1000 ), index = pd . date_range ("2000-1-1" , periods = 1000 )
293
+ s = DataFrame (
294
+ np .random .rand (1000 , 1000 ), index = date_range ("2000-1-1" , periods = 1000 )
339
295
).stack ()
340
296
341
297
s2 = s [:- 1 ].copy ()
342
298
expected = s2 ["2000-1-4" ]
343
- result = s2 [pd . Timestamp ("2000-1-4" )]
299
+ result = s2 [Timestamp ("2000-1-4" )]
344
300
tm .assert_series_equal (result , expected )
345
301
346
- result = s [pd . Timestamp ("2000-1-4" )]
302
+ result = s [Timestamp ("2000-1-4" )]
347
303
expected = s ["2000-1-4" ]
348
304
tm .assert_series_equal (result , expected )
349
305
350
- df2 = pd . DataFrame (s )
306
+ df2 = DataFrame (s )
351
307
expected = df2 .xs ("2000-1-4" )
352
- result = df2 .loc [pd . Timestamp ("2000-1-4" )]
308
+ result = df2 .loc [Timestamp ("2000-1-4" )]
353
309
tm .assert_frame_equal (result , expected )
354
310
355
311
def test_partial_slice_doesnt_require_monotonicity (self ):
356
312
# For historical reasons.
357
- s = pd . Series (np .arange (10 ), pd . date_range ("2014-01-01" , periods = 10 ))
313
+ s = Series (np .arange (10 ), date_range ("2014-01-01" , periods = 10 ))
358
314
359
315
nonmonotonic = s [[3 , 5 , 4 ]]
360
316
expected = nonmonotonic .iloc [:0 ]
361
- timestamp = pd . Timestamp ("2014-01-10" )
317
+ timestamp = Timestamp ("2014-01-10" )
362
318
363
319
tm .assert_series_equal (nonmonotonic ["2014-01-10" :], expected )
364
320
with pytest .raises (KeyError , match = r"Timestamp\('2014-01-10 00:00:00'\)" ):
@@ -370,9 +326,9 @@ def test_partial_slice_doesnt_require_monotonicity(self):
370
326
371
327
def test_loc_datetime_length_one (self ):
372
328
# GH16071
373
- df = pd . DataFrame (
329
+ df = DataFrame (
374
330
columns = ["1" ],
375
- index = pd . date_range ("2016-10-01T00:00:00" , "2016-10-01T23:59:59" ),
331
+ index = date_range ("2016-10-01T00:00:00" , "2016-10-01T23:59:59" ),
376
332
)
377
333
result = df .loc [datetime (2016 , 10 , 1 ) :]
378
334
tm .assert_frame_equal (result , df )
@@ -403,10 +359,10 @@ def test_selection_by_datetimelike(self, datetimelike, op, expected):
403
359
df = DataFrame (
404
360
{
405
361
"A" : [
406
- pd . Timestamp ("20120101" ),
407
- pd . Timestamp ("20130101" ),
362
+ Timestamp ("20120101" ),
363
+ Timestamp ("20130101" ),
408
364
np .nan ,
409
- pd . Timestamp ("20130103" ),
365
+ Timestamp ("20130103" ),
410
366
]
411
367
}
412
368
)
@@ -418,26 +374,26 @@ def test_selection_by_datetimelike(self, datetimelike, op, expected):
418
374
"start" ,
419
375
[
420
376
"2018-12-02 21:50:00+00:00" ,
421
- pd . Timestamp ("2018-12-02 21:50:00+00:00" ),
422
- pd . Timestamp ("2018-12-02 21:50:00+00:00" ).to_pydatetime (),
377
+ Timestamp ("2018-12-02 21:50:00+00:00" ),
378
+ Timestamp ("2018-12-02 21:50:00+00:00" ).to_pydatetime (),
423
379
],
424
380
)
425
381
@pytest .mark .parametrize (
426
382
"end" ,
427
383
[
428
384
"2018-12-02 21:52:00+00:00" ,
429
- pd . Timestamp ("2018-12-02 21:52:00+00:00" ),
430
- pd . Timestamp ("2018-12-02 21:52:00+00:00" ).to_pydatetime (),
385
+ Timestamp ("2018-12-02 21:52:00+00:00" ),
386
+ Timestamp ("2018-12-02 21:52:00+00:00" ).to_pydatetime (),
431
387
],
432
388
)
433
389
def test_getitem_with_datestring_with_UTC_offset (self , start , end ):
434
390
# GH 24076
435
- idx = pd . date_range (
391
+ idx = date_range (
436
392
start = "2018-12-02 14:50:00-07:00" ,
437
393
end = "2018-12-02 14:50:00-07:00" ,
438
394
freq = "1min" ,
439
395
)
440
- df = pd . DataFrame (1 , index = idx , columns = ["A" ])
396
+ df = DataFrame (1 , index = idx , columns = ["A" ])
441
397
result = df [start :end ]
442
398
expected = df .iloc [0 :3 , :]
443
399
tm .assert_frame_equal (result , expected )
@@ -454,11 +410,9 @@ def test_getitem_with_datestring_with_UTC_offset(self, start, end):
454
410
455
411
def test_slice_reduce_to_series (self ):
456
412
# GH 27516
457
- df = pd .DataFrame (
458
- {"A" : range (24 )}, index = pd .date_range ("2000" , periods = 24 , freq = "M" )
459
- )
460
- expected = pd .Series (
461
- range (12 ), index = pd .date_range ("2000" , periods = 12 , freq = "M" ), name = "A"
413
+ df = DataFrame ({"A" : range (24 )}, index = date_range ("2000" , periods = 24 , freq = "M" ))
414
+ expected = Series (
415
+ range (12 ), index = date_range ("2000" , periods = 12 , freq = "M" ), name = "A"
462
416
)
463
417
result = df .loc ["2000" , "A" ]
464
418
tm .assert_series_equal (result , expected )
0 commit comments