Skip to content

Commit 91dfbee

Browse files
committed
TST: update expecteds for using_string_dtype to fix xfails
1 parent 35b0d1d commit 91dfbee

File tree

6 files changed

+77
-48
lines changed

6 files changed

+77
-48
lines changed

pandas/tests/arrays/categorical/test_constructors.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
import numpy as np
77
import pytest
88

9-
from pandas._config import using_string_dtype
10-
11-
from pandas.compat import HAS_PYARROW
12-
139
from pandas.core.dtypes.common import (
1410
is_float_dtype,
1511
is_integer_dtype,
@@ -444,13 +440,12 @@ def test_constructor_str_unknown(self):
444440
with pytest.raises(ValueError, match="Unknown dtype"):
445441
Categorical([1, 2], dtype="foo")
446442

447-
@pytest.mark.xfail(
448-
using_string_dtype() and HAS_PYARROW, reason="Can't be NumPy strings"
449-
)
450443
def test_constructor_np_strs(self):
451444
# GH#31499 Hashtable.map_locations needs to work on np.str_ objects
452-
cat = Categorical(["1", "0", "1"], [np.str_("0"), np.str_("1")])
453-
assert all(isinstance(x, np.str_) for x in cat.categories)
445+
# We can't pass all-strings because the constructor would cast
446+
# those to StringDtype post-PDEP14
447+
cat = Categorical(["1", "0", "1", 2], [np.str_("0"), np.str_("1"), 2])
448+
assert all(isinstance(x, (np.str_, int)) for x in cat.categories)
454449

455450
def test_constructor_from_categorical_with_dtype(self):
456451
dtype = CategoricalDtype(["a", "b", "c"], ordered=True)

pandas/tests/arrays/categorical/test_repr.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
import numpy as np
2-
import pytest
3-
4-
from pandas._config import using_string_dtype
52

63
from pandas import (
74
Categorical,
@@ -77,17 +74,19 @@ def test_print_none_width(self):
7774
with option_context("display.width", None):
7875
assert exp == repr(a)
7976

80-
@pytest.mark.skipif(
81-
using_string_dtype(),
82-
reason="Change once infer_string is set to True by default",
83-
)
84-
def test_unicode_print(self):
77+
def test_unicode_print(self, using_infer_string):
8578
c = Categorical(["aaaaa", "bb", "cccc"] * 20)
8679
expected = """\
8780
['aaaaa', 'bb', 'cccc', 'aaaaa', 'bb', ..., 'bb', 'cccc', 'aaaaa', 'bb', 'cccc']
8881
Length: 60
8982
Categories (3, object): ['aaaaa', 'bb', 'cccc']"""
9083

84+
if using_infer_string:
85+
expected = expected.replace(
86+
"(3, object): ['aaaaa', 'bb', 'cccc']",
87+
"(3, str): [aaaaa, bb, cccc]",
88+
)
89+
9190
assert repr(c) == expected
9291

9392
c = Categorical(["ああああ", "いいいいい", "ううううううう"] * 20)
@@ -96,6 +95,12 @@ def test_unicode_print(self):
9695
Length: 60
9796
Categories (3, object): ['ああああ', 'いいいいい', 'ううううううう']""" # noqa: E501
9897

98+
if using_infer_string:
99+
expected = expected.replace(
100+
"(3, object): ['ああああ', 'いいいいい', 'ううううううう']",
101+
"(3, str): [ああああ, いいいいい, ううううううう]",
102+
)
103+
99104
assert repr(c) == expected
100105

101106
# unicode option should not affect to Categorical, as it doesn't care
@@ -106,6 +111,12 @@ def test_unicode_print(self):
106111
Length: 60
107112
Categories (3, object): ['ああああ', 'いいいいい', 'ううううううう']""" # noqa: E501
108113

114+
if using_infer_string:
115+
expected = expected.replace(
116+
"(3, object): ['ああああ', 'いいいいい', 'ううううううう']",
117+
"(3, str): [ああああ, いいいいい, ううううううう]",
118+
)
119+
109120
assert repr(c) == expected
110121

111122
def test_categorical_repr(self):

pandas/tests/frame/methods/test_astype.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import numpy as np
44
import pytest
55

6-
from pandas._config import using_string_dtype
7-
86
import pandas.util._test_decorators as td
97

108
import pandas as pd
@@ -745,10 +743,7 @@ def test_astype_tz_object_conversion(self, tz):
745743
result = result.astype({"tz": "datetime64[ns, Europe/London]"})
746744
tm.assert_frame_equal(result, expected)
747745

748-
@pytest.mark.xfail(using_string_dtype(), reason="TODO(infer_string) GH#60639")
749-
def test_astype_dt64_to_string(
750-
self, frame_or_series, tz_naive_fixture, using_infer_string
751-
):
746+
def test_astype_dt64_to_string(self, frame_or_series, tz_naive_fixture):
752747
# GH#41409
753748
tz = tz_naive_fixture
754749

@@ -766,10 +761,7 @@ def test_astype_dt64_to_string(
766761
item = result.iloc[0]
767762
if frame_or_series is DataFrame:
768763
item = item.iloc[0]
769-
if using_infer_string:
770-
assert item is np.nan
771-
else:
772-
assert item is pd.NA
764+
assert item is pd.NA
773765

774766
# For non-NA values, we should match what we get for non-EA str
775767
alt = obj.astype(str)

pandas/tests/groupby/test_timegrouper.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
import numpy as np
1212
import pytest
1313

14-
from pandas._config import using_string_dtype
15-
1614
import pandas as pd
1715
from pandas import (
1816
DataFrame,
@@ -76,10 +74,7 @@ def groupby_with_truncated_bingrouper(frame_for_truncated_bingrouper):
7674

7775

7876
class TestGroupBy:
79-
# TODO(infer_string) resample sum introduces 0's
80-
# https://github.com/pandas-dev/pandas/issues/60229
81-
@pytest.mark.xfail(using_string_dtype(), reason="TODO(infer_string)")
82-
def test_groupby_with_timegrouper(self):
77+
def test_groupby_with_timegrouper(self, using_infer_string):
8378
# GH 4161
8479
# TimeGrouper requires a sorted index
8580
# also verifies that the resultant index has the correct name
@@ -116,8 +111,11 @@ def test_groupby_with_timegrouper(self):
116111
{"Buyer": 0, "Quantity": 0},
117112
index=exp_dti,
118113
)
119-
# Cast to object to avoid implicit cast when setting entry to "CarlCarlCarl"
114+
# Cast to object/str to avoid implicit cast when setting
115+
# entry to "CarlCarlCarl"
120116
expected = expected.astype({"Buyer": object})
117+
if using_infer_string:
118+
expected = expected.astype({"Buyer": "str"})
121119
expected.iloc[0, 0] = "CarlCarlCarl"
122120
expected.iloc[6, 0] = "CarlCarl"
123121
expected.iloc[18, 0] = "Joe"

pandas/tests/indexes/base_class/test_formats.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import numpy as np
22
import pytest
33

4-
from pandas._config import using_string_dtype
54
import pandas._config.config as cf
65

76
from pandas import Index
@@ -16,7 +15,6 @@ def test_repr_is_valid_construction_code(self):
1615
res = eval(repr(idx))
1716
tm.assert_index_equal(res, idx)
1817

19-
@pytest.mark.xfail(using_string_dtype(), reason="repr different")
2018
@pytest.mark.parametrize(
2119
"index,expected",
2220
[
@@ -77,11 +75,13 @@ def test_repr_is_valid_construction_code(self):
7775
),
7876
],
7977
)
80-
def test_string_index_repr(self, index, expected):
78+
def test_string_index_repr(self, index, expected, using_infer_string):
8179
result = repr(index)
80+
if using_infer_string:
81+
expected = expected.replace("dtype='object'", "dtype='str'")
82+
8283
assert result == expected
8384

84-
@pytest.mark.xfail(using_string_dtype(), reason="repr different")
8585
@pytest.mark.parametrize(
8686
"index,expected",
8787
[
@@ -121,11 +121,16 @@ def test_string_index_repr(self, index, expected):
121121
),
122122
],
123123
)
124-
def test_string_index_repr_with_unicode_option(self, index, expected):
124+
def test_string_index_repr_with_unicode_option(
125+
self, index, expected, using_infer_string
126+
):
125127
# Enable Unicode option -----------------------------------------
126128
with cf.option_context("display.unicode.east_asian_width", True):
127129
result = repr(index)
128-
assert result == expected
130+
131+
if using_infer_string:
132+
expected = expected.replace("dtype='object'", "dtype='str'")
133+
assert result == expected
129134

130135
def test_repr_summary(self):
131136
with cf.option_context("display.max_seq_items", 10):

pandas/tests/io/formats/test_format.py

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
import numpy as np
1212
import pytest
1313

14-
from pandas._config import using_string_dtype
15-
1614
import pandas as pd
1715
from pandas import (
1816
DataFrame,
@@ -1395,8 +1393,7 @@ def test_unicode_name_in_footer(self):
13951393
sf = fmt.SeriesFormatter(s, name="\u05e2\u05d1\u05e8\u05d9\u05ea")
13961394
sf._get_footer() # should not raise exception
13971395

1398-
@pytest.mark.xfail(using_string_dtype(), reason="Fixup when arrow is default")
1399-
def test_east_asian_unicode_series(self):
1396+
def test_east_asian_unicode_series(self, using_infer_string):
14001397
# not aligned properly because of east asian width
14011398

14021399
# unicode index
@@ -1409,6 +1406,8 @@ def test_east_asian_unicode_series(self):
14091406
"ええええ D\ndtype: object",
14101407
]
14111408
)
1409+
if using_infer_string:
1410+
expected = expected.replace("dtype: object", "dtype: str")
14121411
assert repr(s) == expected
14131412

