13
13
# Index / Series common tests which may trigger dtype coercions
14
14
###############################################################
15
15
16
+
16
17
@pytest .fixture (autouse = True , scope = 'class' )
17
18
def check_comprehensiveness (request ):
18
19
# Iterate over combination of dtype, method and klass
@@ -30,9 +31,10 @@ def has_test(combo):
30
31
if not has_test (combo ):
31
32
msg = 'test method is not defined: {0}, {1}'
32
33
raise AssertionError (msg .format (type (cls ), combo ))
33
-
34
+
34
35
yield
35
36
37
+
36
38
class CoercionBase (object ):
37
39
38
40
klasses = ['index' , 'series' ]
@@ -84,7 +86,7 @@ def test_setitem_series_object(self, val, exp_dtype):
84
86
85
87
exp = pd .Series (['a' , val , 'c' , 'd' ])
86
88
self ._assert_setitem_series_conversion (obj , val , exp , exp_dtype )
87
-
89
+
88
90
@pytest .mark .parametrize ("val,exp_dtype" , [
89
91
(1 , np .int64 ),
90
92
(1.1 , np .float64 ),
@@ -106,15 +108,14 @@ def test_setitem_series_int64(self, val, exp_dtype):
106
108
(np .int32 (1 ), np .int8 ),
107
109
(np .int16 (2 ** 9 ), np .int16 )])
108
110
def test_setitem_series_int8 (self , val , exp_dtype ):
109
- # integer dtype coercion (no change)
110
111
obj = pd .Series ([1 , 2 , 3 , 4 ], dtype = np .int8 )
111
112
assert obj .dtype == np .int8
112
113
113
114
if exp_dtype is np .int16 :
114
115
exp = pd .Series ([1 , 0 , 3 , 4 ], dtype = np .int8 )
115
116
self ._assert_setitem_series_conversion (obj , val , exp , np .int8 )
116
117
pytest .xfail ("BUG: it must be Series([1, 1, 3, 4], dtype=np.int16" )
117
-
118
+
118
119
exp = pd .Series ([1 , val , 3 , 4 ], dtype = np .int8 )
119
120
self ._assert_setitem_series_conversion (obj , val , exp , exp_dtype )
120
121
@@ -159,7 +160,7 @@ def test_setitem_series_bool(self, val, exp_dtype):
159
160
elif exp_dtype is np .float64 :
160
161
exp = pd .Series ([True , True , True , False ])
161
162
self ._assert_setitem_series_conversion (obj , val , exp , np .bool )
162
- pytest .xfail ("TODO_GH12747 The result must be float" )
163
+ pytest .xfail ("TODO_GH12747 The result must be float" )
163
164
elif exp_dtype is np .complex128 :
164
165
exp = pd .Series ([True , True , True , False ])
165
166
self ._assert_setitem_series_conversion (obj , val , exp , np .bool )
@@ -248,20 +249,19 @@ def test_setitem_index_object(self, val, exp_dtype):
248
249
assert obj .index .dtype == np .object
249
250
250
251
if exp_dtype is IndexError :
251
- # object + int -> IndexError, regarded as location
252
252
temp = obj .copy ()
253
253
with pytest .raises (exp_dtype ):
254
254
temp [5 ] = 5
255
255
else :
256
256
exp_index = pd .Index (list ('abcd' ) + [val ])
257
- self ._assert_setitem_index_conversion (obj , val , exp_index , exp_dtype )
257
+ self ._assert_setitem_index_conversion (obj , val , exp_index ,
258
+ exp_dtype )
258
259
259
260
@pytest .mark .parametrize ("val,exp_dtype" , [
260
261
(5 , np .int64 ),
261
262
(1.1 , np .float64 ),
262
263
('x' , np .object )])
263
264
def test_setitem_index_int64 (self , val , exp_dtype ):
264
- # tests setitem with non-existing numeric key
265
265
obj = pd .Series ([1 , 2 , 3 , 4 ])
266
266
assert obj .index .dtype == np .int64
267
267
@@ -273,7 +273,6 @@ def test_setitem_index_int64(self, val, exp_dtype):
273
273
(5.1 , np .float64 ),
274
274
('x' , np .object )])
275
275
def test_setitem_index_float64 (self , val , exp_dtype ):
276
- # tests setitem with non-existing numeric key
277
276
obj = pd .Series ([1 , 2 , 3 , 4 ], index = [1.1 , 2.1 , 3.1 , 4.1 ])
278
277
assert obj .index .dtype == np .float64
279
278
@@ -306,7 +305,7 @@ def test_setitem_index_timedelta64(self):
306
305
pass
307
306
308
307
def test_setitem_index_period (self ):
309
- pass
308
+ pass
310
309
311
310
312
311
class TestInsertIndexCoercion (CoercionBase ):
@@ -355,7 +354,6 @@ def test_insert_index_float64(self, insert, coerced_val, coerced_dtype):
355
354
obj = pd .Float64Index ([1. , 2. , 3. , 4. ])
356
355
assert obj .dtype == np .float64
357
356
358
- # float + int -> int
359
357
exp = pd .Index ([1. , coerced_val , 2. , 3. , 4. ])
360
358
self ._assert_insert_conversion (obj , insert , exp , coerced_dtype )
361
359
@@ -377,7 +375,7 @@ def test_insert_index_datetimes(self, fill_val, exp_dtype):
377
375
if fill_val .tz :
378
376
with tm .assert_raises_regex (ValueError , msg ):
379
377
obj .insert (1 , pd .Timestamp ('2012-01-01' ))
380
-
378
+
381
379
with tm .assert_raises_regex (ValueError , msg ):
382
380
obj .insert (1 , pd .Timestamp ('2012-01-01' , tz = 'Asia/Tokyo' ))
383
381
@@ -422,17 +420,17 @@ def test_insert_index_period(self, insert, coerced_val, coerced_dtype):
422
420
index_type = pd .Index
423
421
424
422
exp = index_type ([pd .Period ('2011-01' , freq = 'M' ),
425
- coerced_val ,
426
- pd .Period ('2011-02' , freq = 'M' ),
427
- pd .Period ('2011-03' , freq = 'M' ),
428
- pd .Period ('2011-04' , freq = 'M' )], freq = 'M' )
423
+ coerced_val ,
424
+ pd .Period ('2011-02' , freq = 'M' ),
425
+ pd .Period ('2011-03' , freq = 'M' ),
426
+ pd .Period ('2011-04' , freq = 'M' )], freq = 'M' )
429
427
self ._assert_insert_conversion (obj , insert , exp , coerced_dtype )
430
428
431
429
def test_insert_index_complex128 (self ):
432
430
pass
433
431
434
432
def test_insert_index_bool (self ):
435
- pass
433
+ pass
436
434
437
435
438
436
class TestWhereCoercion (CoercionBase ):
@@ -453,7 +451,7 @@ def _assert_where_conversion(self, original, cond, values,
453
451
(1.1 , np .object ),
454
452
(1 + 1j , np .object ),
455
453
(True , np .object )])
456
- def test_where_object (self , klass , fill_val ,exp_dtype ):
454
+ def test_where_object (self , klass , fill_val , exp_dtype ):
457
455
obj = klass (list ('abcd' ))
458
456
assert obj .dtype == np .object
459
457
cond = klass ([True , False , True , False ])
@@ -469,8 +467,8 @@ def test_where_object(self, klass, fill_val,exp_dtype):
469
467
if fill_val is True :
470
468
values = klass ([True , False , True , True ])
471
469
else :
472
- values = klass (fill_val * x for x in [5 , 6 , 7 , 8 ])
473
-
470
+ values = klass (fill_val * x for x in [5 , 6 , 7 , 8 ])
471
+
474
472
exp = klass (['a' , values [1 ], 'c' , values [3 ]])
475
473
self ._assert_where_conversion (obj , cond , values , exp , exp_dtype )
476
474
@@ -494,7 +492,7 @@ def test_where_int64(self, klass, fill_val, exp_dtype):
494
492
if fill_val is True :
495
493
values = klass ([True , False , True , True ])
496
494
else :
497
- values = klass (x * fill_val for x in [5 , 6 , 7 , 8 ])
495
+ values = klass (x * fill_val for x in [5 , 6 , 7 , 8 ])
498
496
exp = klass ([1 , values [1 ], 3 , values [3 ]])
499
497
self ._assert_where_conversion (obj , cond , values , exp , exp_dtype )
500
498
@@ -507,7 +505,7 @@ def test_where_int64(self, klass, fill_val, exp_dtype):
507
505
(True , np .object )])
508
506
def test_where_float64 (self , klass , fill_val , exp_dtype ):
509
507
if klass is pd .Index and exp_dtype is np .complex128 :
510
- pytest .skip ("Complex Index not supported" )
508
+ pytest .skip ("Complex Index not supported" )
511
509
obj = klass ([1.1 , 2.2 , 3.3 , 4.4 ])
512
510
assert obj .dtype == np .float64
513
511
cond = klass ([True , False , True , False ])
@@ -518,7 +516,7 @@ def test_where_float64(self, klass, fill_val, exp_dtype):
518
516
if fill_val is True :
519
517
values = klass ([True , False , True , True ])
520
518
else :
521
- values = klass (x * fill_val for x in [5 , 6 , 7 , 8 ])
519
+ values = klass (x * fill_val for x in [5 , 6 , 7 , 8 ])
522
520
exp = klass ([1.1 , values [1 ], 3.3 , values [3 ]])
523
521
self ._assert_where_conversion (obj , cond , values , exp , exp_dtype )
524
522
@@ -532,14 +530,13 @@ def test_where_series_complex128(self, fill_val, exp_dtype):
532
530
assert obj .dtype == np .complex128
533
531
cond = pd .Series ([True , False , True , False ])
534
532
535
- # complex + int -> complex
536
533
exp = pd .Series ([1 + 1j , fill_val , 3 + 3j , fill_val ])
537
534
self ._assert_where_conversion (obj , cond , fill_val , exp , exp_dtype )
538
535
539
536
if fill_val is True :
540
537
values = pd .Series ([True , False , True , True ])
541
538
else :
542
- values = pd .Series (x * fill_val for x in [5 , 6 , 7 , 8 ])
539
+ values = pd .Series (x * fill_val for x in [5 , 6 , 7 , 8 ])
543
540
exp = pd .Series ([1 + 1j , values [1 ], 3 + 3j , values [3 ]])
544
541
self ._assert_where_conversion (obj , cond , values , exp , exp_dtype )
545
542
@@ -560,14 +557,14 @@ def test_where_series_bool(self, fill_val, exp_dtype):
560
557
if fill_val is True :
561
558
values = pd .Series ([True , False , True , True ])
562
559
else :
563
- values = pd .Series (x * fill_val for x in [5 , 6 , 7 , 8 ])
560
+ values = pd .Series (x * fill_val for x in [5 , 6 , 7 , 8 ])
564
561
exp = pd .Series ([True , values [1 ], True , values [3 ]])
565
562
self ._assert_where_conversion (obj , cond , values , exp , exp_dtype )
566
563
567
564
@pytest .mark .parametrize ("fill_val,exp_dtype" , [
568
565
(pd .Timestamp ('2012-01-01' ), 'datetime64[ns]' ),
569
566
(pd .Timestamp ('2012-01-01' , tz = 'US/Eastern' ), np .object )],
570
- ids = ['datetime64' , 'datetime64tz' ])
567
+ ids = ['datetime64' , 'datetime64tz' ])
571
568
def test_where_series_datetime64 (self , fill_val , exp_dtype ):
572
569
obj = pd .Series ([pd .Timestamp ('2011-01-01' ),
573
570
pd .Timestamp ('2011-01-02' ),
@@ -586,7 +583,8 @@ def test_where_series_datetime64(self, fill_val, exp_dtype):
586
583
pd .Timestamp ('2012-01-02 05:00' ),
587
584
pd .Timestamp ('2011-01-03' ),
588
585
pd .Timestamp ('2012-01-04 05:00' )])
589
- self ._assert_where_conversion (obj , cond , values , exp , 'datetime64[ns]' )
586
+ self ._assert_where_conversion (obj , cond , values , exp ,
587
+ 'datetime64[ns]' )
590
588
pytest .xfail ("ToDo: do not coerce to UTC, must be object" )
591
589
592
590
exp = pd .Series ([pd .Timestamp ('2011-01-01' ), values [1 ],
@@ -596,7 +594,7 @@ def test_where_series_datetime64(self, fill_val, exp_dtype):
596
594
@pytest .mark .parametrize ("fill_val,exp_dtype" , [
597
595
(pd .Timestamp ('2012-01-01' ), 'datetime64[ns]' ),
598
596
(pd .Timestamp ('2012-01-01' , tz = 'US/Eastern' ), np .object )],
599
- ids = ['datetime64' , 'datetime64tz' ])
597
+ ids = ['datetime64' , 'datetime64tz' ])
600
598
def test_where_index_datetime (self , fill_val , exp_dtype ):
601
599
obj = pd .Index ([pd .Timestamp ('2011-01-01' ),
602
600
pd .Timestamp ('2011-01-02' ),
@@ -617,10 +615,12 @@ def test_where_index_datetime(self, fill_val, exp_dtype):
617
615
pd .Timestamp ('2012-01-04' )])
618
616
619
617
if fill_val .tz :
620
- self ._assert_where_conversion (obj , cond , values , exp , 'datetime64[ns]' )
618
+ self ._assert_where_conversion (obj , cond , values , exp ,
619
+ 'datetime64[ns]' )
621
620
pytest .xfail ("ToDo: do not ignore timezone, must be object" )
622
621
self ._assert_where_conversion (obj , cond , values , exp , exp_dtype )
623
- pytest .xfail ("datetime64 + datetime64 -> datetime64 must support scalar" )
622
+ pytest .xfail ("datetime64 + datetime64 -> datetime64 must support"
623
+ " scalar" )
624
624
625
625
def test_where_index_complex128 (self ):
626
626
pass
@@ -644,7 +644,7 @@ def test_where_index_timedelta64(self):
644
644
pass
645
645
646
646
def test_where_index_period (self ):
647
- pass
647
+ pass
648
648
649
649
650
650
class TestFillnaSeriesCoercion (CoercionBase ):
@@ -714,8 +714,7 @@ def test_fillna_series_complex128(self, fill_val, fill_dtype):
714
714
(pd .Timestamp ('2012-01-01' ), 'datetime64[ns]' ),
715
715
(pd .Timestamp ('2012-01-01' , tz = 'US/Eastern' ), np .object ),
716
716
(1 , np .object ), ('x' , np .object )],
717
- ids = ['datetime64' , 'datetime64tz' ,
718
- 'object' , 'object' ])
717
+ ids = ['datetime64' , 'datetime64tz' , 'object' , 'object' ])
719
718
def test_fillna_datetime (self , klass , fill_val , fill_dtype ):
720
719
obj = klass ([pd .Timestamp ('2011-01-01' ),
721
720
pd .NaT ,
@@ -753,14 +752,12 @@ def test_fillna_datetime64tz(self, klass, fill_val, fill_dtype):
753
752
self ._assert_fillna_conversion (obj , fill_val , exp , fill_dtype )
754
753
755
754
def test_fillna_series_int64 (self ):
756
- # int can't hold NaN
757
755
pass
758
756
759
757
def test_fillna_index_int64 (self ):
760
758
pass
761
759
762
760
def test_fillna_series_bool (self ):
763
- # bool can't hold NaN
764
761
pass
765
762
766
763
def test_fillna_index_bool (self ):
@@ -777,7 +774,7 @@ def test_fillna_index_timedelta64(self):
777
774
778
775
def test_fillna_index_period (self ):
779
776
pass
780
-
777
+
781
778
782
779
class TestReplaceSeriesCoercion (CoercionBase ):
783
780
@@ -805,13 +802,13 @@ class TestReplaceSeriesCoercion(CoercionBase):
805
802
@pytest .mark .parametrize ('how' , ['dict' , 'series' ])
806
803
@pytest .mark .parametrize ('to_key' , [
807
804
'object' , 'int64' , 'float64' , 'complex128' , 'bool' , 'datetime64[ns]' ,
808
- 'datetime64[ns, UTC]' , 'datetime64[ns, US/Eastern]' , 'timedelta64[ns]' ],
809
- ids = ['object' , 'int64' , 'float64' , 'complex128' ,
810
- 'bool' , 'datetime64' , 'datetime64tz' , 'datetime64tz' ,
811
- 'timedelta64' ])
805
+ 'datetime64[ns, UTC]' , 'datetime64[ns, US/Eastern]' , 'timedelta64[ns]'
806
+ ], ids = ['object' , 'int64' , 'float64' , 'complex128' , 'bool' ,
807
+ 'datetime64' , 'datetime64tz' , 'datetime64tz' , 'timedelta64' ])
812
808
@pytest .mark .parametrize ('from_key' , [
813
809
'object' , 'int64' , 'float64' , 'complex128' , 'bool' , 'datetime64[ns]' ,
814
- 'datetime64[ns, UTC]' , 'datetime64[ns, US/Eastern]' , 'timedelta64[ns]' ])
810
+ 'datetime64[ns, UTC]' , 'datetime64[ns, US/Eastern]' , 'timedelta64[ns]' ]
811
+ )
815
812
def test_replace_series (self , how , to_key , from_key ):
816
813
if from_key == 'bool' and how == 'series' and compat .PY3 :
817
814
# doesn't work in PY3, though ...dict_from_bool works fine
@@ -822,8 +819,8 @@ def test_replace_series(self, how, to_key, from_key):
822
819
assert obj .dtype == from_key
823
820
824
821
if (from_key .startswith ('datetime' ) and to_key .startswith ('datetime' )):
825
- # different tz, currently mask_missing raises SystemError
826
- return
822
+ pytest . xfail ( " different tz, currently mask_missing "
823
+ "raises SystemError" )
827
824
828
825
if how == 'dict' :
829
826
replacer = dict (zip (self .rep [from_key ], self .rep [to_key ]))
0 commit comments