@@ -184,8 +184,8 @@ def _check_set(df, cond, check_dtypes=True):
184184 econd = cond .reindex_like (df ).fillna (True ).infer_objects ()
185185 expected = dfi .mask (~ econd )
186186
187- return_value = dfi .where (cond , np .nan , inplace = True )
188- assert return_value is None
187+ result = dfi .where (cond , np .nan , inplace = True )
188+ assert result is dfi
189189 tm .assert_frame_equal (dfi , expected )
190190
191191 # dtypes (and confirm upcasts)x
@@ -320,24 +320,27 @@ def test_where_ndframe_align(self):
320320
321321 def test_where_bug (self ):
322322 # see gh-2793
323- df = DataFrame (
323+ df_orig = DataFrame (
324324 {"a" : [1.0 , 2.0 , 3.0 , 4.0 ], "b" : [4.0 , 3.0 , 2.0 , 1.0 ]}, dtype = "float64"
325325 )
326326 expected = DataFrame (
327327 {"a" : [np .nan , np .nan , 3.0 , 4.0 ], "b" : [4.0 , 3.0 , np .nan , np .nan ]},
328328 dtype = "float64" ,
329329 )
330+
331+ df = df_orig .copy ()
330332 result = df .where (df > 2 , np .nan )
331333 tm .assert_frame_equal (result , expected )
334+ tm .assert_frame_equal (df , df_orig )
332335
333- result = df .copy ()
334- return_value = result .where (result > 2 , np .nan , inplace = True )
335- assert return_value is None
336- tm .assert_frame_equal (result , expected )
336+ df = df_orig .copy ()
337+ result = df .where (df > 2 , np .nan , inplace = True )
338+ assert result is df
339+ tm .assert_frame_equal (df , expected )
337340
338341 def test_where_bug_mixed (self , any_signed_int_numpy_dtype ):
339342 # see gh-2793
340- df = DataFrame (
343+ df_orig = DataFrame (
341344 {
342345 "a" : np .array ([1 , 2 , 3 , 4 ], dtype = any_signed_int_numpy_dtype ),
343346 "b" : np .array ([4.0 , 3.0 , 2.0 , 1.0 ], dtype = "float64" ),
@@ -348,13 +351,15 @@ def test_where_bug_mixed(self, any_signed_int_numpy_dtype):
348351 {"a" : [- 1 , - 1 , 3 , 4 ], "b" : [4.0 , 3.0 , - 1 , - 1 ]},
349352 ).astype ({"a" : any_signed_int_numpy_dtype , "b" : "float64" })
350353
354+ df = df_orig .copy ()
351355 result = df .where (df > 2 , - 1 )
352356 tm .assert_frame_equal (result , expected )
357+ tm .assert_frame_equal (df , df_orig )
353358
354- result = df .copy ()
355- return_value = result .where (result > 2 , - 1 , inplace = True )
356- assert return_value is None
357- tm .assert_frame_equal (result , expected )
359+ df = df_orig .copy ()
360+ result = df .where (df > 2 , - 1 , inplace = True )
361+ assert result is df
362+ tm .assert_frame_equal (df , expected )
358363
359364 def test_where_bug_transposition (self ):
360365 # see gh-7506
@@ -461,8 +466,8 @@ def create():
461466 result = df .where (pd .notna (df ), df .mean (), axis = "columns" )
462467 tm .assert_frame_equal (result , expected )
463468
464- return_value = df .where (pd .notna (df ), df .mean (), inplace = True , axis = "columns" )
465- assert return_value is None
469+ result = df .where (pd .notna (df ), df .mean (), inplace = True , axis = "columns" )
470+ assert result is df
466471 tm .assert_frame_equal (df , expected )
467472
468473 df = create ().fillna (0 )
@@ -489,27 +494,30 @@ def test_where_complex(self):
489494
490495 def test_where_axis (self ):
491496 # GH 9736
492- df = DataFrame (np .random .default_rng (2 ).standard_normal ((2 , 2 )))
497+ df_orig = DataFrame (np .random .default_rng (2 ).standard_normal ((2 , 2 )))
493498 mask = DataFrame ([[False , False ], [False , False ]])
494499 ser = Series ([0 , 1 ])
495500
501+ df = df_orig .copy ()
496502 expected = DataFrame ([[0 , 0 ], [1 , 1 ]], dtype = "float64" )
497503 result = df .where (mask , ser , axis = "index" )
498504 tm .assert_frame_equal (result , expected )
499505
500- result = df .copy ()
501- return_value = result .where (mask , ser , axis = "index" , inplace = True )
502- assert return_value is None
506+ df = df_orig .copy ()
507+ result = df .where (mask , ser , axis = "index" , inplace = True )
508+ assert result is df
503509 tm .assert_frame_equal (result , expected )
504510
511+ df = df_orig .copy ()
505512 expected = DataFrame ([[0 , 1 ], [0 , 1 ]], dtype = "float64" )
506513 result = df .where (mask , ser , axis = "columns" )
507514 tm .assert_frame_equal (result , expected )
508515
516+ df = df_orig .copy ()
509517 result = df .copy ()
510- return_value = result .where (mask , ser , axis = "columns" , inplace = True )
511- assert return_value is None
512- tm .assert_frame_equal (result , expected )
518+ result = df .where (mask , ser , axis = "columns" , inplace = True )
519+ assert result is df
520+ tm .assert_frame_equal (df , expected )
513521
514522 def test_where_axis_with_upcast (self ):
515523 # Upcast needed
@@ -534,7 +542,7 @@ def test_where_axis_with_upcast(self):
534542
535543 def test_where_axis_multiple_dtypes (self ):
536544 # Multiple dtypes (=> multiple Blocks)
537- df = pd .concat (
545+ df_orig = pd .concat (
538546 [
539547 DataFrame (np .random .default_rng (2 ).standard_normal ((10 , 2 ))),
540548 DataFrame (
@@ -545,64 +553,68 @@ def test_where_axis_multiple_dtypes(self):
545553 ignore_index = True ,
546554 axis = 1 ,
547555 )
548- mask = DataFrame (False , columns = df .columns , index = df .index )
549- s1 = Series (1 , index = df .columns )
550- s2 = Series (2 , index = df .index )
556+ mask = DataFrame (False , columns = df_orig .columns , index = df_orig .index )
557+ s1 = Series (1 , index = df_orig .columns )
558+ s2 = Series (2 , index = df_orig .index )
551559
560+ df = df_orig .copy ()
552561 result = df .where (mask , s1 , axis = "columns" )
553- expected = DataFrame (1.0 , columns = df .columns , index = df .index )
562+ expected = DataFrame (1.0 , columns = df_orig .columns , index = df_orig .index )
554563 expected [2 ] = expected [2 ].astype ("int64" )
555564 expected [3 ] = expected [3 ].astype ("int64" )
556565 tm .assert_frame_equal (result , expected )
557566
558- result = df .copy ()
559- return_value = result .where (mask , s1 , axis = "columns" , inplace = True )
560- assert return_value is None
561- tm .assert_frame_equal (result , expected )
567+ df = df_orig .copy ()
568+ result = df .where (mask , s1 , axis = "columns" , inplace = True )
569+ assert result is df
570+ tm .assert_frame_equal (df , expected )
562571
572+ df = df_orig .copy ()
563573 result = df .where (mask , s2 , axis = "index" )
564- expected = DataFrame (2.0 , columns = df .columns , index = df .index )
574+ expected = DataFrame (2.0 , columns = df_orig .columns , index = df_orig .index )
565575 expected [2 ] = expected [2 ].astype ("int64" )
566576 expected [3 ] = expected [3 ].astype ("int64" )
567577 tm .assert_frame_equal (result , expected )
568578
569- result = df .copy ()
570- return_value = result .where (mask , s2 , axis = "index" , inplace = True )
571- assert return_value is None
572- tm .assert_frame_equal (result , expected )
579+ df = df_orig .copy ()
580+ result = df .where (mask , s2 , axis = "index" , inplace = True )
581+ assert result is df
582+ tm .assert_frame_equal (df , expected )
573583
574584 # DataFrame vs DataFrame
575- d1 = df .copy ().drop (1 , axis = 0 )
585+ d1 = df_orig .copy ().drop (1 , axis = 0 )
576586 # Explicit cast to avoid implicit cast when setting value to np.nan
577- expected = df .copy ().astype ("float" )
587+ expected = df_orig .copy ().astype ("float" )
578588 expected .loc [1 , :] = np .nan
579589
590+ df = df_orig .copy ()
580591 result = df .where (mask , d1 )
581592 tm .assert_frame_equal (result , expected )
582593 result = df .where (mask , d1 , axis = "index" )
583594 tm .assert_frame_equal (result , expected )
584- result = df .copy ()
595+ df = df_orig .copy ()
585596 with pytest .raises (TypeError , match = "Invalid value" ):
586- result .where (mask , d1 , inplace = True )
597+ df .where (mask , d1 , inplace = True )
587598 with pytest .raises (TypeError , match = "Invalid value" ):
588- return_value = result .where (mask , d1 , inplace = True , axis = "index" )
599+ df .where (mask , d1 , inplace = True , axis = "index" )
589600
590- d2 = df .copy ().drop (1 , axis = 1 )
591- expected = df .copy ()
601+ d2 = df_orig .copy ().drop (1 , axis = 1 )
602+ expected = df_orig .copy ()
592603 expected .loc [:, 1 ] = np .nan
593604
605+ df = df_orig .copy ()
594606 result = df .where (mask , d2 )
595607 tm .assert_frame_equal (result , expected )
596608 result = df .where (mask , d2 , axis = "columns" )
597609 tm .assert_frame_equal (result , expected )
598- result = df .copy ()
599- return_value = result .where (mask , d2 , inplace = True )
600- assert return_value is None
601- tm .assert_frame_equal (result , expected )
602- result = df .copy ()
603- return_value = result .where (mask , d2 , inplace = True , axis = "columns" )
604- assert return_value is None
605- tm .assert_frame_equal (result , expected )
610+ df = df_orig .copy ()
611+ result = df .where (mask , d2 , inplace = True )
612+ assert result is df
613+ tm .assert_frame_equal (df , expected )
614+ df = df_orig .copy ()
615+ result = df .where (mask , d2 , inplace = True , axis = "columns" )
616+ assert result is df
617+ tm .assert_frame_equal (df , expected )
606618
607619 def test_where_callable (self ):
608620 # GH 12533
@@ -1047,6 +1059,7 @@ def test_where_inplace_no_other():
10471059 # GH#51685
10481060 df = DataFrame ({"a" : [1.0 , 2.0 ], "b" : ["x" , "y" ]})
10491061 cond = DataFrame ({"a" : [True , False ], "b" : [False , True ]})
1050- df .where (cond , inplace = True )
1062+ result = df .where (cond , inplace = True )
1063+ assert result is df
10511064 expected = DataFrame ({"a" : [1 , np .nan ], "b" : [np .nan , "y" ]})
10521065 tm .assert_frame_equal (df , expected )
0 commit comments