11import numpy as np
22import pytest
33
4- from pandas import (
5- DataFrame ,
6- Series ,
7- )
4+ import pandas as pd
85import pandas ._testing as tm
96
107
@@ -30,7 +27,7 @@ def test_inplace_clip(self, float_frame):
3027
3128 def test_dataframe_clip (self ):
3229 # GH#2747
33- df = DataFrame (np .random .default_rng (2 ).standard_normal ((1000 , 2 )))
30+ df = pd . DataFrame (np .random .default_rng (2 ).standard_normal ((1000 , 2 )))
3431
3532 for lb , ub in [(- 1 , 1 ), (1 , - 1 )]:
3633 clipped_df = df .clip (lb , ub )
@@ -46,12 +43,12 @@ def test_dataframe_clip(self):
4643 def test_clip_mixed_numeric (self ):
4744 # clip on mixed integer or floats
4845 # GH#24162, clipping now preserves numeric types per column
49- df = DataFrame ({"A" : [1 , 2 , 3 ], "B" : [1.0 , np .nan , 3.0 ]})
46+ df = pd . DataFrame ({"A" : [1 , 2 , 3 ], "B" : [1.0 , np .nan , 3.0 ]})
5047 result = df .clip (1 , 2 )
51- expected = DataFrame ({"A" : [1 , 2 , 2 ], "B" : [1.0 , np .nan , 2.0 ]})
48+ expected = pd . DataFrame ({"A" : [1 , 2 , 2 ], "B" : [1.0 , np .nan , 2.0 ]})
5249 tm .assert_frame_equal (result , expected )
5350
54- df = DataFrame ([[1 , 2 , 3.4 ], [3 , 4 , 5.6 ]], columns = ["foo" , "bar" , "baz" ])
51+ df = pd . DataFrame ([[1 , 2 , 3.4 ], [3 , 4 , 5.6 ]], columns = ["foo" , "bar" , "baz" ])
5552 expected = df .dtypes
5653 result = df .clip (upper = 3 ).dtypes
5754 tm .assert_series_equal (result , expected )
@@ -60,8 +57,8 @@ def test_clip_mixed_numeric(self):
6057 def test_clip_against_series (self , inplace ):
6158 # GH#6966
6259
63- df = DataFrame (np .random .default_rng (2 ).standard_normal ((1000 , 2 )))
64- lb = Series (np .random .default_rng (2 ).standard_normal (1000 ))
60+ df = pd . DataFrame (np .random .default_rng (2 ).standard_normal ((1000 , 2 )))
61+ lb = pd . Series (np .random .default_rng (2 ).standard_normal (1000 ))
6562 ub = lb + 1
6663
6764 original = df .copy ()
@@ -98,21 +95,21 @@ def test_clip_against_list_like(self, inplace, lower, axis, res):
9895 # GH#15390
9996 arr = np .array ([[1.0 , 2.0 , 3.0 ], [4.0 , 5.0 , 6.0 ], [7.0 , 8.0 , 9.0 ]])
10097
101- original = DataFrame (
98+ original = pd . DataFrame (
10299 arr , columns = ["one" , "two" , "three" ], index = ["a" , "b" , "c" ]
103100 )
104101
105102 result = original .clip (lower = lower , upper = [5 , 6 , 7 ], axis = axis , inplace = inplace )
106103
107- expected = DataFrame (res , columns = original .columns , index = original .index )
104+ expected = pd . DataFrame (res , columns = original .columns , index = original .index )
108105 if inplace :
109106 result = original
110107 tm .assert_frame_equal (result , expected , check_exact = True )
111108
112109 @pytest .mark .parametrize ("axis" , [0 , 1 , None ])
113110 def test_clip_against_frame (self , axis ):
114- df = DataFrame (np .random .default_rng (2 ).standard_normal ((1000 , 2 )))
115- lb = DataFrame (np .random .default_rng (2 ).standard_normal ((1000 , 2 )))
111+ df = pd . DataFrame (np .random .default_rng (2 ).standard_normal ((1000 , 2 )))
112+ lb = pd . DataFrame (np .random .default_rng (2 ).standard_normal ((1000 , 2 )))
116113 ub = lb + 1
117114
118115 clipped_df = df .clip (lb , ub , axis = axis )
@@ -127,15 +124,15 @@ def test_clip_against_frame(self, axis):
127124
128125 def test_clip_against_unordered_columns (self ):
129126 # GH#20911
130- df1 = DataFrame (
127+ df1 = pd . DataFrame (
131128 np .random .default_rng (2 ).standard_normal ((1000 , 4 )),
132129 columns = ["A" , "B" , "C" , "D" ],
133130 )
134- df2 = DataFrame (
131+ df2 = pd . DataFrame (
135132 np .random .default_rng (2 ).standard_normal ((1000 , 4 )),
136133 columns = ["D" , "A" , "B" , "C" ],
137134 )
138- df3 = DataFrame (df2 .values - 1 , columns = ["B" , "D" , "C" , "A" ])
135+ df3 = pd . DataFrame (df2 .values - 1 , columns = ["B" , "D" , "C" , "A" ])
139136 result_upper = df1 .clip (lower = 0 , upper = df2 )
140137 expected_upper = df1 .clip (lower = 0 , upper = df2 [df1 .columns ])
141138 result_lower = df1 .clip (lower = df3 , upper = 3 )
@@ -153,48 +150,54 @@ def test_clip_with_na_args(self, float_frame):
153150 tm .assert_frame_equal (float_frame .clip (upper = np .nan , lower = np .nan ), float_frame )
154151
155152 # GH#19992 and adjusted in GH#40420
156- df = DataFrame ({"col_0" : [1 , 2 , 3 ], "col_1" : [4 , 5 , 6 ], "col_2" : [7 , 8 , 9 ]})
153+ df = pd . DataFrame ({"col_0" : [1 , 2 , 3 ], "col_1" : [4 , 5 , 6 ], "col_2" : [7 , 8 , 9 ]})
157154
158155 result = df .clip (lower = [4 , 5 , np .nan ], axis = 0 )
159- expected = DataFrame (
156+ expected = pd . DataFrame (
160157 {
161- "col_0" : Series ([4 , 5 , 3 ], dtype = "float" ),
158+ "col_0" : pd . Series ([4 , 5 , 3 ], dtype = "float" ),
162159 "col_1" : [4 , 5 , 6 ],
163160 "col_2" : [7 , 8 , 9 ],
164161 }
165162 )
166163 tm .assert_frame_equal (result , expected )
167164
168165 result = df .clip (lower = [4 , 5 , np .nan ], axis = 1 )
169- expected = DataFrame (
166+ expected = pd . DataFrame (
170167 {"col_0" : [4 , 4 , 4 ], "col_1" : [5 , 5 , 6 ], "col_2" : [7 , 8 , 9 ]}
171168 )
172169 tm .assert_frame_equal (result , expected )
173170
174171 # GH#40420
175172 data = {"col_0" : [9 , - 3 , 0 , - 1 , 5 ], "col_1" : [- 2 , - 7 , 6 , 8 , - 5 ]}
176- df = DataFrame (data )
177- t = Series ([2 , - 4 , np .nan , 6 , 3 ])
173+ df = pd . DataFrame (data )
174+ t = pd . Series ([2 , - 4 , np .nan , 6 , 3 ])
178175 result = df .clip (lower = t , axis = 0 )
179- expected = DataFrame (
176+ expected = pd . DataFrame (
180177 {"col_0" : [9 , - 3 , 0 , 6 , 5 ], "col_1" : [2 , - 4 , 6 , 8 , 3 ]}, dtype = "float"
181178 )
182179 tm .assert_frame_equal (result , expected )
183180
184181 def test_clip_int_data_with_float_bound (self ):
185182 # GH51472
186- df = DataFrame ({"a" : [1 , 2 , 3 ]})
183+ df = pd . DataFrame ({"a" : [1 , 2 , 3 ]})
187184 result = df .clip (lower = 1.5 )
188- expected = DataFrame ({"a" : [1.5 , 2.0 , 3.0 ]})
185+ expected = pd . DataFrame ({"a" : [1.5 , 2.0 , 3.0 ]})
189186 tm .assert_frame_equal (result , expected )
190187
191188 def test_clip_with_list_bound (self ):
192189 # GH#54817
193- df = DataFrame ([1 , 5 ])
194- expected = DataFrame ([3 , 5 ])
190+ df = pd . DataFrame ([1 , 5 ])
191+ expected = pd . DataFrame ([3 , 5 ])
195192 result = df .clip ([3 ])
196193 tm .assert_frame_equal (result , expected )
197194
198- expected = DataFrame ([1 , 3 ])
195+ expected = pd . DataFrame ([1 , 3 ])
199196 result = df .clip (upper = [3 ])
200197 tm .assert_frame_equal (result , expected )
198+
199+ def test_clip_lower_greater_than_upper (self ):
200+ df = pd .DataFrame ({"A" : [1 , 2 , 3 ]})
201+ result = df .clip (lower = 5 , upper = 3 )
202+ expected = pd .DataFrame ({"A" : [3 , 3 , 3 ]})
203+ tm .assert_frame_equal (result , expected )
0 commit comments