2
2
3
3
from __future__ import print_function
4
4
5
+ import pytest
6
+
5
7
from datetime import datetime
6
8
7
9
from numpy import random
@@ -409,33 +411,35 @@ def test_reindex_dups(self):
409
411
410
412
def test_align (self ):
411
413
af , bf = self .frame .align (self .frame )
412
- self . assertIsNot ( af ._data , self .frame ._data )
414
+ assert af ._data is not self .frame ._data
413
415
414
416
af , bf = self .frame .align (self .frame , copy = False )
415
- self . assertIs ( af ._data , self .frame ._data )
417
+ assert af ._data is self .frame ._data
416
418
417
419
# axis = 0
418
420
other = self .frame .iloc [:- 5 , :3 ]
419
421
af , bf = self .frame .align (other , axis = 0 , fill_value = - 1 )
420
- self .assert_index_equal (bf .columns , other .columns )
422
+
423
+ tm .assert_index_equal (bf .columns , other .columns )
424
+
421
425
# test fill value
422
426
join_idx = self .frame .index .join (other .index )
423
427
diff_a = self .frame .index .difference (join_idx )
424
428
diff_b = other .index .difference (join_idx )
425
429
diff_a_vals = af .reindex (diff_a ).values
426
430
diff_b_vals = bf .reindex (diff_b ).values
427
- self . assertTrue (( diff_a_vals == - 1 ).all () )
431
+ assert ( diff_a_vals == - 1 ).all ()
428
432
429
433
af , bf = self .frame .align (other , join = 'right' , axis = 0 )
430
- self .assert_index_equal (bf .columns , other .columns )
431
- self .assert_index_equal (bf .index , other .index )
432
- self .assert_index_equal (af .index , other .index )
434
+ tm .assert_index_equal (bf .columns , other .columns )
435
+ tm .assert_index_equal (bf .index , other .index )
436
+ tm .assert_index_equal (af .index , other .index )
433
437
434
438
# axis = 1
435
439
other = self .frame .iloc [:- 5 , :3 ].copy ()
436
440
af , bf = self .frame .align (other , axis = 1 )
437
- self .assert_index_equal (bf .columns , self .frame .columns )
438
- self .assert_index_equal (bf .index , other .index )
441
+ tm .assert_index_equal (bf .columns , self .frame .columns )
442
+ tm .assert_index_equal (bf .index , other .index )
439
443
440
444
# test fill value
441
445
join_idx = self .frame .index .join (other .index )
@@ -446,42 +450,42 @@ def test_align(self):
446
450
# TODO(wesm): unused?
447
451
diff_b_vals = bf .reindex (diff_b ).values # noqa
448
452
449
- self . assertTrue (( diff_a_vals == - 1 ).all () )
453
+ assert ( diff_a_vals == - 1 ).all ()
450
454
451
455
af , bf = self .frame .align (other , join = 'inner' , axis = 1 )
452
- self .assert_index_equal (bf .columns , other .columns )
456
+ tm .assert_index_equal (bf .columns , other .columns )
453
457
454
458
af , bf = self .frame .align (other , join = 'inner' , axis = 1 , method = 'pad' )
455
- self .assert_index_equal (bf .columns , other .columns )
459
+ tm .assert_index_equal (bf .columns , other .columns )
456
460
457
461
# test other non-float types
458
462
af , bf = self .intframe .align (other , join = 'inner' , axis = 1 , method = 'pad' )
459
- self .assert_index_equal (bf .columns , other .columns )
463
+ tm .assert_index_equal (bf .columns , other .columns )
460
464
461
465
af , bf = self .mixed_frame .align (self .mixed_frame ,
462
466
join = 'inner' , axis = 1 , method = 'pad' )
463
- self .assert_index_equal (bf .columns , self .mixed_frame .columns )
467
+ tm .assert_index_equal (bf .columns , self .mixed_frame .columns )
464
468
465
469
af , bf = self .frame .align (other .iloc [:, 0 ], join = 'inner' , axis = 1 ,
466
470
method = None , fill_value = None )
467
- self .assert_index_equal (bf .index , Index ([]))
471
+ tm .assert_index_equal (bf .index , Index ([]))
468
472
469
473
af , bf = self .frame .align (other .iloc [:, 0 ], join = 'inner' , axis = 1 ,
470
474
method = None , fill_value = 0 )
471
- self .assert_index_equal (bf .index , Index ([]))
475
+ tm .assert_index_equal (bf .index , Index ([]))
472
476
473
477
# mixed floats/ints
474
478
af , bf = self .mixed_float .align (other .iloc [:, 0 ], join = 'inner' , axis = 1 ,
475
479
method = None , fill_value = 0 )
476
- self .assert_index_equal (bf .index , Index ([]))
480
+ tm .assert_index_equal (bf .index , Index ([]))
477
481
478
482
af , bf = self .mixed_int .align (other .iloc [:, 0 ], join = 'inner' , axis = 1 ,
479
483
method = None , fill_value = 0 )
480
- self .assert_index_equal (bf .index , Index ([]))
484
+ tm .assert_index_equal (bf .index , Index ([]))
481
485
482
- # try to align dataframe to series along bad axis
483
- self . assertRaises (ValueError , self . frame . align , af . iloc [ 0 , : 3 ],
484
- join = 'inner' , axis = 2 )
486
+ # Try to align DataFrame to Series along bad axis
487
+ with pytest . raises (ValueError ):
488
+ self . frame . align ( af . iloc [ 0 , : 3 ], join = 'inner' , axis = 2 )
485
489
486
490
# align dataframe to series with broadcast or not
487
491
idx = self .frame .index
@@ -490,7 +494,7 @@ def test_align(self):
490
494
left , right = self .frame .align (s , axis = 0 )
491
495
tm .assert_index_equal (left .index , self .frame .index )
492
496
tm .assert_index_equal (right .index , self .frame .index )
493
- self . assertTrue ( isinstance (right , Series ) )
497
+ assert isinstance (right , Series )
494
498
495
499
left , right = self .frame .align (s , broadcast_axis = 1 )
496
500
tm .assert_index_equal (left .index , self .frame .index )
@@ -499,17 +503,17 @@ def test_align(self):
499
503
expected [c ] = s
500
504
expected = DataFrame (expected , index = self .frame .index ,
501
505
columns = self .frame .columns )
502
- assert_frame_equal (right , expected )
506
+ tm . assert_frame_equal (right , expected )
503
507
504
- # GH 9558
508
+ # see gh- 9558
505
509
df = DataFrame ({'a' : [1 , 2 , 3 ], 'b' : [4 , 5 , 6 ]})
506
510
result = df [df ['a' ] == 2 ]
507
511
expected = DataFrame ([[2 , 5 ]], index = [1 ], columns = ['a' , 'b' ])
508
- assert_frame_equal (result , expected )
512
+ tm . assert_frame_equal (result , expected )
509
513
510
514
result = df .where (df ['a' ] == 2 , 0 )
511
515
expected = DataFrame ({'a' : [0 , 2 , 0 ], 'b' : [0 , 5 , 0 ]})
512
- assert_frame_equal (result , expected )
516
+ tm . assert_frame_equal (result , expected )
513
517
514
518
def _check_align (self , a , b , axis , fill_axis , how , method , limit = None ):
515
519
aa , ab = a .align (b , axis = axis , join = how , method = method , limit = limit ,
0 commit comments