32
32
33
33
34
34
class Base :
35
- def setup_method (self , method ):
36
- self . dtype = self . create ( )
35
+ def test_hash (self , dtype ):
36
+ hash ( dtype )
37
37
38
- def test_hash (self ):
39
- hash (self .dtype )
38
+ def test_equality_invalid (self , dtype ):
39
+ assert not dtype == "foo"
40
+ assert not is_dtype_equal (dtype , np .int64 )
40
41
41
- def test_equality_invalid (self ):
42
- assert not self .dtype == "foo"
43
- assert not is_dtype_equal (self .dtype , np .int64 )
44
-
45
- def test_numpy_informed (self ):
42
+ def test_numpy_informed (self , dtype ):
46
43
with pytest .raises (TypeError , match = "data type not understood" ):
47
- np .dtype (self . dtype )
44
+ np .dtype (dtype )
48
45
49
- assert not self . dtype == np .str_
50
- assert not np .str_ == self . dtype
46
+ assert not dtype == np .str_
47
+ assert not np .str_ == dtype
51
48
52
- def test_pickle (self ):
49
+ def test_pickle (self , dtype ):
53
50
# make sure our cache is NOT pickled
54
51
55
52
# clear the cache
56
- type (self . dtype ).reset_cache ()
57
- assert not len (self . dtype ._cache )
53
+ type (dtype ).reset_cache ()
54
+ assert not len (dtype ._cache )
58
55
59
56
# force back to the cache
60
- result = tm .round_trip_pickle (self . dtype )
61
- assert not len (self . dtype ._cache )
62
- assert result == self . dtype
57
+ result = tm .round_trip_pickle (dtype )
58
+ assert not len (dtype ._cache )
59
+ assert result == dtype
63
60
64
61
65
62
class TestCategoricalDtype (Base ):
66
- def create (self ):
63
+ @pytest .fixture
64
+ def dtype (self ):
65
+ """
66
+ Class level fixture of dtype for TestCategoricalDtype
67
+ """
67
68
return CategoricalDtype ()
68
69
69
- def test_pickle (self ):
70
- # make sure our cache is NOT pickled
71
-
72
- # clear the cache
73
- type (self .dtype ).reset_cache ()
74
- assert not len (self .dtype ._cache )
75
-
76
- # force back to the cache
77
- result = tm .round_trip_pickle (self .dtype )
78
- assert result == self .dtype
79
-
80
- def test_hash_vs_equality (self ):
81
- dtype = self .dtype
70
+ def test_hash_vs_equality (self , dtype ):
82
71
dtype2 = CategoricalDtype ()
83
72
assert dtype == dtype2
84
73
assert dtype2 == dtype
85
74
assert hash (dtype ) == hash (dtype2 )
86
75
87
- def test_equality (self ):
88
- assert is_dtype_equal (self . dtype , "category" )
89
- assert is_dtype_equal (self . dtype , CategoricalDtype ())
90
- assert not is_dtype_equal (self . dtype , "foo" )
76
+ def test_equality (self , dtype ):
77
+ assert is_dtype_equal (dtype , "category" )
78
+ assert is_dtype_equal (dtype , CategoricalDtype ())
79
+ assert not is_dtype_equal (dtype , "foo" )
91
80
92
- def test_construction_from_string (self ):
81
+ def test_construction_from_string (self , dtype ):
93
82
result = CategoricalDtype .construct_from_string ("category" )
94
- assert is_dtype_equal (self . dtype , result )
83
+ assert is_dtype_equal (dtype , result )
95
84
msg = "Cannot construct a 'CategoricalDtype' from 'foo'"
96
85
with pytest .raises (TypeError , match = msg ):
97
86
CategoricalDtype .construct_from_string ("foo" )
@@ -133,16 +122,16 @@ def test_from_values_or_dtype_raises(self, values, categories, ordered, dtype):
133
122
with pytest .raises (ValueError , match = msg ):
134
123
CategoricalDtype ._from_values_or_dtype (values , categories , ordered , dtype )
135
124
136
- def test_is_dtype (self ):
137
- assert CategoricalDtype .is_dtype (self . dtype )
125
+ def test_is_dtype (self , dtype ):
126
+ assert CategoricalDtype .is_dtype (dtype )
138
127
assert CategoricalDtype .is_dtype ("category" )
139
128
assert CategoricalDtype .is_dtype (CategoricalDtype ())
140
129
assert not CategoricalDtype .is_dtype ("foo" )
141
130
assert not CategoricalDtype .is_dtype (np .float64 )
142
131
143
- def test_basic (self ):
132
+ def test_basic (self , dtype ):
144
133
145
- assert is_categorical_dtype (self . dtype )
134
+ assert is_categorical_dtype (dtype )
146
135
147
136
factor = Categorical (["a" , "b" , "b" , "a" , "a" , "c" , "c" , "c" ])
148
137
@@ -180,7 +169,11 @@ def test_is_boolean(self, categories, expected):
180
169
181
170
182
171
class TestDatetimeTZDtype (Base ):
183
- def create (self ):
172
+ @pytest .fixture
173
+ def dtype (self ):
174
+ """
175
+ Class level fixture of dtype for TestDatetimeTZDtype
176
+ """
184
177
return DatetimeTZDtype ("ns" , "US/Eastern" )
185
178
186
179
def test_alias_to_unit_raises (self ):
@@ -196,9 +189,8 @@ def test_alias_to_unit_bad_alias_raises(self):
196
189
with pytest .raises (TypeError , match = "" ):
197
190
DatetimeTZDtype ("datetime64[ns, US/NotATZ]" )
198
191
199
- def test_hash_vs_equality (self ):
192
+ def test_hash_vs_equality (self , dtype ):
200
193
# make sure that we satisfy is semantics
201
- dtype = self .dtype
202
194
dtype2 = DatetimeTZDtype ("ns" , "US/Eastern" )
203
195
dtype3 = DatetimeTZDtype (dtype2 )
204
196
assert dtype == dtype2
@@ -223,19 +215,19 @@ def test_subclass(self):
223
215
assert issubclass (type (a ), type (a ))
224
216
assert issubclass (type (a ), type (b ))
225
217
226
- def test_compat (self ):
227
- assert is_datetime64tz_dtype (self . dtype )
218
+ def test_compat (self , dtype ):
219
+ assert is_datetime64tz_dtype (dtype )
228
220
assert is_datetime64tz_dtype ("datetime64[ns, US/Eastern]" )
229
- assert is_datetime64_any_dtype (self . dtype )
221
+ assert is_datetime64_any_dtype (dtype )
230
222
assert is_datetime64_any_dtype ("datetime64[ns, US/Eastern]" )
231
- assert is_datetime64_ns_dtype (self . dtype )
223
+ assert is_datetime64_ns_dtype (dtype )
232
224
assert is_datetime64_ns_dtype ("datetime64[ns, US/Eastern]" )
233
- assert not is_datetime64_dtype (self . dtype )
225
+ assert not is_datetime64_dtype (dtype )
234
226
assert not is_datetime64_dtype ("datetime64[ns, US/Eastern]" )
235
227
236
- def test_construction_from_string (self ):
228
+ def test_construction_from_string (self , dtype ):
237
229
result = DatetimeTZDtype .construct_from_string ("datetime64[ns, US/Eastern]" )
238
- assert is_dtype_equal (self . dtype , result )
230
+ assert is_dtype_equal (dtype , result )
239
231
msg = "Cannot construct a 'DatetimeTZDtype' from 'foo'"
240
232
with pytest .raises (TypeError , match = msg ):
241
233
DatetimeTZDtype .construct_from_string ("foo" )
@@ -258,29 +250,29 @@ def test_construct_from_string_raises(self):
258
250
# dateutil str that returns None from gettz
259
251
DatetimeTZDtype .construct_from_string ("datetime64[ns, dateutil/invalid]" )
260
252
261
- def test_is_dtype (self ):
253
+ def test_is_dtype (self , dtype ):
262
254
assert not DatetimeTZDtype .is_dtype (None )
263
- assert DatetimeTZDtype .is_dtype (self . dtype )
255
+ assert DatetimeTZDtype .is_dtype (dtype )
264
256
assert DatetimeTZDtype .is_dtype ("datetime64[ns, US/Eastern]" )
265
257
assert not DatetimeTZDtype .is_dtype ("foo" )
266
258
assert DatetimeTZDtype .is_dtype (DatetimeTZDtype ("ns" , "US/Pacific" ))
267
259
assert not DatetimeTZDtype .is_dtype (np .float64 )
268
260
269
- def test_equality (self ):
270
- assert is_dtype_equal (self . dtype , "datetime64[ns, US/Eastern]" )
271
- assert is_dtype_equal (self . dtype , DatetimeTZDtype ("ns" , "US/Eastern" ))
272
- assert not is_dtype_equal (self . dtype , "foo" )
273
- assert not is_dtype_equal (self . dtype , DatetimeTZDtype ("ns" , "CET" ))
261
+ def test_equality (self , dtype ):
262
+ assert is_dtype_equal (dtype , "datetime64[ns, US/Eastern]" )
263
+ assert is_dtype_equal (dtype , DatetimeTZDtype ("ns" , "US/Eastern" ))
264
+ assert not is_dtype_equal (dtype , "foo" )
265
+ assert not is_dtype_equal (dtype , DatetimeTZDtype ("ns" , "CET" ))
274
266
assert not is_dtype_equal (
275
267
DatetimeTZDtype ("ns" , "US/Eastern" ), DatetimeTZDtype ("ns" , "US/Pacific" )
276
268
)
277
269
278
270
# numpy compat
279
271
assert is_dtype_equal (np .dtype ("M8[ns]" ), "datetime64[ns]" )
280
272
281
- def test_basic (self ):
273
+ def test_basic (self , dtype ):
282
274
283
- assert is_datetime64tz_dtype (self . dtype )
275
+ assert is_datetime64tz_dtype (dtype )
284
276
285
277
dr = date_range ("20130101" , periods = 3 , tz = "US/Eastern" )
286
278
s = Series (dr , name = "A" )
@@ -326,12 +318,15 @@ def test_tz_standardize(self):
326
318
327
319
328
320
class TestPeriodDtype (Base ):
329
- def create (self ):
321
+ @pytest .fixture
322
+ def dtype (self ):
323
+ """
324
+ Class level fixture of dtype for TestPeriodDtype
325
+ """
330
326
return PeriodDtype ("D" )
331
327
332
- def test_hash_vs_equality (self ):
328
+ def test_hash_vs_equality (self , dtype ):
333
329
# make sure that we satisfy is semantics
334
- dtype = self .dtype
335
330
dtype2 = PeriodDtype ("D" )
336
331
dtype3 = PeriodDtype (dtype2 )
337
332
assert dtype == dtype2
@@ -386,17 +381,17 @@ def test_identity(self):
386
381
assert PeriodDtype ("period[1S1U]" ) == PeriodDtype ("period[1000001U]" )
387
382
assert PeriodDtype ("period[1S1U]" ) is PeriodDtype ("period[1000001U]" )
388
383
389
- def test_compat (self ):
390
- assert not is_datetime64_ns_dtype (self . dtype )
384
+ def test_compat (self , dtype ):
385
+ assert not is_datetime64_ns_dtype (dtype )
391
386
assert not is_datetime64_ns_dtype ("period[D]" )
392
- assert not is_datetime64_dtype (self . dtype )
387
+ assert not is_datetime64_dtype (dtype )
393
388
assert not is_datetime64_dtype ("period[D]" )
394
389
395
- def test_construction_from_string (self ):
390
+ def test_construction_from_string (self , dtype ):
396
391
result = PeriodDtype ("period[D]" )
397
- assert is_dtype_equal (self . dtype , result )
392
+ assert is_dtype_equal (dtype , result )
398
393
result = PeriodDtype .construct_from_string ("period[D]" )
399
- assert is_dtype_equal (self . dtype , result )
394
+ assert is_dtype_equal (dtype , result )
400
395
with pytest .raises (TypeError ):
401
396
PeriodDtype .construct_from_string ("foo" )
402
397
with pytest .raises (TypeError ):
@@ -412,8 +407,8 @@ def test_construction_from_string(self):
412
407
with pytest .raises (TypeError , match = "list" ):
413
408
PeriodDtype .construct_from_string ([1 , 2 , 3 ])
414
409
415
- def test_is_dtype (self ):
416
- assert PeriodDtype .is_dtype (self . dtype )
410
+ def test_is_dtype (self , dtype ):
411
+ assert PeriodDtype .is_dtype (dtype )
417
412
assert PeriodDtype .is_dtype ("period[D]" )
418
413
assert PeriodDtype .is_dtype ("period[3D]" )
419
414
assert PeriodDtype .is_dtype (PeriodDtype ("3D" ))
@@ -431,17 +426,17 @@ def test_is_dtype(self):
431
426
assert not PeriodDtype .is_dtype (np .int64 )
432
427
assert not PeriodDtype .is_dtype (np .float64 )
433
428
434
- def test_equality (self ):
435
- assert is_dtype_equal (self . dtype , "period[D]" )
436
- assert is_dtype_equal (self . dtype , PeriodDtype ("D" ))
437
- assert is_dtype_equal (self . dtype , PeriodDtype ("D" ))
429
+ def test_equality (self , dtype ):
430
+ assert is_dtype_equal (dtype , "period[D]" )
431
+ assert is_dtype_equal (dtype , PeriodDtype ("D" ))
432
+ assert is_dtype_equal (dtype , PeriodDtype ("D" ))
438
433
assert is_dtype_equal (PeriodDtype ("D" ), PeriodDtype ("D" ))
439
434
440
- assert not is_dtype_equal (self . dtype , "D" )
435
+ assert not is_dtype_equal (dtype , "D" )
441
436
assert not is_dtype_equal (PeriodDtype ("D" ), PeriodDtype ("2D" ))
442
437
443
- def test_basic (self ):
444
- assert is_period_dtype (self . dtype )
438
+ def test_basic (self , dtype ):
439
+ assert is_period_dtype (dtype )
445
440
446
441
pidx = pd .period_range ("2013-01-01 09:00" , periods = 5 , freq = "H" )
447
442
@@ -467,12 +462,15 @@ def test_not_string(self):
467
462
468
463
469
464
class TestIntervalDtype (Base ):
470
- def create (self ):
465
+ @pytest .fixture
466
+ def dtype (self ):
467
+ """
468
+ Class level fixture of dtype for TestIntervalDtype
469
+ """
471
470
return IntervalDtype ("int64" )
472
471
473
- def test_hash_vs_equality (self ):
472
+ def test_hash_vs_equality (self , dtype ):
474
473
# make sure that we satisfy is semantics
475
- dtype = self .dtype
476
474
dtype2 = IntervalDtype ("int64" )
477
475
dtype3 = IntervalDtype (dtype2 )
478
476
assert dtype == dtype2
@@ -539,11 +537,11 @@ def test_construction_errors(self, subtype):
539
537
with pytest .raises (TypeError , match = msg ):
540
538
IntervalDtype (subtype )
541
539
542
- def test_construction_from_string (self ):
540
+ def test_construction_from_string (self , dtype ):
543
541
result = IntervalDtype ("interval[int64]" )
544
- assert is_dtype_equal (self . dtype , result )
542
+ assert is_dtype_equal (dtype , result )
545
543
result = IntervalDtype .construct_from_string ("interval[int64]" )
546
- assert is_dtype_equal (self . dtype , result )
544
+ assert is_dtype_equal (dtype , result )
547
545
548
546
@pytest .mark .parametrize ("string" , [0 , 3.14 , ("a" , "b" ), None ])
549
547
def test_construction_from_string_errors (self , string ):
@@ -572,8 +570,8 @@ def test_subclass(self):
572
570
assert issubclass (type (a ), type (a ))
573
571
assert issubclass (type (a ), type (b ))
574
572
575
- def test_is_dtype (self ):
576
- assert IntervalDtype .is_dtype (self . dtype )
573
+ def test_is_dtype (self , dtype ):
574
+ assert IntervalDtype .is_dtype (dtype )
577
575
assert IntervalDtype .is_dtype ("interval" )
578
576
assert IntervalDtype .is_dtype (IntervalDtype ("float64" ))
579
577
assert IntervalDtype .is_dtype (IntervalDtype ("int64" ))
@@ -589,12 +587,12 @@ def test_is_dtype(self):
589
587
assert not IntervalDtype .is_dtype (np .int64 )
590
588
assert not IntervalDtype .is_dtype (np .float64 )
591
589
592
- def test_equality (self ):
593
- assert is_dtype_equal (self . dtype , "interval[int64]" )
594
- assert is_dtype_equal (self . dtype , IntervalDtype ("int64" ))
590
+ def test_equality (self , dtype ):
591
+ assert is_dtype_equal (dtype , "interval[int64]" )
592
+ assert is_dtype_equal (dtype , IntervalDtype ("int64" ))
595
593
assert is_dtype_equal (IntervalDtype ("int64" ), IntervalDtype ("int64" ))
596
594
597
- assert not is_dtype_equal (self . dtype , "int64" )
595
+ assert not is_dtype_equal (dtype , "int64" )
598
596
assert not is_dtype_equal (IntervalDtype ("int64" ), IntervalDtype ("float64" ))
599
597
600
598
# invalid subtype comparisons do not raise when directly compared
@@ -650,8 +648,8 @@ def test_name_repr_generic(self, subtype):
650
648
assert str (dtype ) == "interval"
651
649
assert dtype .name == "interval"
652
650
653
- def test_basic (self ):
654
- assert is_interval_dtype (self . dtype )
651
+ def test_basic (self , dtype ):
652
+ assert is_interval_dtype (dtype )
655
653
656
654
ii = IntervalIndex .from_breaks (range (3 ))
657
655
0 commit comments