@@ -301,13 +301,17 @@ def test_pivot_multi_functions(self):
301
301
expected = concat ([means , stds ], keys = ['mean' , 'std' ], axis = 1 )
302
302
tm .assert_frame_equal (result , expected )
303
303
304
- def test_pivot_index_with_nan (self ):
304
+ @pytest .mark .parametrize ('method' , [True , False ])
305
+ def test_pivot_index_with_nan (self , method ):
305
306
# GH 3588
306
307
nan = np .nan
307
308
df = DataFrame ({'a' : ['R1' , 'R2' , nan , 'R4' ],
308
309
'b' : ['C1' , 'C2' , 'C3' , 'C4' ],
309
310
'c' : [10 , 15 , 17 , 20 ]})
310
- result = df .pivot ('a' , 'b' , 'c' )
311
+ if method :
312
+ result = df .pivot ('a' , 'b' , 'c' )
313
+ else :
314
+ result = pd .pivot (df , 'a' , 'b' , 'c' )
311
315
expected = DataFrame ([[nan , nan , 17 , nan ], [10 , nan , nan , nan ],
312
316
[nan , 15 , nan , nan ], [nan , nan , nan , 20 ]],
313
317
index = Index ([nan , 'R1' , 'R2' , 'R4' ], name = 'a' ),
@@ -322,15 +326,23 @@ def test_pivot_index_with_nan(self):
322
326
df .loc [1 , 'a' ] = df .loc [3 , 'a' ] = nan
323
327
df .loc [1 , 'b' ] = df .loc [4 , 'b' ] = nan
324
328
325
- pv = df .pivot ('a' , 'b' , 'c' )
329
+ if method :
330
+ pv = df .pivot ('a' , 'b' , 'c' )
331
+ else :
332
+ pv = pd .pivot (df , 'a' , 'b' , 'c' )
326
333
assert pv .notna ().values .sum () == len (df )
327
334
328
335
for _ , row in df .iterrows ():
329
336
assert pv .loc [row ['a' ], row ['b' ]] == row ['c' ]
330
337
331
- tm .assert_frame_equal (df .pivot ('b' , 'a' , 'c' ), pv .T )
338
+ if method :
339
+ result = df .pivot ('b' , 'a' , 'c' )
340
+ else :
341
+ result = pd .pivot (df , 'b' , 'a' , 'c' )
342
+ tm .assert_frame_equal (result , pv .T )
332
343
333
- def test_pivot_with_tz (self ):
344
+ @pytest .mark .parametrize ('method' , [True , False ])
345
+ def test_pivot_with_tz (self , method ):
334
346
# GH 5878
335
347
df = DataFrame ({'dt1' : [datetime (2013 , 1 , 1 , 9 , 0 ),
336
348
datetime (2013 , 1 , 2 , 9 , 0 ),
@@ -358,7 +370,10 @@ def test_pivot_with_tz(self):
358
370
tz = 'US/Pacific' ),
359
371
columns = exp_col )
360
372
361
- pv = df .pivot (index = 'dt1' , columns = 'dt2' )
373
+ if method :
374
+ pv = df .pivot (index = 'dt1' , columns = 'dt2' )
375
+ else :
376
+ pv = pd .pivot (df , index = 'dt1' , columns = 'dt2' )
362
377
tm .assert_frame_equal (pv , expected )
363
378
364
379
expected = DataFrame ([[0 , 2 ], [1 , 3 ]],
@@ -371,10 +386,14 @@ def test_pivot_with_tz(self):
371
386
name = 'dt2' ,
372
387
tz = 'Asia/Tokyo' ))
373
388
374
- pv = df .pivot (index = 'dt1' , columns = 'dt2' , values = 'data1' )
389
+ if method :
390
+ pv = df .pivot (index = 'dt1' , columns = 'dt2' , values = 'data1' )
391
+ else :
392
+ pv = pd .pivot (df , index = 'dt1' , columns = 'dt2' , values = 'data1' )
375
393
tm .assert_frame_equal (pv , expected )
376
394
377
- def test_pivot_periods (self ):
395
+ @pytest .mark .parametrize ('method' , [True , False ])
396
+ def test_pivot_periods (self , method ):
378
397
df = DataFrame ({'p1' : [pd .Period ('2013-01-01' , 'D' ),
379
398
pd .Period ('2013-01-02' , 'D' ),
380
399
pd .Period ('2013-01-01' , 'D' ),
@@ -394,31 +413,39 @@ def test_pivot_periods(self):
394
413
index = pd .PeriodIndex (['2013-01-01' , '2013-01-02' ],
395
414
name = 'p1' , freq = 'D' ),
396
415
columns = exp_col )
397
-
398
- pv = df .pivot (index = 'p1' , columns = 'p2' )
416
+ if method :
417
+ pv = df .pivot (index = 'p1' , columns = 'p2' )
418
+ else :
419
+ pv = pd .pivot (df , index = 'p1' , columns = 'p2' )
399
420
tm .assert_frame_equal (pv , expected )
400
421
401
422
expected = DataFrame ([[0 , 2 ], [1 , 3 ]],
402
423
index = pd .PeriodIndex (['2013-01-01' , '2013-01-02' ],
403
424
name = 'p1' , freq = 'D' ),
404
425
columns = pd .PeriodIndex (['2013-01' , '2013-02' ],
405
426
name = 'p2' , freq = 'M' ))
406
-
407
- pv = df .pivot (index = 'p1' , columns = 'p2' , values = 'data1' )
427
+ if method :
428
+ pv = df .pivot (index = 'p1' , columns = 'p2' , values = 'data1' )
429
+ else :
430
+ pv = pd .pivot (df , index = 'p1' , columns = 'p2' , values = 'data1' )
408
431
tm .assert_frame_equal (pv , expected )
409
432
410
433
@pytest .mark .parametrize ('values' , [
411
434
['baz' , 'zoo' ], np .array (['baz' , 'zoo' ]),
412
435
pd .Series (['baz' , 'zoo' ]), pd .Index (['baz' , 'zoo' ])
413
436
])
414
- def test_pivot_with_list_like_values (self , values ):
437
+ @pytest .mark .parametrize ('method' , [True , False ])
438
+ def test_pivot_with_list_like_values (self , values , method ):
415
439
# issue #17160
416
440
df = pd .DataFrame ({'foo' : ['one' , 'one' , 'one' , 'two' , 'two' , 'two' ],
417
441
'bar' : ['A' , 'B' , 'C' , 'A' , 'B' , 'C' ],
418
442
'baz' : [1 , 2 , 3 , 4 , 5 , 6 ],
419
443
'zoo' : ['x' , 'y' , 'z' , 'q' , 'w' , 't' ]})
420
444
421
- result = df .pivot (index = 'foo' , columns = 'bar' , values = values )
445
+ if method :
446
+ result = df .pivot (index = 'foo' , columns = 'bar' , values = values )
447
+ else :
448
+ result = pd .pivot (df , index = 'foo' , columns = 'bar' , values = values )
422
449
423
450
data = [[1 , 2 , 3 , 'x' , 'y' , 'z' ],
424
451
[4 , 5 , 6 , 'q' , 'w' , 't' ]]
@@ -434,14 +461,18 @@ def test_pivot_with_list_like_values(self, values):
434
461
['bar' , 'baz' ], np .array (['bar' , 'baz' ]),
435
462
pd .Series (['bar' , 'baz' ]), pd .Index (['bar' , 'baz' ])
436
463
])
437
- def test_pivot_with_list_like_values_nans (self , values ):
464
+ @pytest .mark .parametrize ('method' , [True , False ])
465
+ def test_pivot_with_list_like_values_nans (self , values , method ):
438
466
# issue #17160
439
467
df = pd .DataFrame ({'foo' : ['one' , 'one' , 'one' , 'two' , 'two' , 'two' ],
440
468
'bar' : ['A' , 'B' , 'C' , 'A' , 'B' , 'C' ],
441
469
'baz' : [1 , 2 , 3 , 4 , 5 , 6 ],
442
470
'zoo' : ['x' , 'y' , 'z' , 'q' , 'w' , 't' ]})
443
471
444
- result = df .pivot (index = 'zoo' , columns = 'foo' , values = values )
472
+ if method :
473
+ result = df .pivot (index = 'zoo' , columns = 'foo' , values = values )
474
+ else :
475
+ result = pd .pivot (df , index = 'zoo' , columns = 'foo' , values = values )
445
476
446
477
data = [[np .nan , 'A' , np .nan , 4 ],
447
478
[np .nan , 'C' , np .nan , 6 ],
@@ -460,7 +491,8 @@ def test_pivot_with_list_like_values_nans(self, values):
460
491
@pytest .mark .xfail (reason = 'MultiIndexed unstack with tuple names fails'
461
492
'with KeyError GH#19966' ,
462
493
strict = True )
463
- def test_pivot_with_multiindex (self ):
494
+ @pytest .mark .parametrize ('method' , [True , False ])
495
+ def test_pivot_with_multiindex (self , method ):
464
496
# issue #17160
465
497
index = Index (data = [0 , 1 , 2 , 3 , 4 , 5 ])
466
498
data = [['one' , 'A' , 1 , 'x' ],
@@ -472,24 +504,35 @@ def test_pivot_with_multiindex(self):
472
504
columns = MultiIndex (levels = [['bar' , 'baz' ], ['first' , 'second' ]],
473
505
labels = [[0 , 0 , 1 , 1 ], [0 , 1 , 0 , 1 ]])
474
506
df = DataFrame (data = data , index = index , columns = columns , dtype = 'object' )
475
- result = df .pivot (index = ('bar' , 'first' ), columns = ('bar' , 'second' ),
476
- values = ('baz' , 'first' ))
507
+ if method :
508
+ result = df .pivot (index = ('bar' , 'first' ),
509
+ columns = ('bar' , 'second' ),
510
+ values = ('baz' , 'first' ))
511
+ else :
512
+ result = pd .pivot (df ,
513
+ index = ('bar' , 'first' ),
514
+ columns = ('bar' , 'second' ),
515
+ values = ('baz' , 'first' ))
477
516
478
517
data = {'A' : Series ([1 , 4 ], index = ['one' , 'two' ]),
479
518
'B' : Series ([2 , 5 ], index = ['one' , 'two' ]),
480
519
'C' : Series ([3 , 6 ], index = ['one' , 'two' ])}
481
520
expected = DataFrame (data )
482
521
tm .assert_frame_equal (result , expected )
483
522
484
- def test_pivot_with_tuple_of_values (self ):
523
+ @pytest .mark .parametrize ('method' , [True , False ])
524
+ def test_pivot_with_tuple_of_values (self , method ):
485
525
# issue #17160
486
526
df = pd .DataFrame ({'foo' : ['one' , 'one' , 'one' , 'two' , 'two' , 'two' ],
487
527
'bar' : ['A' , 'B' , 'C' , 'A' , 'B' , 'C' ],
488
528
'baz' : [1 , 2 , 3 , 4 , 5 , 6 ],
489
529
'zoo' : ['x' , 'y' , 'z' , 'q' , 'w' , 't' ]})
490
530
with pytest .raises (KeyError ):
491
531
# tuple is seen as a single column name
492
- df .pivot (index = 'zoo' , columns = 'foo' , values = ('bar' , 'baz' ))
532
+ if method :
533
+ df .pivot (index = 'zoo' , columns = 'foo' , values = ('bar' , 'baz' ))
534
+ else :
535
+ pd .pivot (df , index = 'zoo' , columns = 'foo' , values = ('bar' , 'baz' ))
493
536
494
537
def test_margins (self ):
495
538
def _check_output (result , values_col , index = ['A' , 'B' ],
0 commit comments