@@ -204,7 +204,7 @@ def test_pivot_table_categorical(self):
204
204
result = pivot_table (df , values = "values" , index = ["A" , "B" ], dropna = True )
205
205
206
206
exp_index = MultiIndex .from_arrays ([cat1 , cat2 ], names = ["A" , "B" ])
207
- expected = DataFrame ({"values" : [1 , 2 , 3 , 4 ]}, index = exp_index )
207
+ expected = DataFrame ({"values" : [1.0 , 2.0 , 3.0 , 4.0 ]}, index = exp_index )
208
208
tm .assert_frame_equal (result , expected )
209
209
210
210
def test_pivot_table_dropna_categoricals (self , dropna ):
@@ -225,7 +225,7 @@ def test_pivot_table_dropna_categoricals(self, dropna):
225
225
expected_columns = expected_columns .astype (CDT (categories , ordered = False ))
226
226
expected_index = Series ([1 , 2 , 3 ], name = "B" )
227
227
expected = DataFrame (
228
- [[0 , 3 , 6 ], [1 , 4 , 7 ], [2 , 5 , 8 ]],
228
+ [[0.0 , 3.0 , 6.0 ], [1.0 , 4.0 , 7.0 ], [2.0 , 5.0 , 8.0 ]],
229
229
index = expected_index ,
230
230
columns = expected_columns ,
231
231
)
@@ -283,7 +283,7 @@ def test_pivot_with_non_observable_dropna_multi_cat(self, dropna):
283
283
284
284
result = df .pivot_table (index = "A" , values = "B" , dropna = dropna )
285
285
expected = DataFrame (
286
- {"B" : [2 , 3 , 0 ]},
286
+ {"B" : [2.0 , 3.0 , 0. 0 ]},
287
287
index = Index (
288
288
Categorical .from_codes (
289
289
[0 , 1 , 2 ], categories = ["low" , "high" , "left" ], ordered = True
@@ -300,7 +300,7 @@ def test_pivot_with_interval_index(self, interval_values, dropna):
300
300
# GH 25814
301
301
df = DataFrame ({"A" : interval_values , "B" : 1 })
302
302
result = df .pivot_table (index = "A" , values = "B" , dropna = dropna )
303
- expected = DataFrame ({"B" : 1 }, index = Index (interval_values .unique (), name = "A" ))
303
+ expected = DataFrame ({"B" : 1.0 }, index = Index (interval_values .unique (), name = "A" ))
304
304
if not dropna :
305
305
expected = expected .astype (float )
306
306
tm .assert_frame_equal (result , expected )
@@ -444,7 +444,7 @@ def test_pivot_no_values(self):
444
444
index = Grouper (freq = "A" ), columns = Grouper (key = "dt" , freq = "M" )
445
445
)
446
446
exp = DataFrame (
447
- [3 ], index = pd .DatetimeIndex (["2011-12-31" ], freq = "A" ), columns = exp_columns
447
+ [3.0 ], index = pd .DatetimeIndex (["2011-12-31" ], freq = "A" ), columns = exp_columns
448
448
)
449
449
tm .assert_frame_equal (res , exp )
450
450
@@ -1059,7 +1059,7 @@ def test_pivot_table_multiindex_only(self, cols):
1059
1059
1060
1060
result = df2 .pivot_table (values = "v" , columns = cols )
1061
1061
expected = DataFrame (
1062
- [[4 , 5 , 6 ]],
1062
+ [[4.0 , 5.0 , 6.0 ]],
1063
1063
columns = MultiIndex .from_tuples ([(1 , 1 ), (2 , 2 ), (3 , 3 )], names = cols ),
1064
1064
index = Index (["v" ]),
1065
1065
)
@@ -1558,7 +1558,7 @@ def test_pivot_datetime_tz(self):
1558
1558
exp_col1 = Index (["value1" , "value1" ])
1559
1559
exp_col2 = Index (["a" , "b" ], name = "label" )
1560
1560
exp_col = MultiIndex .from_arrays ([exp_col1 , exp_col2 ])
1561
- expected = DataFrame ([[0 , 3 ], [1 , 4 ], [2 , 5 ]], index = exp_idx , columns = exp_col )
1561
+ expected = DataFrame ([[0.0 , 3.0 ], [1.0 , 4.0 ], [2.0 , 5.0 ]], index = exp_idx , columns = exp_col )
1562
1562
result = pivot_table (df , index = ["dt1" ], columns = ["label" ], values = ["value1" ])
1563
1563
tm .assert_frame_equal (result , expected )
1564
1564
@@ -1570,18 +1570,30 @@ def test_pivot_datetime_tz(self):
1570
1570
name = "dt2" ,
1571
1571
)
1572
1572
exp_col = MultiIndex .from_arrays ([exp_col1 , exp_col2 , exp_col3 ])
1573
- expected = DataFrame (
1573
+ expected1 = DataFrame (
1574
1574
np .array (
1575
1575
[
1576
- [0 , 3 , 1 , 2 , 0 , 3 , 1 , 2 ],
1577
- [1 , 4 , 2 , 1 , 1 , 4 , 2 , 1 ],
1578
- [2 , 5 , 1 , 2 , 2 , 5 , 1 , 2 ],
1576
+ [0 , 3 , 1 , 2 ,],
1577
+ [1 , 4 , 2 , 1 ],
1578
+ [2 , 5 , 1 , 2 ],
1579
1579
],
1580
1580
dtype = "int64" ,
1581
1581
),
1582
1582
index = exp_idx ,
1583
- columns = exp_col ,
1583
+ columns = exp_col [:4 ],
1584
+ )
1585
+ expected2 = DataFrame (
1586
+ np .array (
1587
+ [
1588
+ [0.0 , 3.0 , 1.0 , 2.0 ],
1589
+ [1.0 , 4.0 , 2.0 , 1.0 ],
1590
+ [2.0 , 5.0 , 1.0 , 2.0 ],
1591
+ ],
1592
+ ),
1593
+ index = exp_idx ,
1594
+ columns = exp_col [4 :],
1584
1595
)
1596
+ expected = concat ([expected1 , expected2 ], axis = 1 )
1585
1597
1586
1598
result = pivot_table (
1587
1599
df ,
@@ -1628,7 +1640,7 @@ def test_pivot_dtaccessor(self):
1628
1640
1629
1641
exp_idx = Index (["a" , "b" ], name = "label" )
1630
1642
expected = DataFrame (
1631
- {7 : [0 , 3 ], 8 : [1 , 4 ], 9 : [2 , 5 ]},
1643
+ {7 : [0.0 , 3.0 ], 8 : [1.0 , 4.0 ], 9 : [2.0 , 5.0 ]},
1632
1644
index = exp_idx ,
1633
1645
columns = Index ([7 , 8 , 9 ], dtype = np .int32 , name = "dt1" ),
1634
1646
)
@@ -1639,7 +1651,7 @@ def test_pivot_dtaccessor(self):
1639
1651
)
1640
1652
1641
1653
expected = DataFrame (
1642
- {7 : [0 , 3 ], 8 : [1 , 4 ], 9 : [2 , 5 ]},
1654
+ {7 : [0.0 , 3.0 ], 8 : [1.0 , 4.0 ], 9 : [2.0 , 5.0 ]},
1643
1655
index = Index ([1 , 2 ], dtype = np .int32 , name = "dt2" ),
1644
1656
columns = Index ([7 , 8 , 9 ], dtype = np .int32 , name = "dt1" ),
1645
1657
)
@@ -1660,7 +1672,7 @@ def test_pivot_dtaccessor(self):
1660
1672
names = ["dt1" , "dt2" ],
1661
1673
)
1662
1674
expected = DataFrame (
1663
- np .array ([[0 , 3 , 1 , 4 , 2 , 5 ]], dtype = "int64" ),
1675
+ np .array ([[0.0 , 3.0 , 1.0 , 4.0 , 2.0 , 5.0 ]] ),
1664
1676
index = Index ([2013 ], dtype = np .int32 ),
1665
1677
columns = exp_col ,
1666
1678
)
@@ -1765,12 +1777,6 @@ def test_pivot_table_margins_name_with_aggfunc_list(self):
1765
1777
tm .assert_frame_equal (table , expected )
1766
1778
1767
1779
def test_categorical_margins (self , observed , request ):
1768
- if observed :
1769
- request .node .add_marker (
1770
- pytest .mark .xfail (
1771
- reason = "GH#17035 (np.mean of ints is casted back to ints)"
1772
- )
1773
- )
1774
1780
# GH 10989
1775
1781
df = DataFrame (
1776
1782
{"x" : np .arange (8 ), "y" : np .arange (8 ) // 4 , "z" : np .arange (8 ) % 2 }
@@ -1784,12 +1790,6 @@ def test_categorical_margins(self, observed, request):
1784
1790
tm .assert_frame_equal (table , expected )
1785
1791
1786
1792
def test_categorical_margins_category (self , observed , request ):
1787
- if observed :
1788
- request .node .add_marker (
1789
- pytest .mark .xfail (
1790
- reason = "GH#17035 (np.mean of ints is casted back to ints)"
1791
- )
1792
- )
1793
1793
df = DataFrame (
1794
1794
{"x" : np .arange (8 ), "y" : np .arange (8 ) // 4 , "z" : np .arange (8 ) % 2 }
1795
1795
)
@@ -1816,7 +1816,7 @@ def test_margins_casted_to_float(self):
1816
1816
1817
1817
result = pivot_table (df , index = "D" , margins = True )
1818
1818
expected = DataFrame (
1819
- {"A" : [3 , 7 , 5 ], "B" : [2.5 , 6.5 , 4.5 ], "C" : [2 , 5 , 3.5 ]},
1819
+ {"A" : [3.0 , 7.0 , 5 ], "B" : [2.5 , 6.5 , 4.5 ], "C" : [2.0 , 5.0 , 3.5 ]},
1820
1820
index = Index (["X" , "Y" , "All" ], name = "D" ),
1821
1821
)
1822
1822
tm .assert_frame_equal (result , expected )
@@ -2249,7 +2249,7 @@ def test_pivot_table_sort_false_with_multiple_values(self):
2249
2249
index = ["lastname" , "firstname" ], values = ["height" , "age" ], sort = False
2250
2250
)
2251
2251
expected = DataFrame (
2252
- [[173 , 47 ], [182 , 33 ]],
2252
+ [[173.0 , 47.0 ], [182.0 , 33.0 ]],
2253
2253
columns = ["height" , "age" ],
2254
2254
index = MultiIndex .from_tuples (
2255
2255
[("Foo" , "John" ), ("Bar" , "Michael" )],
0 commit comments