25
25
)
26
26
from pandas .core .indexes .datetimes import DatetimeIndex
27
27
from pandas .core .indexes .timedeltas import TimedeltaIndex
28
- from pandas .tests .series .common import TestData
29
28
import pandas .util .testing as tm
30
29
from pandas .util .testing import (
31
30
assert_almost_equal ,
@@ -47,32 +46,34 @@ def assert_range_equal(left, right):
47
46
assert left .tz == right .tz
48
47
49
48
50
- class TestTimeSeries ( TestData ) :
51
- def test_shift (self ):
52
- shifted = self . ts .shift (1 )
49
+ class TestTimeSeries :
50
+ def test_shift (self , datetime_series ):
51
+ shifted = datetime_series .shift (1 )
53
52
unshifted = shifted .shift (- 1 )
54
53
55
- tm .assert_index_equal (shifted .index , self .ts .index )
56
- tm .assert_index_equal (unshifted .index , self .ts .index )
57
- tm .assert_numpy_array_equal (unshifted .dropna ().values , self .ts .values [:- 1 ])
54
+ tm .assert_index_equal (shifted .index , datetime_series .index )
55
+ tm .assert_index_equal (unshifted .index , datetime_series .index )
56
+ tm .assert_numpy_array_equal (
57
+ unshifted .dropna ().values , datetime_series .values [:- 1 ]
58
+ )
58
59
59
60
offset = BDay ()
60
- shifted = self . ts .shift (1 , freq = offset )
61
+ shifted = datetime_series .shift (1 , freq = offset )
61
62
unshifted = shifted .shift (- 1 , freq = offset )
62
63
63
- assert_series_equal (unshifted , self . ts )
64
+ assert_series_equal (unshifted , datetime_series )
64
65
65
- unshifted = self . ts .shift (0 , freq = offset )
66
- assert_series_equal (unshifted , self . ts )
66
+ unshifted = datetime_series .shift (0 , freq = offset )
67
+ assert_series_equal (unshifted , datetime_series )
67
68
68
- shifted = self . ts .shift (1 , freq = "B" )
69
+ shifted = datetime_series .shift (1 , freq = "B" )
69
70
unshifted = shifted .shift (- 1 , freq = "B" )
70
71
71
- assert_series_equal (unshifted , self . ts )
72
+ assert_series_equal (unshifted , datetime_series )
72
73
73
74
# corner case
74
- unshifted = self . ts .shift (0 )
75
- assert_series_equal (unshifted , self . ts )
75
+ unshifted = datetime_series .shift (0 )
76
+ assert_series_equal (unshifted , datetime_series )
76
77
77
78
# Shifting with PeriodIndex
78
79
ps = tm .makePeriodSeries ()
@@ -208,7 +209,7 @@ def test_shift_dst(self):
208
209
tm .assert_series_equal (res , exp )
209
210
assert res .dtype == "datetime64[ns, US/Eastern]"
210
211
211
- def test_tshift (self ):
212
+ def test_tshift (self , datetime_series ):
212
213
# PeriodIndex
213
214
ps = tm .makePeriodSeries ()
214
215
shifted = ps .tshift (1 )
@@ -227,34 +228,34 @@ def test_tshift(self):
227
228
ps .tshift (freq = "M" )
228
229
229
230
# DatetimeIndex
230
- shifted = self . ts .tshift (1 )
231
+ shifted = datetime_series .tshift (1 )
231
232
unshifted = shifted .tshift (- 1 )
232
233
233
- assert_series_equal (self . ts , unshifted )
234
+ assert_series_equal (datetime_series , unshifted )
234
235
235
- shifted2 = self . ts . tshift (freq = self . ts .index .freq )
236
+ shifted2 = datetime_series . tshift (freq = datetime_series .index .freq )
236
237
assert_series_equal (shifted , shifted2 )
237
238
238
239
inferred_ts = Series (
239
- self . ts . values , Index (np .asarray (self . ts .index )), name = "ts"
240
+ datetime_series . values , Index (np .asarray (datetime_series .index )), name = "ts"
240
241
)
241
242
shifted = inferred_ts .tshift (1 )
242
243
unshifted = shifted .tshift (- 1 )
243
- assert_series_equal (shifted , self . ts .tshift (1 ))
244
+ assert_series_equal (shifted , datetime_series .tshift (1 ))
244
245
assert_series_equal (unshifted , inferred_ts )
245
246
246
- no_freq = self . ts [[0 , 5 , 7 ]]
247
+ no_freq = datetime_series [[0 , 5 , 7 ]]
247
248
msg = "Freq was not given and was not set in the index"
248
249
with pytest .raises (ValueError , match = msg ):
249
250
no_freq .tshift ()
250
251
251
- def test_truncate (self ):
252
+ def test_truncate (self , datetime_series ):
252
253
offset = BDay ()
253
254
254
- ts = self . ts [::3 ]
255
+ ts = datetime_series [::3 ]
255
256
256
- start , end = self . ts . index [3 ], self . ts .index [6 ]
257
- start_missing , end_missing = self . ts . index [2 ], self . ts .index [7 ]
257
+ start , end = datetime_series . index [3 ], datetime_series .index [6 ]
258
+ start_missing , end_missing = datetime_series . index [2 ], datetime_series .index [7 ]
258
259
259
260
# neither specified
260
261
truncated = ts .truncate ()
@@ -288,16 +289,17 @@ def test_truncate(self):
288
289
assert_series_equal (truncated , expected )
289
290
290
291
# corner case, empty series returned
291
- truncated = ts .truncate (after = self . ts .index [0 ] - offset )
292
+ truncated = ts .truncate (after = datetime_series .index [0 ] - offset )
292
293
assert len (truncated ) == 0
293
294
294
- truncated = ts .truncate (before = self . ts .index [- 1 ] + offset )
295
+ truncated = ts .truncate (before = datetime_series .index [- 1 ] + offset )
295
296
assert len (truncated ) == 0
296
297
297
298
msg = "Truncate: 1999-12-31 00:00:00 must be after 2000-02-14 00:00:00"
298
299
with pytest .raises (ValueError , match = msg ):
299
300
ts .truncate (
300
- before = self .ts .index [- 1 ] + offset , after = self .ts .index [0 ] - offset
301
+ before = datetime_series .index [- 1 ] + offset ,
302
+ after = datetime_series .index [0 ] - offset ,
301
303
)
302
304
303
305
def test_truncate_nonsortedindex (self ):
@@ -355,20 +357,20 @@ def test_asfreq_datetimeindex_empty_series(self):
355
357
)
356
358
tm .assert_index_equal (expected .index , result .index )
357
359
358
- def test_pct_change (self ):
359
- rs = self . ts .pct_change (fill_method = None )
360
- assert_series_equal (rs , self . ts / self . ts .shift (1 ) - 1 )
360
+ def test_pct_change (self , datetime_series ):
361
+ rs = datetime_series .pct_change (fill_method = None )
362
+ assert_series_equal (rs , datetime_series / datetime_series .shift (1 ) - 1 )
361
363
362
- rs = self . ts .pct_change (2 )
363
- filled = self . ts .fillna (method = "pad" )
364
+ rs = datetime_series .pct_change (2 )
365
+ filled = datetime_series .fillna (method = "pad" )
364
366
assert_series_equal (rs , filled / filled .shift (2 ) - 1 )
365
367
366
- rs = self . ts .pct_change (fill_method = "bfill" , limit = 1 )
367
- filled = self . ts .fillna (method = "bfill" , limit = 1 )
368
+ rs = datetime_series .pct_change (fill_method = "bfill" , limit = 1 )
369
+ filled = datetime_series .fillna (method = "bfill" , limit = 1 )
368
370
assert_series_equal (rs , filled / filled .shift (1 ) - 1 )
369
371
370
- rs = self . ts .pct_change (freq = "5D" )
371
- filled = self . ts .fillna (method = "pad" )
372
+ rs = datetime_series .pct_change (freq = "5D" )
373
+ filled = datetime_series .fillna (method = "pad" )
372
374
assert_series_equal (
373
375
rs , (filled / filled .shift (freq = "5D" ) - 1 ).reindex_like (filled )
374
376
)
@@ -391,46 +393,52 @@ def test_pct_change_shift_over_nas(self):
391
393
("14B" , 14 , None , None ),
392
394
],
393
395
)
394
- def test_pct_change_periods_freq (self , freq , periods , fill_method , limit ):
396
+ def test_pct_change_periods_freq (
397
+ self , freq , periods , fill_method , limit , datetime_series
398
+ ):
395
399
# GH 7292
396
- rs_freq = self .ts .pct_change (freq = freq , fill_method = fill_method , limit = limit )
397
- rs_periods = self .ts .pct_change (periods , fill_method = fill_method , limit = limit )
400
+ rs_freq = datetime_series .pct_change (
401
+ freq = freq , fill_method = fill_method , limit = limit
402
+ )
403
+ rs_periods = datetime_series .pct_change (
404
+ periods , fill_method = fill_method , limit = limit
405
+ )
398
406
assert_series_equal (rs_freq , rs_periods )
399
407
400
- empty_ts = Series (index = self . ts .index )
408
+ empty_ts = Series (index = datetime_series .index )
401
409
rs_freq = empty_ts .pct_change (freq = freq , fill_method = fill_method , limit = limit )
402
410
rs_periods = empty_ts .pct_change (periods , fill_method = fill_method , limit = limit )
403
411
assert_series_equal (rs_freq , rs_periods )
404
412
405
- def test_autocorr (self ):
413
+ def test_autocorr (self , datetime_series ):
406
414
# Just run the function
407
- corr1 = self . ts .autocorr ()
415
+ corr1 = datetime_series .autocorr ()
408
416
409
417
# Now run it with the lag parameter
410
- corr2 = self . ts .autocorr (lag = 1 )
418
+ corr2 = datetime_series .autocorr (lag = 1 )
411
419
412
420
# corr() with lag needs Series of at least length 2
413
- if len (self . ts ) <= 2 :
421
+ if len (datetime_series ) <= 2 :
414
422
assert np .isnan (corr1 )
415
423
assert np .isnan (corr2 )
416
424
else :
417
425
assert corr1 == corr2
418
426
419
427
# Choose a random lag between 1 and length of Series - 2
420
428
# and compare the result with the Series corr() function
421
- n = 1 + np .random .randint (max (1 , len (self . ts ) - 2 ))
422
- corr1 = self . ts . corr (self . ts .shift (n ))
423
- corr2 = self . ts .autocorr (lag = n )
429
+ n = 1 + np .random .randint (max (1 , len (datetime_series ) - 2 ))
430
+ corr1 = datetime_series . corr (datetime_series .shift (n ))
431
+ corr2 = datetime_series .autocorr (lag = n )
424
432
425
433
# corr() with lag needs Series of at least length 2
426
- if len (self . ts ) <= 2 :
434
+ if len (datetime_series ) <= 2 :
427
435
assert np .isnan (corr1 )
428
436
assert np .isnan (corr2 )
429
437
else :
430
438
assert corr1 == corr2
431
439
432
- def test_first_last_valid (self ):
433
- ts = self . ts .copy ()
440
+ def test_first_last_valid (self , datetime_series ):
441
+ ts = datetime_series .copy ()
434
442
ts [:5 ] = np .NaN
435
443
436
444
index = ts .first_valid_index ()
@@ -462,9 +470,9 @@ def test_first_last_valid(self):
462
470
assert ts .first_valid_index ().freq == ts .index .freq
463
471
assert ts .last_valid_index ().freq == ts .index .freq
464
472
465
- def test_mpl_compat_hack (self ):
466
- result = self . ts [:, np .newaxis ]
467
- expected = self . ts .values [:, np .newaxis ]
473
+ def test_mpl_compat_hack (self , datetime_series ):
474
+ result = datetime_series [:, np .newaxis ]
475
+ expected = datetime_series .values [:, np .newaxis ]
468
476
assert_almost_equal (result , expected )
469
477
470
478
def test_timeseries_coercion (self ):
0 commit comments