20
20
21
21
import pandas .util .testing as tm
22
22
23
- from pandas .tests .frame .common import TestData
24
23
24
+ class TestDataFrameAlterAxes ():
25
25
26
- class TestDataFrameAlterAxes (TestData ):
27
-
28
- def test_set_index_directly (self , mixed_frame ):
29
- df = mixed_frame
26
+ def test_set_index_directly (self , float_string_frame ):
27
+ df = float_string_frame
30
28
idx = Index (np .arange (len (df ))[::- 1 ])
31
29
32
30
df .index = idx
33
31
tm .assert_index_equal (df .index , idx )
34
32
with tm .assert_raises_regex (ValueError , 'Length mismatch' ):
35
33
df .index = idx [::2 ]
36
34
37
- def test_set_index (self , mixed_frame ):
38
- df = mixed_frame
35
+ def test_set_index (self , float_string_frame ):
36
+ df = float_string_frame
39
37
idx = Index (np .arange (len (df ))[::- 1 ])
40
38
41
39
df = df .set_index (idx )
@@ -187,20 +185,20 @@ def test_set_index_pass_arrays_duplicate(self, frame_of_index_cols, drop,
187
185
keys = [box1 (df ['A' ]), box2 (df ['A' ])]
188
186
189
187
# == gives ambiguous Boolean for Series
190
- if keys [0 ] is 'A' and keys [1 ] is 'A' :
191
- with tm .assert_raises_regex (ValueError ,
192
- 'Passed duplicate column names.*' ):
193
- df .set_index (keys , drop = drop , append = append )
188
+ if drop and keys [0 ] is 'A' and keys [1 ] is 'A' :
189
+ # can't drop same column twice
190
+ first_drop = False
194
191
else :
195
- result = df . set_index ( keys , drop = drop , append = append )
192
+ first_drop = drop
196
193
197
- # to test against already-tested behavior , we add sequentially,
198
- # hence second append always True; must wrap in list, otherwise
199
- # list-box will be illegal
200
- expected = df .set_index ([keys [0 ]], drop = drop , append = append )
201
- expected = expected .set_index ([keys [1 ]], drop = drop , append = True )
194
+ # to test against already-tested behaviour , we add sequentially,
195
+ # hence second append always True; must wrap in list, otherwise
196
+ # list-box will be illegal
197
+ expected = df .set_index ([keys [0 ]], drop = first_drop , append = append )
198
+ expected = expected .set_index ([keys [1 ]], drop = drop , append = True )
202
199
203
- tm .assert_frame_equal (result , expected )
200
+ result = df .set_index (keys , drop = drop , append = append )
201
+ tm .assert_frame_equal (result , expected )
204
202
205
203
@pytest .mark .parametrize ('append' , [True , False ])
206
204
@pytest .mark .parametrize ('drop' , [True , False ])
@@ -227,15 +225,28 @@ def test_set_index_verify_integrity(self, frame_of_index_cols):
227
225
'Index has duplicate keys' ):
228
226
df .set_index ([df ['A' ], df ['A' ]], verify_integrity = True )
229
227
230
- def test_set_index_raise (self , frame_of_index_cols ):
228
+ @pytest .mark .parametrize ('append' , [True , False ])
229
+ @pytest .mark .parametrize ('drop' , [True , False ])
230
+ def test_set_index_raise (self , frame_of_index_cols , drop , append ):
231
231
df = frame_of_index_cols
232
232
233
- with tm .assert_raises_regex (KeyError , '.*' ): # column names are A-E
234
- df .set_index (['foo' , 'bar' , 'baz' ], verify_integrity = True )
233
+ with tm .assert_raises_regex (KeyError , "['foo', 'bar', 'baz']" ):
234
+ # column names are A-E
235
+ df .set_index (['foo' , 'bar' , 'baz' ], drop = drop , append = append )
235
236
236
237
# non-existent key in list with arrays
237
- with tm .assert_raises_regex (KeyError , '.*' ):
238
- df .set_index ([df ['A' ], df ['B' ], 'X' ], verify_integrity = True )
238
+ with tm .assert_raises_regex (KeyError , 'X' ):
239
+ df .set_index ([df ['A' ], df ['B' ], 'X' ], drop = drop , append = append )
240
+
241
+ rgx = 'keys may only contain a combination of the following:.*'
242
+ # forbidden type, e.g. iterator
243
+ with tm .assert_raises_regex (TypeError , rgx ):
244
+ df .set_index (map (str , df ['A' ]), drop = drop , append = append )
245
+
246
+ # forbidden type in list, e.g. iterator
247
+ with tm .assert_raises_regex (TypeError , rgx ):
248
+ df .set_index (['A' , df ['A' ], map (str , df ['A' ])],
249
+ drop = drop , append = append )
239
250
240
251
def test_construction_with_categorical_index (self ):
241
252
ci = tm .makeCategoricalIndex (10 )
@@ -398,11 +409,11 @@ def test_set_index_empty_column(self):
398
409
names = ['a' , 'x' ])
399
410
tm .assert_frame_equal (result , expected )
400
411
401
- def test_set_columns (self , mixed_frame ):
402
- cols = Index (np .arange (len (mixed_frame .columns )))
403
- mixed_frame .columns = cols
412
+ def test_set_columns (self , float_string_frame ):
413
+ cols = Index (np .arange (len (float_string_frame .columns )))
414
+ float_string_frame .columns = cols
404
415
with tm .assert_raises_regex (ValueError , 'Length mismatch' ):
405
- mixed_frame .columns = cols [::2 ]
416
+ float_string_frame .columns = cols [::2 ]
406
417
407
418
def test_dti_set_index_reindex (self ):
408
419
# GH 6631
@@ -430,20 +441,20 @@ def test_dti_set_index_reindex(self):
430
441
431
442
# Renaming
432
443
433
- def test_rename (self , frame ):
444
+ def test_rename (self , float_frame ):
434
445
mapping = {
435
446
'A' : 'a' ,
436
447
'B' : 'b' ,
437
448
'C' : 'c' ,
438
449
'D' : 'd'
439
450
}
440
451
441
- renamed = frame .rename (columns = mapping )
442
- renamed2 = frame .rename (columns = str .lower )
452
+ renamed = float_frame .rename (columns = mapping )
453
+ renamed2 = float_frame .rename (columns = str .lower )
443
454
444
455
tm .assert_frame_equal (renamed , renamed2 )
445
456
tm .assert_frame_equal (renamed2 .rename (columns = str .upper ),
446
- frame , check_names = False )
457
+ float_frame , check_names = False )
447
458
448
459
# index
449
460
data = {
@@ -459,14 +470,14 @@ def test_rename(self, frame):
459
470
tm .assert_index_equal (renamed .index , Index (['BAR' , 'FOO' ]))
460
471
461
472
# have to pass something
462
- pytest .raises (TypeError , frame .rename )
473
+ pytest .raises (TypeError , float_frame .rename )
463
474
464
475
# partial columns
465
- renamed = frame .rename (columns = {'C' : 'foo' , 'D' : 'bar' })
476
+ renamed = float_frame .rename (columns = {'C' : 'foo' , 'D' : 'bar' })
466
477
tm .assert_index_equal (renamed .columns , Index (['A' , 'B' , 'foo' , 'bar' ]))
467
478
468
479
# other axis
469
- renamed = frame .T .rename (index = {'C' : 'foo' , 'D' : 'bar' })
480
+ renamed = float_frame .T .rename (index = {'C' : 'foo' , 'D' : 'bar' })
470
481
tm .assert_index_equal (renamed .index , Index (['A' , 'B' , 'foo' , 'bar' ]))
471
482
472
483
# index with name
@@ -477,17 +488,17 @@ def test_rename(self, frame):
477
488
Index (['bar' , 'foo' ], name = 'name' ))
478
489
assert renamed .index .name == renamer .index .name
479
490
480
- def test_rename_axis_inplace (self , frame ):
491
+ def test_rename_axis_inplace (self , float_frame ):
481
492
# GH 15704
482
- expected = frame .rename_axis ('foo' )
483
- result = frame .copy ()
493
+ expected = float_frame .rename_axis ('foo' )
494
+ result = float_frame .copy ()
484
495
no_return = result .rename_axis ('foo' , inplace = True )
485
496
486
497
assert no_return is None
487
498
tm .assert_frame_equal (result , expected )
488
499
489
- expected = frame .rename_axis ('bar' , axis = 1 )
490
- result = frame .copy ()
500
+ expected = float_frame .rename_axis ('bar' , axis = 1 )
501
+ result = float_frame .copy ()
491
502
no_return = result .rename_axis ('bar' , axis = 1 , inplace = True )
492
503
493
504
assert no_return is None
@@ -587,23 +598,23 @@ def test_rename_multiindex(self):
587
598
level = 0 )
588
599
tm .assert_index_equal (renamed .index , new_index )
589
600
590
- def test_rename_nocopy (self , frame ):
591
- renamed = frame .rename (columns = {'C' : 'foo' }, copy = False )
601
+ def test_rename_nocopy (self , float_frame ):
602
+ renamed = float_frame .rename (columns = {'C' : 'foo' }, copy = False )
592
603
renamed ['foo' ] = 1.
593
- assert (frame ['C' ] == 1. ).all ()
604
+ assert (float_frame ['C' ] == 1. ).all ()
594
605
595
- def test_rename_inplace (self , frame ):
596
- frame .rename (columns = {'C' : 'foo' })
597
- assert 'C' in frame
598
- assert 'foo' not in frame
606
+ def test_rename_inplace (self , float_frame ):
607
+ float_frame .rename (columns = {'C' : 'foo' })
608
+ assert 'C' in float_frame
609
+ assert 'foo' not in float_frame
599
610
600
- c_id = id (frame ['C' ])
601
- frame = frame .copy ()
602
- frame .rename (columns = {'C' : 'foo' }, inplace = True )
611
+ c_id = id (float_frame ['C' ])
612
+ float_frame = float_frame .copy ()
613
+ float_frame .rename (columns = {'C' : 'foo' }, inplace = True )
603
614
604
- assert 'C' not in frame
605
- assert 'foo' in frame
606
- assert id (frame ['foo' ]) != c_id
615
+ assert 'C' not in float_frame
616
+ assert 'foo' in float_frame
617
+ assert id (float_frame ['foo' ]) != c_id
607
618
608
619
def test_rename_bug (self ):
609
620
# GH 5344
@@ -671,8 +682,8 @@ def test_reorder_levels(self):
671
682
result = df .reorder_levels (['L0' , 'L0' , 'L0' ])
672
683
tm .assert_frame_equal (result , expected )
673
684
674
- def test_reset_index (self , frame ):
675
- stacked = frame .stack ()[::2 ]
685
+ def test_reset_index (self , float_frame ):
686
+ stacked = float_frame .stack ()[::2 ]
676
687
stacked = DataFrame ({'foo' : stacked , 'bar' : stacked })
677
688
678
689
names = ['first' , 'second' ]
@@ -692,55 +703,55 @@ def test_reset_index(self, frame):
692
703
check_names = False )
693
704
694
705
# default name assigned
695
- rdf = frame .reset_index ()
696
- exp = Series (frame .index .values , name = 'index' )
706
+ rdf = float_frame .reset_index ()
707
+ exp = Series (float_frame .index .values , name = 'index' )
697
708
tm .assert_series_equal (rdf ['index' ], exp )
698
709
699
710
# default name assigned, corner case
700
- df = frame .copy ()
711
+ df = float_frame .copy ()
701
712
df ['index' ] = 'foo'
702
713
rdf = df .reset_index ()
703
- exp = Series (frame .index .values , name = 'level_0' )
714
+ exp = Series (float_frame .index .values , name = 'level_0' )
704
715
tm .assert_series_equal (rdf ['level_0' ], exp )
705
716
706
717
# but this is ok
707
- frame .index .name = 'index'
708
- deleveled = frame .reset_index ()
709
- tm .assert_series_equal (deleveled ['index' ], Series (frame .index ))
718
+ float_frame .index .name = 'index'
719
+ deleveled = float_frame .reset_index ()
720
+ tm .assert_series_equal (deleveled ['index' ], Series (float_frame .index ))
710
721
tm .assert_index_equal (deleveled .index ,
711
722
Index (np .arange (len (deleveled ))))
712
723
713
724
# preserve column names
714
- frame .columns .name = 'columns'
715
- resetted = frame .reset_index ()
725
+ float_frame .columns .name = 'columns'
726
+ resetted = float_frame .reset_index ()
716
727
assert resetted .columns .name == 'columns'
717
728
718
729
# only remove certain columns
719
- df = frame .reset_index ().set_index (['index' , 'A' , 'B' ])
730
+ df = float_frame .reset_index ().set_index (['index' , 'A' , 'B' ])
720
731
rs = df .reset_index (['A' , 'B' ])
721
732
722
733
# TODO should reset_index check_names ?
723
- tm .assert_frame_equal (rs , frame , check_names = False )
734
+ tm .assert_frame_equal (rs , float_frame , check_names = False )
724
735
725
736
rs = df .reset_index (['index' , 'A' , 'B' ])
726
- tm .assert_frame_equal (rs , frame .reset_index (), check_names = False )
737
+ tm .assert_frame_equal (rs , float_frame .reset_index (), check_names = False )
727
738
728
739
rs = df .reset_index (['index' , 'A' , 'B' ])
729
- tm .assert_frame_equal (rs , frame .reset_index (), check_names = False )
740
+ tm .assert_frame_equal (rs , float_frame .reset_index (), check_names = False )
730
741
731
742
rs = df .reset_index ('A' )
732
- xp = frame .reset_index ().set_index (['index' , 'B' ])
743
+ xp = float_frame .reset_index ().set_index (['index' , 'B' ])
733
744
tm .assert_frame_equal (rs , xp , check_names = False )
734
745
735
746
# test resetting in place
736
- df = frame .copy ()
737
- resetted = frame .reset_index ()
747
+ df = float_frame .copy ()
748
+ resetted = float_frame .reset_index ()
738
749
df .reset_index (inplace = True )
739
750
tm .assert_frame_equal (df , resetted , check_names = False )
740
751
741
- df = frame .reset_index ().set_index (['index' , 'A' , 'B' ])
752
+ df = float_frame .reset_index ().set_index (['index' , 'A' , 'B' ])
742
753
rs = df .reset_index ('A' , drop = True )
743
- xp = frame .copy ()
754
+ xp = float_frame .copy ()
744
755
del xp ['A' ]
745
756
xp = xp .set_index (['B' ], append = True )
746
757
tm .assert_frame_equal (rs , xp , check_names = False )
@@ -918,8 +929,8 @@ def test_set_index_names(self):
918
929
# Check equality
919
930
tm .assert_index_equal (df .set_index ([df .index , idx2 ]).index , mi2 )
920
931
921
- def test_rename_objects (self , mixed_frame ):
922
- renamed = mixed_frame .rename (columns = str .upper )
932
+ def test_rename_objects (self , float_string_frame ):
933
+ renamed = float_string_frame .rename (columns = str .upper )
923
934
924
935
assert 'FOO' in renamed
925
936
assert 'foo' not in renamed
@@ -1042,13 +1053,14 @@ def test_rename_positional(self):
1042
1053
assert 'rename' in message
1043
1054
assert 'Use named arguments' in message
1044
1055
1045
- def test_assign_columns (self , frame ):
1046
- frame ['hi' ] = 'there'
1056
+ def test_assign_columns (self , float_frame ):
1057
+ float_frame ['hi' ] = 'there'
1047
1058
1048
- df = frame .copy ()
1059
+ df = float_frame .copy ()
1049
1060
df .columns = ['foo' , 'bar' , 'baz' , 'quux' , 'foo2' ]
1050
- tm .assert_series_equal (frame ['C' ], df ['baz' ], check_names = False )
1051
- tm .assert_series_equal (frame ['hi' ], df ['foo2' ], check_names = False )
1061
+ tm .assert_series_equal (float_frame ['C' ], df ['baz' ], check_names = False )
1062
+ tm .assert_series_equal (float_frame ['hi' ], df ['foo2' ],
1063
+ check_names = False )
1052
1064
1053
1065
def test_set_index_preserve_categorical_dtype (self ):
1054
1066
# GH13743, GH13854
0 commit comments