14141413
# unicode values
@@ -1422,7 +1421,8 @@ def test_east_asian_unicode_series(self):
14221421
"dtype: object",
14231422
]
14241423
)
1425-
1424+
if using_infer_string:
1425+
expected = expected.replace("dtype: object", "dtype: str")
14261426
assert repr(s) == expected
14271427

14281428
# both
@@ -1439,7 +1439,8 @@ def test_east_asian_unicode_series(self):
14391439
"dtype: object",
14401440
]
14411441
)
1442-
1442+
if using_infer_string:
1443+
expected = expected.replace("dtype: object", "dtype: str")
14431444
assert repr(s) == expected
14441445

14451446
# unicode footer
@@ -1452,6 +1453,8 @@ def test_east_asian_unicode_series(self):
14521453
"ああ あ\nいいいい いい\nう ううう\n"
14531454
"えええ ええええ\nName: おおおおおおお, dtype: object"
14541455
)
1456+
if using_infer_string:
1457+
expected = expected.replace("dtype: object", "dtype: str")
14551458
assert repr(s) == expected
14561459

14571460
# MultiIndex
@@ -1495,6 +1498,8 @@ def test_east_asian_unicode_series(self):
14951498
"3 ええええ\n"
14961499
"Name: おおおおおおお, Length: 4, dtype: object"
14971500
)
1501+
if using_infer_string:
1502+
expected = expected.replace("dtype: object", "dtype: str")
14981503
assert repr(s) == expected
14991504

