@@ -748,6 +748,82 @@ def test_head_tail(method, using_copy_on_write):
748
748
tm .assert_frame_equal (df , df_orig )
749
749
750
750
751
+ def test_infer_objects (using_copy_on_write ):
752
+ df = DataFrame ({"a" : [1 , 2 ], "b" : "c" , "c" : 1 , "d" : "x" })
753
+ df_orig = df .copy ()
754
+ df2 = df .infer_objects ()
755
+
756
+ if using_copy_on_write :
757
+ assert np .shares_memory (get_array (df2 , "a" ), get_array (df , "a" ))
758
+ assert np .shares_memory (get_array (df2 , "b" ), get_array (df , "b" ))
759
+
760
+ else :
761
+ assert not np .shares_memory (get_array (df2 , "a" ), get_array (df , "a" ))
762
+ assert not np .shares_memory (get_array (df2 , "b" ), get_array (df , "b" ))
763
+
764
+ df2 .iloc [0 , 0 ] = 0
765
+ df2 .iloc [0 , 1 ] = "d"
766
+ if using_copy_on_write :
767
+ assert not np .shares_memory (get_array (df2 , "a" ), get_array (df , "a" ))
768
+ assert not np .shares_memory (get_array (df2 , "b" ), get_array (df , "b" ))
769
+ tm .assert_frame_equal (df , df_orig )
770
+
771
+
772
+ def test_infer_objects_no_reference (using_copy_on_write ):
773
+ df = DataFrame (
774
+ {
775
+ "a" : [1 , 2 ],
776
+ "b" : "c" ,
777
+ "c" : 1 ,
778
+ "d" : Series (
779
+ [Timestamp ("2019-12-31" ), Timestamp ("2020-12-31" )], dtype = "object"
780
+ ),
781
+ "e" : "b" ,
782
+ }
783
+ )
784
+ df = df .infer_objects ()
785
+
786
+ arr_a = get_array (df , "a" )
787
+ arr_b = get_array (df , "b" )
788
+ arr_d = get_array (df , "d" )
789
+
790
+ df .iloc [0 , 0 ] = 0
791
+ df .iloc [0 , 1 ] = "d"
792
+ df .iloc [0 , 3 ] = Timestamp ("2018-12-31" )
793
+ if using_copy_on_write :
794
+ assert np .shares_memory (arr_a , get_array (df , "a" ))
795
+ # TODO(CoW): Block splitting causes references here
796
+ assert not np .shares_memory (arr_b , get_array (df , "b" ))
797
+ assert np .shares_memory (arr_d , get_array (df , "d" ))
798
+
799
+
800
+ def test_infer_objects_reference (using_copy_on_write ):
801
+ df = DataFrame (
802
+ {
803
+ "a" : [1 , 2 ],
804
+ "b" : "c" ,
805
+ "c" : 1 ,
806
+ "d" : Series (
807
+ [Timestamp ("2019-12-31" ), Timestamp ("2020-12-31" )], dtype = "object"
808
+ ),
809
+ }
810
+ )
811
+ view = df [:] # noqa: F841
812
+ df = df .infer_objects ()
813
+
814
+ arr_a = get_array (df , "a" )
815
+ arr_b = get_array (df , "b" )
816
+ arr_d = get_array (df , "d" )
817
+
818
+ df .iloc [0 , 0 ] = 0
819
+ df .iloc [0 , 1 ] = "d"
820
+ df .iloc [0 , 3 ] = Timestamp ("2018-12-31" )
821
+ if using_copy_on_write :
822
+ assert not np .shares_memory (arr_a , get_array (df , "a" ))
823
+ assert not np .shares_memory (arr_b , get_array (df , "b" ))
824
+ assert np .shares_memory (arr_d , get_array (df , "d" ))
825
+
826
+
751
827
@pytest .mark .parametrize (
752
828
"kwargs" ,
753
829
[
0 commit comments