11import numpy as np
22import pytest
33
4- from pandas ._config import using_string_dtype
5-
64import pandas .util ._test_decorators as td
75
86from pandas import (
@@ -64,7 +62,7 @@ def test_interpolate_inplace(self, frame_or_series, request):
6462 assert np .shares_memory (orig , obj .values )
6563 assert orig .squeeze ()[1 ] == 1.5
6664
67- def test_interp_basic (self , using_infer_string ):
65+ def test_interp_with_non_numeric (self , using_infer_string ):
6866 df = DataFrame (
6967 {
7068 "A" : [1 , 2 , np .nan , 4 ],
@@ -73,43 +71,74 @@ def test_interp_basic(self, using_infer_string):
7371 "D" : list ("abcd" ),
7472 }
7573 )
74+ df_orig = df .copy ()
75+ expected = DataFrame (
76+ {
77+ "A" : [1.0 , 2.0 , 3.0 , 4.0 ],
78+ "B" : [1.0 , 4.0 , 9.0 , 9.0 ],
79+ "C" : [1 , 2 , 3 , 5 ],
80+ "D" : list ("abcd" ),
81+ }
82+ )
83+
7684 dtype = "str" if using_infer_string else "object"
7785 msg = f"[Cc]annot interpolate with { dtype } dtype"
7886 with pytest .raises (TypeError , match = msg ):
7987 df .interpolate ()
88+ tm .assert_frame_equal (df , df_orig )
8089
81- cvalues = df ["C" ]._values
82- dvalues = df ["D" ].values
8390 with pytest .raises (TypeError , match = msg ):
8491 df .interpolate (inplace = True )
92+ # columns A and B already get interpolated before we hit the error
93+ tm .assert_frame_equal (df , expected )
94+
95+ def test_interp_basic (self ):
96+ df = DataFrame (
97+ {
98+ "A" : [1 , 2 , np .nan , 4 ],
99+ "B" : [1 , 4 , 9 , np .nan ],
100+ "C" : [1 , 2 , 3 , 5 ],
101+ }
102+ )
103+ df_orig = df .copy ()
104+ expected = DataFrame (
105+ {
106+ "A" : [1.0 , 2.0 , 3.0 , 4.0 ],
107+ "B" : [1.0 , 4.0 , 9.0 , 9.0 ],
108+ "C" : [1 , 2 , 3 , 5 ],
109+ }
110+ )
111+ result = df .interpolate ()
112+ tm .assert_frame_equal (result , expected )
113+
114+ # check we didn't operate inplace GH#45791
115+ tm .assert_frame_equal (df , df_orig )
116+ bvalues = df ["B" ]._values
117+ cvalues = df ["C" ]._values
118+ assert not tm .shares_memory (bvalues , result ["B" ]._values )
119+ assert tm .shares_memory (cvalues , result ["C" ]._values )
120+
121+ res = df .interpolate (inplace = True )
122+ assert res is None
123+ tm .assert_frame_equal (df , expected )
85124
86125 # check we DID operate inplace
126+ assert tm .shares_memory (df ["B" ]._values , bvalues )
87127 assert tm .shares_memory (df ["C" ]._values , cvalues )
88- assert tm .shares_memory (df ["D" ]._values , dvalues )
89128
90- @pytest .mark .xfail (
91- using_string_dtype (), reason = "interpolate doesn't work for string"
92- )
93- def test_interp_basic_with_non_range_index (self , using_infer_string ):
129+ def test_interp_basic_with_non_range_index (self ):
94130 df = DataFrame (
95131 {
96132 "A" : [1 , 2 , np .nan , 4 ],
97133 "B" : [1 , 4 , 9 , np .nan ],
98134 "C" : [1 , 2 , 3 , 5 ],
99- "D" : list ("abcd" ),
100135 }
101136 )
102-
103- msg = "DataFrame cannot interpolate with object dtype"
104- if not using_infer_string :
105- with pytest .raises (TypeError , match = msg ):
106- df .set_index ("C" ).interpolate ()
107- else :
108- result = df .set_index ("C" ).interpolate ()
109- expected = df .set_index ("C" )
110- expected .loc [3 , "A" ] = 2.66667
111- expected .loc [5 , "B" ] = 9
112- tm .assert_frame_equal (result , expected )
137+ result = df .set_index ("C" ).interpolate ()
138+ expected = df .set_index ("C" )
139+ expected .loc [3 , "A" ] = 2.66667
140+ expected .loc [5 , "B" ] = 9
141+ tm .assert_frame_equal (result , expected )
113142
114143 def test_interp_empty (self ):
115144 # https://github.com/pandas-dev/pandas/issues/35598
0 commit comments