15001505
s.index = ["ああ", "いいいい", "う", "えええ"]
@@ -1503,6 +1508,8 @@ def test_east_asian_unicode_series(self):
15031508
"えええ ええええ\n"
15041509
"Name: おおおおおおお, Length: 4, dtype: object"
15051510
)
1511+
if using_infer_string:
1512+
expected = expected.replace("dtype: object", "dtype: str")
15061513
assert repr(s) == expected
15071514

15081515
# Enable Unicode option -----------------------------------------
@@ -1516,6 +1523,8 @@ def test_east_asian_unicode_series(self):
15161523
"あ a\nいい bb\nううう CCC\n"
15171524
"ええええ D\ndtype: object"
15181525
)
1526+
if using_infer_string:
1527+
expected = expected.replace("dtype: object", "dtype: str")
15191528
assert repr(s) == expected
15201529

15211530
# unicode values
@@ -1527,6 +1536,8 @@ def test_east_asian_unicode_series(self):
15271536
"a あ\nbb いい\nc ううう\n"
15281537
"ddd ええええ\ndtype: object"
15291538
)
1539+
if using_infer_string:
1540+
expected = expected.replace("dtype: object", "dtype: str")
15301541
assert repr(s) == expected
15311542
# both
15321543
s = Series(
@@ -1539,6 +1550,8 @@ def test_east_asian_unicode_series(self):
15391550
"う ううう\n"
15401551
"えええ ええええ\ndtype: object"
15411552
)
1553+
if using_infer_string:
1554+
expected = expected.replace("dtype: object", "dtype: str")
15421555
assert repr(s) == expected
15431556

