Skip to content

Commit bfd46cc

Browse files
phoflmroeschke
authored andcommitted
Adjust pivot tests for new string option (pandas-dev#56529)
* Adjust pivot tests for new string option * BUG: pivot dropping wrong column level with numeric columns and ea dtype * Adjust pivot tests for new string option * Fixup --------- Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 97e8926 commit bfd46cc

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

pandas/tests/reshape/test_pivot.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import numpy as np
1010
import pytest
1111

12+
from pandas._config import using_pyarrow_string_dtype
13+
1214
from pandas.errors import PerformanceWarning
1315

1416
import pandas as pd
@@ -781,7 +783,8 @@ def test_pivot_with_list_like_values(self, values, method):
781783
codes=[[0, 0, 0, 1, 1, 1], [0, 1, 2, 0, 1, 2]],
782784
names=[None, "bar"],
783785
)
784-
expected = DataFrame(data=data, index=index, columns=columns, dtype="object")
786+
expected = DataFrame(data=data, index=index, columns=columns)
787+
expected["baz"] = expected["baz"].astype(object)
785788
tm.assert_frame_equal(result, expected)
786789

787790
@pytest.mark.parametrize(
@@ -824,7 +827,8 @@ def test_pivot_with_list_like_values_nans(self, values, method):
824827
codes=[[0, 0, 1, 1], [0, 1, 0, 1]],
825828
names=[None, "foo"],
826829
)
827-
expected = DataFrame(data=data, index=index, columns=columns, dtype="object")
830+
expected = DataFrame(data=data, index=index, columns=columns)
831+
expected["baz"] = expected["baz"].astype(object)
828832
tm.assert_frame_equal(result, expected)
829833

830834
def test_pivot_columns_none_raise_error(self):
@@ -949,7 +953,7 @@ def test_no_col(self, data):
949953

950954
# to help with a buglet
951955
data.columns = [k * 2 for k in data.columns]
952-
msg = re.escape("agg function failed [how->mean,dtype->object]")
956+
msg = re.escape("agg function failed [how->mean,dtype->")
953957
with pytest.raises(TypeError, match=msg):
954958
data.pivot_table(index=["AA", "BB"], margins=True, aggfunc="mean")
955959
table = data.drop(columns="CC").pivot_table(
@@ -1022,7 +1026,7 @@ def test_margin_with_only_columns_defined(
10221026
}
10231027
)
10241028
if aggfunc != "sum":
1025-
msg = re.escape("agg function failed [how->mean,dtype->object]")
1029+
msg = re.escape("agg function failed [how->mean,dtype->")
10261030
with pytest.raises(TypeError, match=msg):
10271031
df.pivot_table(columns=columns, margins=True, aggfunc=aggfunc)
10281032
if "B" not in columns:
@@ -1086,7 +1090,7 @@ def test_pivot_table_multiindex_only(self, cols):
10861090
expected = DataFrame(
10871091
[[4.0, 5.0, 6.0]],
10881092
columns=MultiIndex.from_tuples([(1, 1), (2, 2), (3, 3)], names=cols),
1089-
index=Index(["v"]),
1093+
index=Index(["v"], dtype=object),
10901094
)
10911095

10921096
tm.assert_frame_equal(result, expected)
@@ -1803,7 +1807,7 @@ def test_pivot_table_margins_name_with_aggfunc_list(self):
18031807
margins_name=margins_name,
18041808
aggfunc=["mean", "max"],
18051809
)
1806-
ix = Index(["bacon", "cheese", margins_name], dtype="object", name="item")
1810+
ix = Index(["bacon", "cheese", margins_name], name="item")
18071811
tups = [
18081812
("mean", "cost", "ME"),
18091813
("mean", "cost", "T"),
@@ -1995,7 +1999,7 @@ def test_pivot_table_not_series(self):
19951999
def test_pivot_margins_name_unicode(self):
19962000
# issue #13292
19972001
greek = "\u0394\u03bf\u03ba\u03b9\u03bc\u03ae"
1998-
frame = DataFrame({"foo": [1, 2, 3]})
2002+
frame = DataFrame({"foo": [1, 2, 3]}, columns=Index(["foo"], dtype=object))
19992003
table = pivot_table(
20002004
frame, index=["foo"], aggfunc=len, margins=True, margins_name=greek
20012005
)
@@ -2607,6 +2611,7 @@ def test_pivot_columns_not_given(self):
26072611
with pytest.raises(TypeError, match="missing 1 required keyword-only argument"):
26082612
df.pivot() # pylint: disable=missing-kwoa
26092613

2614+
@pytest.mark.xfail(using_pyarrow_string_dtype(), reason="None is cast to NaN")
26102615
def test_pivot_columns_is_none(self):
26112616
# GH#48293
26122617
df = DataFrame({None: [1], "b": 2, "c": 3})
@@ -2622,6 +2627,7 @@ def test_pivot_columns_is_none(self):
26222627
expected = DataFrame({1: 3}, index=Index([2], name="b"))
26232628
tm.assert_frame_equal(result, expected)
26242629

2630+
@pytest.mark.xfail(using_pyarrow_string_dtype(), reason="None is cast to NaN")
26252631
def test_pivot_index_is_none(self):
26262632
# GH#48293
26272633
df = DataFrame({None: [1], "b": 2, "c": 3})
@@ -2635,6 +2641,7 @@ def test_pivot_index_is_none(self):
26352641
expected = DataFrame(3, index=[1], columns=Index([2], name="b"))
26362642
tm.assert_frame_equal(result, expected)
26372643

2644+
@pytest.mark.xfail(using_pyarrow_string_dtype(), reason="None is cast to NaN")
26382645
def test_pivot_values_is_none(self):
26392646
# GH#48293
26402647
df = DataFrame({None: [1], "b": 2, "c": 3})

pandas/tests/reshape/test_union_categoricals.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def test_union_categoricals_nan(self):
110110
res = union_categoricals(
111111
[
112112
Categorical(np.array([np.nan, np.nan], dtype=object)),
113-
Categorical(["X"]),
113+
Categorical(["X"], categories=pd.Index(["X"], dtype=object)),
114114
]
115115
)
116116
exp = Categorical([np.nan, np.nan, "X"])
@@ -123,8 +123,10 @@ def test_union_categoricals_nan(self):
123123
tm.assert_categorical_equal(res, exp)
124124

125125
@pytest.mark.parametrize("val", [[], ["1"]])
126-
def test_union_categoricals_empty(self, val):
126+
def test_union_categoricals_empty(self, val, request, using_infer_string):
127127
# GH 13759
128+
if using_infer_string and val == ["1"]:
129+
request.applymarker(pytest.mark.xfail("object and strings dont match"))
128130
res = union_categoricals([Categorical([]), Categorical(val)])
129131
exp = Categorical(val)
130132
tm.assert_categorical_equal(res, exp)

0 commit comments

Comments
 (0)