3232
3333
3434class Base :
35- def setup_method (self , method ):
36- self . dtype = self . create ( )
35+ def test_hash (self , dtype ):
36+ hash ( dtype )
3737
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 )
4041
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 ):
4643 with pytest .raises (TypeError , match = "data type not understood" ):
47- np .dtype (self . dtype )
44+ np .dtype (dtype )
4845
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
5148
52- def test_pickle (self ):
49+ def test_pickle (self , dtype ):
5350 # make sure our cache is NOT pickled
5451
5552 # 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 )
5855
5956 # 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
6360
6461
6562class TestCategoricalDtype (Base ):
66- def create (self ):
63+ @pytest .fixture
64+ def dtype (self ):
65+ """
66+ Class level fixture of dtype for TestCategoricalDtype
67+ """
6768 return CategoricalDtype ()
6869
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 ):
8271 dtype2 = CategoricalDtype ()
8372 assert dtype == dtype2
8473 assert dtype2 == dtype
8574 assert hash (dtype ) == hash (dtype2 )
8675
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" )
9180
92- def test_construction_from_string (self ):
81+ def test_construction_from_string (self , dtype ):
9382 result = CategoricalDtype .construct_from_string ("category" )
94- assert is_dtype_equal (self . dtype , result )
83+ assert is_dtype_equal (dtype , result )
9584 msg = "Cannot construct a 'CategoricalDtype' from 'foo'"
9685 with pytest .raises (TypeError , match = msg ):
9786 CategoricalDtype .construct_from_string ("foo" )
@@ -133,16 +122,16 @@ def test_from_values_or_dtype_raises(self, values, categories, ordered, dtype):
133122 with pytest .raises (ValueError , match = msg ):
134123 CategoricalDtype ._from_values_or_dtype (values , categories , ordered , dtype )
135124
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 )
138127 assert CategoricalDtype .is_dtype ("category" )
139128 assert CategoricalDtype .is_dtype (CategoricalDtype ())
140129 assert not CategoricalDtype .is_dtype ("foo" )
141130 assert not CategoricalDtype .is_dtype (np .float64 )
142131
143- def test_basic (self ):
132+ def test_basic (self , dtype ):
144133
145- assert is_categorical_dtype (self . dtype )
134+ assert is_categorical_dtype (dtype )
146135
147136 factor = Categorical (["a" , "b" , "b" , "a" , "a" , "c" , "c" , "c" ])
148137
@@ -180,7 +169,11 @@ def test_is_boolean(self, categories, expected):
180169
181170
182171class TestDatetimeTZDtype (Base ):
183- def create (self ):
172+ @pytest .fixture
173+ def dtype (self ):
174+ """
175+ Class level fixture of dtype for TestDatetimeTZDtype
176+ """
184177 return DatetimeTZDtype ("ns" , "US/Eastern" )
185178
186179 def test_alias_to_unit_raises (self ):
@@ -196,9 +189,8 @@ def test_alias_to_unit_bad_alias_raises(self):
196189 with pytest .raises (TypeError , match = "" ):
197190 DatetimeTZDtype ("datetime64[ns, US/NotATZ]" )
198191
199- def test_hash_vs_equality (self ):
192+ def test_hash_vs_equality (self , dtype ):
200193 # make sure that we satisfy is semantics
201- dtype = self .dtype
202194 dtype2 = DatetimeTZDtype ("ns" , "US/Eastern" )
203195 dtype3 = DatetimeTZDtype (dtype2 )
204196 assert dtype == dtype2
@@ -223,19 +215,19 @@ def test_subclass(self):
223215 assert issubclass (type (a ), type (a ))
224216 assert issubclass (type (a ), type (b ))
225217
226- def test_compat (self ):
227- assert is_datetime64tz_dtype (self . dtype )
218+ def test_compat (self , dtype ):
219+ assert is_datetime64tz_dtype (dtype )
228220 assert is_datetime64tz_dtype ("datetime64[ns, US/Eastern]" )
229- assert is_datetime64_any_dtype (self . dtype )
221+ assert is_datetime64_any_dtype (dtype )
230222 assert is_datetime64_any_dtype ("datetime64[ns, US/Eastern]" )
231- assert is_datetime64_ns_dtype (self . dtype )
223+ assert is_datetime64_ns_dtype (dtype )
232224 assert is_datetime64_ns_dtype ("datetime64[ns, US/Eastern]" )
233- assert not is_datetime64_dtype (self . dtype )
225+ assert not is_datetime64_dtype (dtype )
234226 assert not is_datetime64_dtype ("datetime64[ns, US/Eastern]" )
235227
236- def test_construction_from_string (self ):
228+ def test_construction_from_string (self , dtype ):
237229 result = DatetimeTZDtype .construct_from_string ("datetime64[ns, US/Eastern]" )
238- assert is_dtype_equal (self . dtype , result )
230+ assert is_dtype_equal (dtype , result )
239231 msg = "Cannot construct a 'DatetimeTZDtype' from 'foo'"
240232 with pytest .raises (TypeError , match = msg ):
241233 DatetimeTZDtype .construct_from_string ("foo" )
@@ -258,29 +250,29 @@ def test_construct_from_string_raises(self):
258250 # dateutil str that returns None from gettz
259251 DatetimeTZDtype .construct_from_string ("datetime64[ns, dateutil/invalid]" )
260252
261- def test_is_dtype (self ):
253+ def test_is_dtype (self , dtype ):
262254 assert not DatetimeTZDtype .is_dtype (None )
263- assert DatetimeTZDtype .is_dtype (self . dtype )
255+ assert DatetimeTZDtype .is_dtype (dtype )
264256 assert DatetimeTZDtype .is_dtype ("datetime64[ns, US/Eastern]" )
265257 assert not DatetimeTZDtype .is_dtype ("foo" )
266258 assert DatetimeTZDtype .is_dtype (DatetimeTZDtype ("ns" , "US/Pacific" ))
267259 assert not DatetimeTZDtype .is_dtype (np .float64 )
268260
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" ))
274266 assert not is_dtype_equal (
275267 DatetimeTZDtype ("ns" , "US/Eastern" ), DatetimeTZDtype ("ns" , "US/Pacific" )
276268 )
277269
278270 # numpy compat
279271 assert is_dtype_equal (np .dtype ("M8[ns]" ), "datetime64[ns]" )
280272
281- def test_basic (self ):
273+ def test_basic (self , dtype ):
282274
283- assert is_datetime64tz_dtype (self . dtype )
275+ assert is_datetime64tz_dtype (dtype )
284276
285277 dr = date_range ("20130101" , periods = 3 , tz = "US/Eastern" )
286278 s = Series (dr , name = "A" )
@@ -326,12 +318,15 @@ def test_tz_standardize(self):
326318
327319
328320class TestPeriodDtype (Base ):
329- def create (self ):
321+ @pytest .fixture
322+ def dtype (self ):
323+ """
324+ Class level fixture of dtype for TestPeriodDtype
325+ """
330326 return PeriodDtype ("D" )
331327
332- def test_hash_vs_equality (self ):
328+ def test_hash_vs_equality (self , dtype ):
333329 # make sure that we satisfy is semantics
334- dtype = self .dtype
335330 dtype2 = PeriodDtype ("D" )
336331 dtype3 = PeriodDtype (dtype2 )
337332 assert dtype == dtype2
@@ -386,17 +381,17 @@ def test_identity(self):
386381 assert PeriodDtype ("period[1S1U]" ) == PeriodDtype ("period[1000001U]" )
387382 assert PeriodDtype ("period[1S1U]" ) is PeriodDtype ("period[1000001U]" )
388383
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 )
391386 assert not is_datetime64_ns_dtype ("period[D]" )
392- assert not is_datetime64_dtype (self . dtype )
387+ assert not is_datetime64_dtype (dtype )
393388 assert not is_datetime64_dtype ("period[D]" )
394389
395- def test_construction_from_string (self ):
390+ def test_construction_from_string (self , dtype ):
396391 result = PeriodDtype ("period[D]" )
397- assert is_dtype_equal (self . dtype , result )
392+ assert is_dtype_equal (dtype , result )
398393 result = PeriodDtype .construct_from_string ("period[D]" )
399- assert is_dtype_equal (self . dtype , result )
394+ assert is_dtype_equal (dtype , result )
400395 with pytest .raises (TypeError ):
401396 PeriodDtype .construct_from_string ("foo" )
402397 with pytest .raises (TypeError ):
@@ -412,8 +407,8 @@ def test_construction_from_string(self):
412407 with pytest .raises (TypeError , match = "list" ):
413408 PeriodDtype .construct_from_string ([1 , 2 , 3 ])
414409
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 )
417412 assert PeriodDtype .is_dtype ("period[D]" )
418413 assert PeriodDtype .is_dtype ("period[3D]" )
419414 assert PeriodDtype .is_dtype (PeriodDtype ("3D" ))
@@ -431,17 +426,17 @@ def test_is_dtype(self):
431426 assert not PeriodDtype .is_dtype (np .int64 )
432427 assert not PeriodDtype .is_dtype (np .float64 )
433428
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" ))
438433 assert is_dtype_equal (PeriodDtype ("D" ), PeriodDtype ("D" ))
439434
440- assert not is_dtype_equal (self . dtype , "D" )
435+ assert not is_dtype_equal (dtype , "D" )
441436 assert not is_dtype_equal (PeriodDtype ("D" ), PeriodDtype ("2D" ))
442437
443- def test_basic (self ):
444- assert is_period_dtype (self . dtype )
438+ def test_basic (self , dtype ):
439+ assert is_period_dtype (dtype )
445440
446441 pidx = pd .period_range ("2013-01-01 09:00" , periods = 5 , freq = "H" )
447442
@@ -467,12 +462,15 @@ def test_not_string(self):
467462
468463
469464class TestIntervalDtype (Base ):
470- def create (self ):
465+ @pytest .fixture
466+ def dtype (self ):
467+ """
468+ Class level fixture of dtype for TestIntervalDtype
469+ """
471470 return IntervalDtype ("int64" )
472471
473- def test_hash_vs_equality (self ):
472+ def test_hash_vs_equality (self , dtype ):
474473 # make sure that we satisfy is semantics
475- dtype = self .dtype
476474 dtype2 = IntervalDtype ("int64" )
477475 dtype3 = IntervalDtype (dtype2 )
478476 assert dtype == dtype2
@@ -539,11 +537,11 @@ def test_construction_errors(self, subtype):
539537 with pytest .raises (TypeError , match = msg ):
540538 IntervalDtype (subtype )
541539
542- def test_construction_from_string (self ):
540+ def test_construction_from_string (self , dtype ):
543541 result = IntervalDtype ("interval[int64]" )
544- assert is_dtype_equal (self . dtype , result )
542+ assert is_dtype_equal (dtype , result )
545543 result = IntervalDtype .construct_from_string ("interval[int64]" )
546- assert is_dtype_equal (self . dtype , result )
544+ assert is_dtype_equal (dtype , result )
547545
548546 @pytest .mark .parametrize ("string" , [0 , 3.14 , ("a" , "b" ), None ])
549547 def test_construction_from_string_errors (self , string ):
@@ -572,8 +570,8 @@ def test_subclass(self):
572570 assert issubclass (type (a ), type (a ))
573571 assert issubclass (type (a ), type (b ))
574572
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 )
577575 assert IntervalDtype .is_dtype ("interval" )
578576 assert IntervalDtype .is_dtype (IntervalDtype ("float64" ))
579577 assert IntervalDtype .is_dtype (IntervalDtype ("int64" ))
@@ -589,12 +587,12 @@ def test_is_dtype(self):
589587 assert not IntervalDtype .is_dtype (np .int64 )
590588 assert not IntervalDtype .is_dtype (np .float64 )
591589
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" ))
595593 assert is_dtype_equal (IntervalDtype ("int64" ), IntervalDtype ("int64" ))
596594
597- assert not is_dtype_equal (self . dtype , "int64" )
595+ assert not is_dtype_equal (dtype , "int64" )
598596 assert not is_dtype_equal (IntervalDtype ("int64" ), IntervalDtype ("float64" ))
599597
600598 # invalid subtype comparisons do not raise when directly compared
@@ -650,8 +648,8 @@ def test_name_repr_generic(self, subtype):
650648 assert str (dtype ) == "interval"
651649 assert dtype .name == "interval"
652650
653- def test_basic (self ):
654- assert is_interval_dtype (self . dtype )
651+ def test_basic (self , dtype ):
652+ assert is_interval_dtype (dtype )
655653
656654 ii = IntervalIndex .from_breaks (range (3 ))
657655
0 commit comments