1
1
from copy import deepcopy
2
- import datetime
3
2
import inspect
4
3
import pydoc
5
4
import warnings
6
5
7
6
import numpy as np
8
7
import pytest
9
8
10
- from pandas .compat import IS64 , is_platform_windows
11
9
import pandas .util ._test_decorators as td
12
10
from pandas .util ._test_decorators import async_mark , skip_if_no
13
11
14
12
import pandas as pd
15
- from pandas import Categorical , DataFrame , Series , date_range , timedelta_range
13
+ from pandas import DataFrame , Series , date_range , timedelta_range
16
14
import pandas ._testing as tm
17
15
18
16
@@ -30,23 +28,6 @@ def test_getitem_pop_assign_name(self, float_frame):
30
28
s2 = s .loc [:]
31
29
assert s2 .name == "B"
32
30
33
- def test_add_prefix_suffix (self , float_frame ):
34
- with_prefix = float_frame .add_prefix ("foo#" )
35
- expected = pd .Index ([f"foo#{ c } " for c in float_frame .columns ])
36
- tm .assert_index_equal (with_prefix .columns , expected )
37
-
38
- with_suffix = float_frame .add_suffix ("#foo" )
39
- expected = pd .Index ([f"{ c } #foo" for c in float_frame .columns ])
40
- tm .assert_index_equal (with_suffix .columns , expected )
41
-
42
- with_pct_prefix = float_frame .add_prefix ("%" )
43
- expected = pd .Index ([f"%{ c } " for c in float_frame .columns ])
44
- tm .assert_index_equal (with_pct_prefix .columns , expected )
45
-
46
- with_pct_suffix = float_frame .add_suffix ("%" )
47
- expected = pd .Index ([f"{ c } %" for c in float_frame .columns ])
48
- tm .assert_index_equal (with_pct_suffix .columns , expected )
49
-
50
31
def test_get_axis (self , float_frame ):
51
32
f = float_frame
52
33
assert f ._get_axis_number (0 ) == 0
@@ -76,9 +57,6 @@ def test_get_axis(self, float_frame):
76
57
with pytest .raises (ValueError , match = "No axis named" ):
77
58
f ._get_axis_number (None )
78
59
79
- def test_keys (self , float_frame ):
80
- assert float_frame .keys () is float_frame .columns
81
-
82
60
def test_column_contains_raises (self , float_frame ):
83
61
with pytest .raises (TypeError , match = "unhashable type: 'Index'" ):
84
62
float_frame .columns in float_frame
@@ -149,143 +127,6 @@ def test_empty(self, float_frame, float_string_frame):
149
127
del df ["A" ]
150
128
assert not df .empty
151
129
152
- def test_iteritems (self ):
153
- df = DataFrame ([[1 , 2 , 3 ], [4 , 5 , 6 ]], columns = ["a" , "a" , "b" ])
154
- for k , v in df .items ():
155
- assert isinstance (v , DataFrame ._constructor_sliced )
156
-
157
- def test_items (self ):
158
- # GH 17213, GH 13918
159
- cols = ["a" , "b" , "c" ]
160
- df = DataFrame ([[1 , 2 , 3 ], [4 , 5 , 6 ]], columns = cols )
161
- for c , (k , v ) in zip (cols , df .items ()):
162
- assert c == k
163
- assert isinstance (v , Series )
164
- assert (df [k ] == v ).all ()
165
-
166
- def test_iter (self , float_frame ):
167
- assert tm .equalContents (list (float_frame ), float_frame .columns )
168
-
169
- def test_iterrows (self , float_frame , float_string_frame ):
170
- for k , v in float_frame .iterrows ():
171
- exp = float_frame .loc [k ]
172
- tm .assert_series_equal (v , exp )
173
-
174
- for k , v in float_string_frame .iterrows ():
175
- exp = float_string_frame .loc [k ]
176
- tm .assert_series_equal (v , exp )
177
-
178
- def test_iterrows_iso8601 (self ):
179
- # GH 19671
180
- s = DataFrame (
181
- {
182
- "non_iso8601" : ["M1701" , "M1802" , "M1903" , "M2004" ],
183
- "iso8601" : date_range ("2000-01-01" , periods = 4 , freq = "M" ),
184
- }
185
- )
186
- for k , v in s .iterrows ():
187
- exp = s .loc [k ]
188
- tm .assert_series_equal (v , exp )
189
-
190
- def test_iterrows_corner (self ):
191
- # gh-12222
192
- df = DataFrame (
193
- {
194
- "a" : [datetime .datetime (2015 , 1 , 1 )],
195
- "b" : [None ],
196
- "c" : [None ],
197
- "d" : ["" ],
198
- "e" : [[]],
199
- "f" : [set ()],
200
- "g" : [{}],
201
- }
202
- )
203
- expected = Series (
204
- [datetime .datetime (2015 , 1 , 1 ), None , None , "" , [], set (), {}],
205
- index = list ("abcdefg" ),
206
- name = 0 ,
207
- dtype = "object" ,
208
- )
209
- _ , result = next (df .iterrows ())
210
- tm .assert_series_equal (result , expected )
211
-
212
- def test_itertuples (self , float_frame ):
213
- for i , tup in enumerate (float_frame .itertuples ()):
214
- s = DataFrame ._constructor_sliced (tup [1 :])
215
- s .name = tup [0 ]
216
- expected = float_frame .iloc [i , :].reset_index (drop = True )
217
- tm .assert_series_equal (s , expected )
218
-
219
- df = DataFrame (
220
- {"floats" : np .random .randn (5 ), "ints" : range (5 )}, columns = ["floats" , "ints" ]
221
- )
222
-
223
- for tup in df .itertuples (index = False ):
224
- assert isinstance (tup [1 ], int )
225
-
226
- df = DataFrame (data = {"a" : [1 , 2 , 3 ], "b" : [4 , 5 , 6 ]})
227
- dfaa = df [["a" , "a" ]]
228
-
229
- assert list (dfaa .itertuples ()) == [(0 , 1 , 1 ), (1 , 2 , 2 ), (2 , 3 , 3 )]
230
-
231
- # repr with int on 32-bit/windows
232
- if not (is_platform_windows () or not IS64 ):
233
- assert (
234
- repr (list (df .itertuples (name = None )))
235
- == "[(0, 1, 4), (1, 2, 5), (2, 3, 6)]"
236
- )
237
-
238
- tup = next (df .itertuples (name = "TestName" ))
239
- assert tup ._fields == ("Index" , "a" , "b" )
240
- assert (tup .Index , tup .a , tup .b ) == tup
241
- assert type (tup ).__name__ == "TestName"
242
-
243
- df .columns = ["def" , "return" ]
244
- tup2 = next (df .itertuples (name = "TestName" ))
245
- assert tup2 == (0 , 1 , 4 )
246
- assert tup2 ._fields == ("Index" , "_1" , "_2" )
247
-
248
- df3 = DataFrame ({"f" + str (i ): [i ] for i in range (1024 )})
249
- # will raise SyntaxError if trying to create namedtuple
250
- tup3 = next (df3 .itertuples ())
251
- assert isinstance (tup3 , tuple )
252
- assert hasattr (tup3 , "_fields" )
253
-
254
- # GH 28282
255
- df_254_columns = DataFrame ([{f"foo_{ i } " : f"bar_{ i } " for i in range (254 )}])
256
- result_254_columns = next (df_254_columns .itertuples (index = False ))
257
- assert isinstance (result_254_columns , tuple )
258
- assert hasattr (result_254_columns , "_fields" )
259
-
260
- df_255_columns = DataFrame ([{f"foo_{ i } " : f"bar_{ i } " for i in range (255 )}])
261
- result_255_columns = next (df_255_columns .itertuples (index = False ))
262
- assert isinstance (result_255_columns , tuple )
263
- assert hasattr (result_255_columns , "_fields" )
264
-
265
- def test_sequence_like_with_categorical (self ):
266
-
267
- # GH 7839
268
- # make sure can iterate
269
- df = DataFrame (
270
- {"id" : [1 , 2 , 3 , 4 , 5 , 6 ], "raw_grade" : ["a" , "b" , "b" , "a" , "a" , "e" ]}
271
- )
272
- df ["grade" ] = Categorical (df ["raw_grade" ])
273
-
274
- # basic sequencing testing
275
- result = list (df .grade .values )
276
- expected = np .array (df .grade .values ).tolist ()
277
- tm .assert_almost_equal (result , expected )
278
-
279
- # iteration
280
- for t in df .itertuples (index = False ):
281
- str (t )
282
-
283
- for row , s in df .iterrows ():
284
- str (s )
285
-
286
- for c , col in df .items ():
287
- str (s )
288
-
289
130
def test_len (self , float_frame ):
290
131
assert len (float_frame ) == len (float_frame .index )
291
132
@@ -294,15 +135,6 @@ def test_len(self, float_frame):
294
135
expected = float_frame .reindex (columns = ["A" , "B" ]).values
295
136
tm .assert_almost_equal (arr , expected )
296
137
297
- def test_swapaxes (self ):
298
- df = DataFrame (np .random .randn (10 , 5 ))
299
- tm .assert_frame_equal (df .T , df .swapaxes (0 , 1 ))
300
- tm .assert_frame_equal (df .T , df .swapaxes (1 , 0 ))
301
- tm .assert_frame_equal (df , df .swapaxes (0 , 0 ))
302
- msg = "No axis named 2 for object type DataFrame"
303
- with pytest .raises (ValueError , match = msg ):
304
- df .swapaxes (2 , 5 )
305
-
306
138
def test_axis_aliases (self , float_frame ):
307
139
f = float_frame
308
140
@@ -321,10 +153,6 @@ def test_class_axis(self):
321
153
assert pydoc .getdoc (DataFrame .index )
322
154
assert pydoc .getdoc (DataFrame .columns )
323
155
324
- def test_items_names (self , float_string_frame ):
325
- for k , v in float_string_frame .items ():
326
- assert v .name == k
327
-
328
156
def test_series_put_names (self , float_string_frame ):
329
157
series = float_string_frame ._series
330
158
for k , v in series .items ():
0 commit comments