15441557
# unicode footer
@@ -1554,6 +1567,8 @@ def test_east_asian_unicode_series(self):
15541567
"えええ ええええ\n"
15551568
"Name: おおおおおおお, dtype: object"
15561569
)
1570+
if using_infer_string:
1571+
expected = expected.replace("dtype: object", "dtype: str")
15571572
assert repr(s) == expected
15581573

15591574
# MultiIndex
@@ -1599,6 +1614,8 @@ def test_east_asian_unicode_series(self):
15991614
"3 ええええ\n"
16001615
"Name: おおおおおおお, Length: 4, dtype: object"
16011616
)
1617+
if using_infer_string:
1618+
expected = expected.replace("dtype: object", "dtype: str")
16021619
assert repr(s) == expected
16031620

16041621
s.index = ["ああ", "いいいい", "う", "えええ"]
@@ -1608,6 +1625,8 @@ def test_east_asian_unicode_series(self):
16081625
"えええ ええええ\n"
16091626
"Name: おおおおおおお, Length: 4, dtype: object"
16101627
)
1628+
if using_infer_string:
1629+
expected = expected.replace("dtype: object", "dtype: str")
16111630
assert repr(s) == expected
16121631

16131632
# ambiguous unicode
@@ -1621,6 +1640,8 @@ def test_east_asian_unicode_series(self):
16211640
"¡¡ ううう\n"
16221641
"えええ ええええ\ndtype: object"
16231642
)
1643+
if using_infer_string:
1644+
expected = expected.replace("dtype: object", "dtype: str")
16241645
assert repr(s) == expected
16251646

16261647
def test_float_trim_zeros(self):
@@ -1770,27 +1791,34 @@ def chck_ncols(self, s):
17701791
ncolsizes = len({len(line.strip()) for line in lines})
17711792
assert ncolsizes == 1
17721793

1773-
@pytest.mark.xfail(using_string_dtype(), reason="change when arrow is default")
1774-
def test_format_explicit(self):
1794+
def test_format_explicit(self, using_infer_string):
17751795
test_sers = gen_series_formatting()
17761796
with option_context("display.max_rows", 4, "display.show_dimensions", False):
17771797
res = repr(test_sers["onel"])
17781798
exp = "0 a\n1 a\n ..\n98 a\n99 a\ndtype: object"
1799+
if using_infer_string:
1800+
exp = exp.replace("dtype: object", "dtype: str")
17791801
assert exp == res
17801802
res = repr(test_sers["twol"])
17811803
exp = "0 ab\n1 ab\n ..\n98 ab\n99 ab\ndtype: object"
1804+
if using_infer_string:
1805+
exp = exp.replace("dtype: object", "dtype: str")
17821806
assert exp == res
17831807
res = repr(test_sers["asc"])
17841808
exp = (
17851809
"0 a\n1 ab\n ... \n4 abcde\n5 "
17861810
"abcdef\ndtype: object"
17871811
)
1812+
if using_infer_string:
1813+
exp = exp.replace("dtype: object", "dtype: str")
17881814
assert exp == res
17891815
res = repr(test_sers["desc"])
17901816
exp = (
17911817
"5 abcdef\n4 abcde\n ... \n1 ab\n0 "
17921818
"a\ndtype: object"
17931819
)
1820+
if using_infer_string:
1821+
exp = exp.replace("dtype: object", "dtype: str")
17941822
assert exp == res
17951823

17961824
def test_ncols(self):

0 commit comments

Comments
 (0)