1
1
import numpy as np
2
2
import pytest
3
3
4
- from pandas import (
5
- DataFrame ,
6
- Series ,
7
- )
4
+ import pandas as pd
8
5
import pandas ._testing as tm
9
6
10
7
@@ -30,7 +27,7 @@ def test_inplace_clip(self, float_frame):
30
27
31
28
def test_dataframe_clip (self ):
32
29
# 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 )))
34
31
35
32
for lb , ub in [(- 1 , 1 ), (1 , - 1 )]:
36
33
clipped_df = df .clip (lb , ub )
@@ -46,12 +43,12 @@ def test_dataframe_clip(self):
46
43
def test_clip_mixed_numeric (self ):
47
44
# clip on mixed integer or floats
48
45
# 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 ]})
50
47
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 ]})
52
49
tm .assert_frame_equal (result , expected )
53
50
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" ])
55
52
expected = df .dtypes
56
53
result = df .clip (upper = 3 ).dtypes
57
54
tm .assert_series_equal (result , expected )
@@ -60,8 +57,8 @@ def test_clip_mixed_numeric(self):
60
57
def test_clip_against_series (self , inplace ):
61
58
# GH#6966
62
59
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 ))
65
62
ub = lb + 1
66
63
67
64
original = df .copy ()
@@ -98,21 +95,21 @@ def test_clip_against_list_like(self, inplace, lower, axis, res):
98
95
# GH#15390
99
96
arr = np .array ([[1.0 , 2.0 , 3.0 ], [4.0 , 5.0 , 6.0 ], [7.0 , 8.0 , 9.0 ]])
100
97
101
- original = DataFrame (
98
+ original = pd . DataFrame (
102
99
arr , columns = ["one" , "two" , "three" ], index = ["a" , "b" , "c" ]
103
100
)
104
101
105
102
result = original .clip (lower = lower , upper = [5 , 6 , 7 ], axis = axis , inplace = inplace )
106
103
107
- expected = DataFrame (res , columns = original .columns , index = original .index )
104
+ expected = pd . DataFrame (res , columns = original .columns , index = original .index )
108
105
if inplace :
109
106
result = original
110
107
tm .assert_frame_equal (result , expected , check_exact = True )
111
108
112
109
@pytest .mark .parametrize ("axis" , [0 , 1 , None ])
113
110
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 )))
116
113
ub = lb + 1
117
114
118
115
clipped_df = df .clip (lb , ub , axis = axis )
@@ -127,15 +124,15 @@ def test_clip_against_frame(self, axis):
127
124
128
125
def test_clip_against_unordered_columns (self ):
129
126
# GH#20911
130
- df1 = DataFrame (
127
+ df1 = pd . DataFrame (
131
128
np .random .default_rng (2 ).standard_normal ((1000 , 4 )),
132
129
columns = ["A" , "B" , "C" , "D" ],
133
130
)
134
- df2 = DataFrame (
131
+ df2 = pd . DataFrame (
135
132
np .random .default_rng (2 ).standard_normal ((1000 , 4 )),
136
133
columns = ["D" , "A" , "B" , "C" ],
137
134
)
138
- df3 = DataFrame (df2 .values - 1 , columns = ["B" , "D" , "C" , "A" ])
135
+ df3 = pd . DataFrame (df2 .values - 1 , columns = ["B" , "D" , "C" , "A" ])
139
136
result_upper = df1 .clip (lower = 0 , upper = df2 )
140
137
expected_upper = df1 .clip (lower = 0 , upper = df2 [df1 .columns ])
141
138
result_lower = df1 .clip (lower = df3 , upper = 3 )
@@ -153,48 +150,54 @@ def test_clip_with_na_args(self, float_frame):
153
150
tm .assert_frame_equal (float_frame .clip (upper = np .nan , lower = np .nan ), float_frame )
154
151
155
152
# 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 ]})
157
154
158
155
result = df .clip (lower = [4 , 5 , np .nan ], axis = 0 )
159
- expected = DataFrame (
156
+ expected = pd . DataFrame (
160
157
{
161
- "col_0" : Series ([4 , 5 , 3 ], dtype = "float" ),
158
+ "col_0" : pd . Series ([4 , 5 , 3 ], dtype = "float" ),
162
159
"col_1" : [4 , 5 , 6 ],
163
160
"col_2" : [7 , 8 , 9 ],
164
161
}
165
162
)
166
163
tm .assert_frame_equal (result , expected )
167
164
168
165
result = df .clip (lower = [4 , 5 , np .nan ], axis = 1 )
169
- expected = DataFrame (
166
+ expected = pd . DataFrame (
170
167
{"col_0" : [4 , 4 , 4 ], "col_1" : [5 , 5 , 6 ], "col_2" : [7 , 8 , 9 ]}
171
168
)
172
169
tm .assert_frame_equal (result , expected )
173
170
174
171
# GH#40420
175
172
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 ])
178
175
result = df .clip (lower = t , axis = 0 )
179
- expected = DataFrame (
176
+ expected = pd . DataFrame (
180
177
{"col_0" : [9 , - 3 , 0 , 6 , 5 ], "col_1" : [2 , - 4 , 6 , 8 , 3 ]}, dtype = "float"
181
178
)
182
179
tm .assert_frame_equal (result , expected )
183
180
184
181
def test_clip_int_data_with_float_bound (self ):
185
182
# GH51472
186
- df = DataFrame ({"a" : [1 , 2 , 3 ]})
183
+ df = pd . DataFrame ({"a" : [1 , 2 , 3 ]})
187
184
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 ]})
189
186
tm .assert_frame_equal (result , expected )
190
187
191
188
def test_clip_with_list_bound (self ):
192
189
# GH#54817
193
- df = DataFrame ([1 , 5 ])
194
- expected = DataFrame ([3 , 5 ])
190
+ df = pd . DataFrame ([1 , 5 ])
191
+ expected = pd . DataFrame ([3 , 5 ])
195
192
result = df .clip ([3 ])
196
193
tm .assert_frame_equal (result , expected )
197
194
198
- expected = DataFrame ([1 , 3 ])
195
+ expected = pd . DataFrame ([1 , 3 ])
199
196
result = df .clip (upper = [3 ])
200
197
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