2525)
2626from pandas .core .indexes .datetimes import DatetimeIndex
2727from pandas .core .indexes .timedeltas import TimedeltaIndex
28- from pandas .tests .series .common import TestData
2928import pandas .util .testing as tm
3029from pandas .util .testing import (
3130 assert_almost_equal ,
@@ -47,32 +46,34 @@ def assert_range_equal(left, right):
4746 assert left .tz == right .tz
4847
4948
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 )
5352 unshifted = shifted .shift (- 1 )
5453
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+ )
5859
5960 offset = BDay ()
60- shifted = self . ts .shift (1 , freq = offset )
61+ shifted = datetime_series .shift (1 , freq = offset )
6162 unshifted = shifted .shift (- 1 , freq = offset )
6263
63- assert_series_equal (unshifted , self . ts )
64+ assert_series_equal (unshifted , datetime_series )
6465
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 )
6768
68- shifted = self . ts .shift (1 , freq = "B" )
69+ shifted = datetime_series .shift (1 , freq = "B" )
6970 unshifted = shifted .shift (- 1 , freq = "B" )
7071
71- assert_series_equal (unshifted , self . ts )
72+ assert_series_equal (unshifted , datetime_series )
7273
7374 # 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 )
7677
7778 # Shifting with PeriodIndex
7879 ps = tm .makePeriodSeries ()
@@ -208,7 +209,7 @@ def test_shift_dst(self):
208209 tm .assert_series_equal (res , exp )
209210 assert res .dtype == "datetime64[ns, US/Eastern]"
210211
211- def test_tshift (self ):
212+ def test_tshift (self , datetime_series ):
212213 # PeriodIndex
213214 ps = tm .makePeriodSeries ()
214215 shifted = ps .tshift (1 )
@@ -227,34 +228,34 @@ def test_tshift(self):
227228 ps .tshift (freq = "M" )
228229
229230 # DatetimeIndex
230- shifted = self . ts .tshift (1 )
231+ shifted = datetime_series .tshift (1 )
231232 unshifted = shifted .tshift (- 1 )
232233
233- assert_series_equal (self . ts , unshifted )
234+ assert_series_equal (datetime_series , unshifted )
234235
235- shifted2 = self . ts . tshift (freq = self . ts .index .freq )
236+ shifted2 = datetime_series . tshift (freq = datetime_series .index .freq )
236237 assert_series_equal (shifted , shifted2 )
237238
238239 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"
240241 )
241242 shifted = inferred_ts .tshift (1 )
242243 unshifted = shifted .tshift (- 1 )
243- assert_series_equal (shifted , self . ts .tshift (1 ))
244+ assert_series_equal (shifted , datetime_series .tshift (1 ))
244245 assert_series_equal (unshifted , inferred_ts )
245246
246- no_freq = self . ts [[0 , 5 , 7 ]]
247+ no_freq = datetime_series [[0 , 5 , 7 ]]
247248 msg = "Freq was not given and was not set in the index"
248249 with pytest .raises (ValueError , match = msg ):
249250 no_freq .tshift ()
250251
251- def test_truncate (self ):
252+ def test_truncate (self , datetime_series ):
252253 offset = BDay ()
253254
254- ts = self . ts [::3 ]
255+ ts = datetime_series [::3 ]
255256
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 ]
258259
259260 # neither specified
260261 truncated = ts .truncate ()
@@ -288,16 +289,17 @@ def test_truncate(self):
288289 assert_series_equal (truncated , expected )
289290
290291 # 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 )
292293 assert len (truncated ) == 0
293294
294- truncated = ts .truncate (before = self . ts .index [- 1 ] + offset )
295+ truncated = ts .truncate (before = datetime_series .index [- 1 ] + offset )
295296 assert len (truncated ) == 0
296297
297298 msg = "Truncate: 1999-12-31 00:00:00 must be after 2000-02-14 00:00:00"
298299 with pytest .raises (ValueError , match = msg ):
299300 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 ,
301303 )
302304
303305 def test_truncate_nonsortedindex (self ):
@@ -355,20 +357,20 @@ def test_asfreq_datetimeindex_empty_series(self):
355357 )
356358 tm .assert_index_equal (expected .index , result .index )
357359
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 )
361363
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" )
364366 assert_series_equal (rs , filled / filled .shift (2 ) - 1 )
365367
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 )
368370 assert_series_equal (rs , filled / filled .shift (1 ) - 1 )
369371
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" )
372374 assert_series_equal (
373375 rs , (filled / filled .shift (freq = "5D" ) - 1 ).reindex_like (filled )
374376 )
@@ -391,46 +393,52 @@ def test_pct_change_shift_over_nas(self):
391393 ("14B" , 14 , None , None ),
392394 ],
393395 )
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+ ):
395399 # 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+ )
398406 assert_series_equal (rs_freq , rs_periods )
399407
400- empty_ts = Series (index = self . ts .index )
408+ empty_ts = Series (index = datetime_series .index )
401409 rs_freq = empty_ts .pct_change (freq = freq , fill_method = fill_method , limit = limit )
402410 rs_periods = empty_ts .pct_change (periods , fill_method = fill_method , limit = limit )
403411 assert_series_equal (rs_freq , rs_periods )
404412
405- def test_autocorr (self ):
413+ def test_autocorr (self , datetime_series ):
406414 # Just run the function
407- corr1 = self . ts .autocorr ()
415+ corr1 = datetime_series .autocorr ()
408416
409417 # Now run it with the lag parameter
410- corr2 = self . ts .autocorr (lag = 1 )
418+ corr2 = datetime_series .autocorr (lag = 1 )
411419
412420 # corr() with lag needs Series of at least length 2
413- if len (self . ts ) <= 2 :
421+ if len (datetime_series ) <= 2 :
414422 assert np .isnan (corr1 )
415423 assert np .isnan (corr2 )
416424 else :
417425 assert corr1 == corr2
418426
419427 # Choose a random lag between 1 and length of Series - 2
420428 # 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 )
424432
425433 # corr() with lag needs Series of at least length 2
426- if len (self . ts ) <= 2 :
434+ if len (datetime_series ) <= 2 :
427435 assert np .isnan (corr1 )
428436 assert np .isnan (corr2 )
429437 else :
430438 assert corr1 == corr2
431439
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 ()
434442 ts [:5 ] = np .NaN
435443
436444 index = ts .first_valid_index ()
@@ -462,9 +470,9 @@ def test_first_last_valid(self):
462470 assert ts .first_valid_index ().freq == ts .index .freq
463471 assert ts .last_valid_index ().freq == ts .index .freq
464472
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 ]
468476 assert_almost_equal (result , expected )
469477
470478 def test_timeseries_coercion (self ):
0 commit comments