Skip to content

Commit 1ed2132

Browse files
committed
Remove downcasting and update tests to expect floats
1 parent 317290a commit 1ed2132

File tree

2 files changed

+29
-51
lines changed

2 files changed

+29
-51
lines changed

pandas/core/reshape/pivot.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -172,28 +172,6 @@ def __internal_pivot_table(
172172
if dropna and isinstance(agged, ABCDataFrame) and len(agged.columns):
173173
agged = agged.dropna(how="all")
174174

175-
# gh-21133
176-
# we want to down cast if
177-
# the original values are ints
178-
# as we grouped with a NaN value
179-
# and then dropped, coercing to floats
180-
for v in values:
181-
if (
182-
v in data
183-
and is_integer_dtype(data[v])
184-
and v in agged
185-
and not is_integer_dtype(agged[v])
186-
):
187-
if not isinstance(agged[v], ABCDataFrame) and isinstance(
188-
data[v].dtype, np.dtype
189-
):
190-
# exclude DataFrame case bc maybe_downcast_to_dtype expects
191-
# ArrayLike
192-
# e.g. test_pivot_table_multiindex_columns_doctest_case
193-
# agged.columns is a MultiIndex and 'v' is indexing only
194-
# on its first level.
195-
agged[v] = maybe_downcast_to_dtype(agged[v], data[v].dtype)
196-
197175
table = agged
198176

199177
# GH17038, this check should only happen if index is defined (not None)

pandas/tests/reshape/test_pivot.py

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def test_pivot_table_categorical(self):
204204
result = pivot_table(df, values="values", index=["A", "B"], dropna=True)
205205

206206
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)
208208
tm.assert_frame_equal(result, expected)
209209

210210
def test_pivot_table_dropna_categoricals(self, dropna):
@@ -225,7 +225,7 @@ def test_pivot_table_dropna_categoricals(self, dropna):
225225
expected_columns = expected_columns.astype(CDT(categories, ordered=False))
226226
expected_index = Series([1, 2, 3], name="B")
227227
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]],
229229
index=expected_index,
230230
columns=expected_columns,
231231
)
@@ -283,7 +283,7 @@ def test_pivot_with_non_observable_dropna_multi_cat(self, dropna):
283283

284284
result = df.pivot_table(index="A", values="B", dropna=dropna)
285285
expected = DataFrame(
286-
{"B": [2, 3, 0]},
286+
{"B": [2.0, 3.0, 0.0]},
287287
index=Index(
288288
Categorical.from_codes(
289289
[0, 1, 2], categories=["low", "high", "left"], ordered=True
@@ -300,7 +300,7 @@ def test_pivot_with_interval_index(self, interval_values, dropna):
300300
# GH 25814
301301
df = DataFrame({"A": interval_values, "B": 1})
302302
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"))
304304
if not dropna:
305305
expected = expected.astype(float)
306306
tm.assert_frame_equal(result, expected)
@@ -444,7 +444,7 @@ def test_pivot_no_values(self):
444444
index=Grouper(freq="A"), columns=Grouper(key="dt", freq="M")
445445
)
446446
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
448448
)
449449
tm.assert_frame_equal(res, exp)
450450

@@ -1059,7 +1059,7 @@ def test_pivot_table_multiindex_only(self, cols):
10591059

10601060
result = df2.pivot_table(values="v", columns=cols)
10611061
expected = DataFrame(
1062-
[[4, 5, 6]],
1062+
[[4.0, 5.0, 6.0]],
10631063
columns=MultiIndex.from_tuples([(1, 1), (2, 2), (3, 3)], names=cols),
10641064
index=Index(["v"]),
10651065
)
@@ -1558,7 +1558,7 @@ def test_pivot_datetime_tz(self):
15581558
exp_col1 = Index(["value1", "value1"])
15591559
exp_col2 = Index(["a", "b"], name="label")
15601560
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)
15621562
result = pivot_table(df, index=["dt1"], columns=["label"], values=["value1"])
15631563
tm.assert_frame_equal(result, expected)
15641564

@@ -1570,18 +1570,30 @@ def test_pivot_datetime_tz(self):
15701570
name="dt2",
15711571
)
15721572
exp_col = MultiIndex.from_arrays([exp_col1, exp_col2, exp_col3])
1573-
expected = DataFrame(
1573+
expected1 = DataFrame(
15741574
np.array(
15751575
[
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],
15791579
],
15801580
dtype="int64",
15811581
),
15821582
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:],
15841595
)
1596+
expected = concat([expected1, expected2], axis=1)
15851597

15861598
result = pivot_table(
15871599
df,
@@ -1628,7 +1640,7 @@ def test_pivot_dtaccessor(self):
16281640

16291641
exp_idx = Index(["a", "b"], name="label")
16301642
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]},
16321644
index=exp_idx,
16331645
columns=Index([7, 8, 9], dtype=np.int32, name="dt1"),
16341646
)
@@ -1639,7 +1651,7 @@ def test_pivot_dtaccessor(self):
16391651
)
16401652

16411653
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]},
16431655
index=Index([1, 2], dtype=np.int32, name="dt2"),
16441656
columns=Index([7, 8, 9], dtype=np.int32, name="dt1"),
16451657
)
@@ -1660,7 +1672,7 @@ def test_pivot_dtaccessor(self):
16601672
names=["dt1", "dt2"],
16611673
)
16621674
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]]),
16641676
index=Index([2013], dtype=np.int32),
16651677
columns=exp_col,
16661678
)
@@ -1765,12 +1777,6 @@ def test_pivot_table_margins_name_with_aggfunc_list(self):
17651777
tm.assert_frame_equal(table, expected)
17661778

17671779
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-
)
17741780
# GH 10989
17751781
df = DataFrame(
17761782
{"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):
17841790
tm.assert_frame_equal(table, expected)
17851791

17861792
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-
)
17931793
df = DataFrame(
17941794
{"x": np.arange(8), "y": np.arange(8) // 4, "z": np.arange(8) % 2}
17951795
)
@@ -1816,7 +1816,7 @@ def test_margins_casted_to_float(self):
18161816

18171817
result = pivot_table(df, index="D", margins=True)
18181818
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]},
18201820
index=Index(["X", "Y", "All"], name="D"),
18211821
)
18221822
tm.assert_frame_equal(result, expected)
@@ -2249,7 +2249,7 @@ def test_pivot_table_sort_false_with_multiple_values(self):
22492249
index=["lastname", "firstname"], values=["height", "age"], sort=False
22502250
)
22512251
expected = DataFrame(
2252-
[[173, 47], [182, 33]],
2252+
[[173.0, 47.0], [182.0, 33.0]],
22532253
columns=["height", "age"],
22542254
index=MultiIndex.from_tuples(
22552255
[("Foo", "John"), ("Bar", "Michael")],

0 commit comments

Comments
 (0)