7
7
import pandas .util .testing as tm
8
8
from pandas .util .testing import assert_almost_equal , assert_series_equal
9
9
10
- from .common import TestData
11
10
12
-
13
- class TestSeriesSorting (TestData ):
14
- def test_sort_values (self ):
11
+ class TestSeriesSorting :
12
+ def test_sort_values (self , datetime_series ):
15
13
16
14
# check indexes are reordered corresponding with the values
17
15
ser = Series ([3 , 2 , 4 , 1 ], ["A" , "B" , "C" , "D" ])
18
16
expected = Series ([1 , 2 , 3 , 4 ], ["D" , "B" , "A" , "C" ])
19
17
result = ser .sort_values ()
20
18
tm .assert_series_equal (expected , result )
21
19
22
- ts = self . ts .copy ()
20
+ ts = datetime_series .copy ()
23
21
ts [:5 ] = np .NaN
24
22
vals = ts .values
25
23
@@ -69,10 +67,12 @@ def test_sort_values(self):
69
67
ts .sort_values (ascending = "foobar" )
70
68
71
69
# inplace=True
72
- ts = self . ts .copy ()
70
+ ts = datetime_series .copy ()
73
71
ts .sort_values (ascending = False , inplace = True )
74
- tm .assert_series_equal (ts , self .ts .sort_values (ascending = False ))
75
- tm .assert_index_equal (ts .index , self .ts .sort_values (ascending = False ).index )
72
+ tm .assert_series_equal (ts , datetime_series .sort_values (ascending = False ))
73
+ tm .assert_index_equal (
74
+ ts .index , datetime_series .sort_values (ascending = False ).index
75
+ )
76
76
77
77
# GH 5856/5853
78
78
# Series.sort_values operating on a view
@@ -86,55 +86,59 @@ def test_sort_values(self):
86
86
with pytest .raises (ValueError , match = msg ):
87
87
s .sort_values (inplace = True )
88
88
89
- def test_sort_index (self ):
90
- rindex = list (self . ts .index )
89
+ def test_sort_index (self , datetime_series ):
90
+ rindex = list (datetime_series .index )
91
91
random .shuffle (rindex )
92
92
93
- random_order = self . ts .reindex (rindex )
93
+ random_order = datetime_series .reindex (rindex )
94
94
sorted_series = random_order .sort_index ()
95
- assert_series_equal (sorted_series , self . ts )
95
+ assert_series_equal (sorted_series , datetime_series )
96
96
97
97
# descending
98
98
sorted_series = random_order .sort_index (ascending = False )
99
- assert_series_equal (sorted_series , self .ts .reindex (self .ts .index [::- 1 ]))
99
+ assert_series_equal (
100
+ sorted_series , datetime_series .reindex (datetime_series .index [::- 1 ])
101
+ )
100
102
101
103
# compat on level
102
104
sorted_series = random_order .sort_index (level = 0 )
103
- assert_series_equal (sorted_series , self . ts )
105
+ assert_series_equal (sorted_series , datetime_series )
104
106
105
107
# compat on axis
106
108
sorted_series = random_order .sort_index (axis = 0 )
107
- assert_series_equal (sorted_series , self . ts )
109
+ assert_series_equal (sorted_series , datetime_series )
108
110
109
111
msg = "No axis named 1 for object type <class 'pandas.core.series.Series'>"
110
112
with pytest .raises (ValueError , match = msg ):
111
113
random_order .sort_values (axis = 1 )
112
114
113
115
sorted_series = random_order .sort_index (level = 0 , axis = 0 )
114
- assert_series_equal (sorted_series , self . ts )
116
+ assert_series_equal (sorted_series , datetime_series )
115
117
116
118
with pytest .raises (ValueError , match = msg ):
117
119
random_order .sort_index (level = 0 , axis = 1 )
118
120
119
- def test_sort_index_inplace (self ):
121
+ def test_sort_index_inplace (self , datetime_series ):
120
122
121
123
# For #11402
122
- rindex = list (self . ts .index )
124
+ rindex = list (datetime_series .index )
123
125
random .shuffle (rindex )
124
126
125
127
# descending
126
- random_order = self . ts .reindex (rindex )
128
+ random_order = datetime_series .reindex (rindex )
127
129
result = random_order .sort_index (ascending = False , inplace = True )
128
130
129
131
assert result is None
130
- tm .assert_series_equal (random_order , self .ts .reindex (self .ts .index [::- 1 ]))
132
+ tm .assert_series_equal (
133
+ random_order , datetime_series .reindex (datetime_series .index [::- 1 ])
134
+ )
131
135
132
136
# ascending
133
- random_order = self . ts .reindex (rindex )
137
+ random_order = datetime_series .reindex (rindex )
134
138
result = random_order .sort_index (ascending = True , inplace = True )
135
139
136
140
assert result is None
137
- tm .assert_series_equal (random_order , self . ts )
141
+ tm .assert_series_equal (random_order , datetime_series )
138
142
139
143
@pytest .mark .parametrize ("level" , ["A" , 0 ]) # GH 21052
140
144
def test_sort_index_multiindex (self , level ):
0 commit comments