10
10
11
11
import pandas as pd
12
12
import pandas .core .common as com
13
+ from pandas import option_context
13
14
from pandas .core .api import (DataFrame , Index , Series , Panel , isnull ,
14
15
MultiIndex , Float64Index , Timestamp )
15
16
from pandas .util .testing import (assert_almost_equal , assert_series_equal ,
@@ -2320,10 +2321,12 @@ def test_ix_assign_column_mixed(self):
2320
2321
assert_frame_equal (df ,expected )
2321
2322
2322
2323
# ok, but chained assignments are dangerous
2323
- df = pd .DataFrame ({'a' : lrange (4 ) })
2324
- df ['b' ] = np .nan
2325
- df ['b' ].ix [[1 ,3 ]] = [100 ,- 100 ]
2326
- assert_frame_equal (df ,expected )
2324
+ # if we turn off chained assignement it will work
2325
+ with option_context ('chained_assignment' ,None ):
2326
+ df = pd .DataFrame ({'a' : lrange (4 ) })
2327
+ df ['b' ] = np .nan
2328
+ df ['b' ].ix [[1 ,3 ]] = [100 ,- 100 ]
2329
+ assert_frame_equal (df ,expected )
2327
2330
2328
2331
def test_ix_get_set_consistency (self ):
2329
2332
@@ -3036,22 +3039,26 @@ def test_cache_updating(self):
3036
3039
self .assertEqual (result , 2 )
3037
3040
3038
3041
def test_slice_consolidate_invalidate_item_cache (self ):
3039
- # #3970
3040
- df = DataFrame ({ "aa" :lrange (5 ), "bb" :[2.2 ]* 5 })
3041
3042
3042
- # Creates a second float block
3043
- df ["cc" ] = 0.0
3043
+ # this is chained assignment, but will 'work'
3044
+ with option_context ('chained_assignment' ,None ):
3045
+
3046
+ # #3970
3047
+ df = DataFrame ({ "aa" :lrange (5 ), "bb" :[2.2 ]* 5 })
3044
3048
3045
- # caches a reference to the 'bb' series
3046
- df ["bb" ]
3049
+ # Creates a second float block
3050
+ df ["cc" ] = 0.0
3047
3051
3048
- # repr machinery triggers consolidation
3049
- repr ( df )
3052
+ # caches a reference to the 'bb' series
3053
+ df [ "bb" ]
3050
3054
3051
- # Assignment to wrong series
3052
- df ['bb' ].iloc [0 ] = 0.17
3053
- df ._clear_item_cache ()
3054
- self .assertAlmostEqual (df ['bb' ][0 ], 0.17 )
3055
+ # repr machinery triggers consolidation
3056
+ repr (df )
3057
+
3058
+ # Assignment to wrong series
3059
+ df ['bb' ].iloc [0 ] = 0.17
3060
+ df ._clear_item_cache ()
3061
+ self .assertAlmostEqual (df ['bb' ][0 ], 0.17 )
3055
3062
3056
3063
def test_setitem_cache_updating (self ):
3057
3064
# GH 5424
@@ -3135,17 +3142,19 @@ def test_detect_chained_assignment(self):
3135
3142
expected = DataFrame ([[- 5 ,1 ],[- 6 ,3 ]],columns = list ('AB' ))
3136
3143
df = DataFrame (np .arange (4 ).reshape (2 ,2 ),columns = list ('AB' ),dtype = 'int64' )
3137
3144
self .assertIsNone (df .is_copy )
3138
-
3139
3145
df ['A' ][0 ] = - 5
3140
3146
df ['A' ][1 ] = - 6
3141
3147
assert_frame_equal (df , expected )
3142
3148
3143
- expected = DataFrame ([[ - 5 , 2 ],[ np . nan , 3. ]], columns = list ( 'AB' ))
3149
+ # test with the chaining
3144
3150
df = DataFrame ({ 'A' : Series (range (2 ),dtype = 'int64' ), 'B' : np .array (np .arange (2 ,4 ),dtype = np .float64 )})
3145
3151
self .assertIsNone (df .is_copy )
3146
- df ['A' ][0 ] = - 5
3147
- df ['A' ][1 ] = np .nan
3148
- assert_frame_equal (df , expected )
3152
+ def f ():
3153
+ df ['A' ][0 ] = - 5
3154
+ self .assertRaises (com .SettingWithCopyError , f )
3155
+ def f ():
3156
+ df ['A' ][1 ] = np .nan
3157
+ self .assertRaises (com .SettingWithCopyError , f )
3149
3158
self .assertIsNone (df ['A' ].is_copy )
3150
3159
3151
3160
# using a copy (the chain), fails
@@ -3172,18 +3181,14 @@ def f():
3172
3181
3173
3182
expected = DataFrame ({'A' :[111 ,'bbb' ,'ccc' ],'B' :[1 ,2 ,3 ]})
3174
3183
df = DataFrame ({'A' :['aaa' ,'bbb' ,'ccc' ],'B' :[1 ,2 ,3 ]})
3175
- df ['A' ][0 ] = 111
3184
+ def f ():
3185
+ df ['A' ][0 ] = 111
3186
+ self .assertRaises (com .SettingWithCopyError , f )
3176
3187
def f ():
3177
3188
df .loc [0 ]['A' ] = 111
3178
3189
self .assertRaises (com .SettingWithCopyError , f )
3179
3190
assert_frame_equal (df ,expected )
3180
3191
3181
- # warnings
3182
- pd .set_option ('chained_assignment' ,'warn' )
3183
- df = DataFrame ({'A' :['aaa' ,'bbb' ,'ccc' ],'B' :[1 ,2 ,3 ]})
3184
- with tm .assert_produces_warning (expected_warning = com .SettingWithCopyWarning ):
3185
- df .loc [0 ]['A' ] = 111
3186
-
3187
3192
# make sure that is_copy is picked up reconstruction
3188
3193
# GH5475
3189
3194
df = DataFrame ({"A" : [1 ,2 ]})
@@ -3196,7 +3201,6 @@ def f():
3196
3201
3197
3202
# a suprious raise as we are setting the entire column here
3198
3203
# GH5597
3199
- pd .set_option ('chained_assignment' ,'raise' )
3200
3204
from string import ascii_letters as letters
3201
3205
3202
3206
def random_text (nobs = 100 ):
@@ -3295,6 +3299,28 @@ def f():
3295
3299
df .iloc [0 :5 ]['group' ] = 'a'
3296
3300
self .assertRaises (com .SettingWithCopyError , f )
3297
3301
3302
+ # mixed type setting
3303
+ # same dtype & changing dtype
3304
+ df = DataFrame (dict (A = date_range ('20130101' ,periods = 5 ),B = np .random .randn (5 ),C = np .arange (5 ,dtype = 'int64' ),D = list ('abcde' )))
3305
+
3306
+ def f ():
3307
+ df .ix [2 ]['D' ] = 'foo'
3308
+ self .assertRaises (com .SettingWithCopyError , f )
3309
+ def f ():
3310
+ df .ix [2 ]['C' ] = 'foo'
3311
+ self .assertRaises (com .SettingWithCopyError , f )
3312
+ def f ():
3313
+ df ['C' ][2 ] = 'foo'
3314
+ self .assertRaises (com .SettingWithCopyError , f )
3315
+
3316
+ def test_detect_chained_assignment_warnings (self ):
3317
+
3318
+ # warnings
3319
+ with option_context ('chained_assignment' ,'warn' ):
3320
+ df = DataFrame ({'A' :['aaa' ,'bbb' ,'ccc' ],'B' :[1 ,2 ,3 ]})
3321
+ with tm .assert_produces_warning (expected_warning = com .SettingWithCopyWarning ):
3322
+ df .loc [0 ]['A' ] = 111
3323
+
3298
3324
def test_float64index_slicing_bug (self ):
3299
3325
# GH 5557, related to slicing a float index
3300
3326
ser = {256 : 2321.0 , 1 : 78.0 , 2 : 2716.0 , 3 : 0.0 , 4 : 369.0 , 5 : 0.0 , 6 : 269.0 , 7 : 0.0 , 8 : 0.0 , 9 : 0.0 , 10 : 3536.0 , 11 : 0.0 , 12 : 24.0 , 13 : 0.0 , 14 : 931.0 , 15 : 0.0 , 16 : 101.0 , 17 : 78.0 , 18 : 9643.0 , 19 : 0.0 , 20 : 0.0 , 21 : 0.0 , 22 : 63761.0 , 23 : 0.0 , 24 : 446.0 , 25 : 0.0 , 26 : 34773.0 , 27 : 0.0 , 28 : 729.0 , 29 : 78.0 , 30 : 0.0 , 31 : 0.0 , 32 : 3374.0 , 33 : 0.0 , 34 : 1391.0 , 35 : 0.0 , 36 : 361.0 , 37 : 0.0 , 38 : 61808.0 , 39 : 0.0 , 40 : 0.0 , 41 : 0.0 , 42 : 6677.0 , 43 : 0.0 , 44 : 802.0 , 45 : 0.0 , 46 : 2691.0 , 47 : 0.0 , 48 : 3582.0 , 49 : 0.0 , 50 : 734.0 , 51 : 0.0 , 52 : 627.0 , 53 : 70.0 , 54 : 2584.0 , 55 : 0.0 , 56 : 324.0 , 57 : 0.0 , 58 : 605.0 , 59 : 0.0 , 60 : 0.0 , 61 : 0.0 , 62 : 3989.0 , 63 : 10.0 , 64 : 42.0 , 65 : 0.0 , 66 : 904.0 , 67 : 0.0 , 68 : 88.0 , 69 : 70.0 , 70 : 8172.0 , 71 : 0.0 , 72 : 0.0 , 73 : 0.0 , 74 : 64902.0 , 75 : 0.0 , 76 : 347.0 , 77 : 0.0 , 78 : 36605.0 , 79 : 0.0 , 80 : 379.0 , 81 : 70.0 , 82 : 0.0 , 83 : 0.0 , 84 : 3001.0 , 85 : 0.0 , 86 : 1630.0 , 87 : 7.0 , 88 : 364.0 , 89 : 0.0 , 90 : 67404.0 , 91 : 9.0 , 92 : 0.0 , 93 : 0.0 , 94 : 7685.0 , 95 : 0.0 , 96 : 1017.0 , 97 : 0.0 , 98 : 2831.0 , 99 : 0.0 , 100 : 2963.0 , 101 : 0.0 , 102 : 854.0 , 103 : 0.0 , 104 : 0.0 , 105 : 0.0 , 106 : 0.0 , 107 : 0.0 , 108 : 0.0 , 109 : 0.0 , 110 : 0.0 , 111 : 0.0 , 112 : 0.0 , 113 : 0.0 , 114 : 0.0 , 115 : 0.0 , 116 : 0.0 , 117 : 0.0 , 118 : 0.0 , 119 : 0.0 , 120 : 0.0 , 121 : 0.0 , 122 : 0.0 , 123 : 0.0 , 124 : 0.0 , 125 : 0.0 , 126 : 67744.0 , 127 : 22.0 , 128 : 264.0 , 129 : 0.0 , 260 : 197.0 , 268 : 0.0 , 265 : 0.0 , 269 : 0.0 , 261 : 0.0 , 266 : 1198.0 , 267 : 0.0 , 262 : 2629.0 , 258 : 775.0 , 257 : 0.0 , 263 : 0.0 , 259 : 0.0 , 264 : 163.0 , 250 : 10326.0 , 251 : 0.0 , 252 : 1228.0 , 253 : 0.0 , 254 : 2769.0 , 255 : 0.0 }
0 commit comments