Skip to content

Commit cacd4bb

Browse files
[backport 2.3.x] TST (string dtype): resolve xfails in pandas/tests/series (#60233) (#60240)
(cherry picked from commit 3f7bc81)
1 parent db68cd5 commit cacd4bb

File tree

7 files changed

+64
-56
lines changed

7 files changed

+64
-56
lines changed

pandas/tests/series/accessors/test_dt_accessor.py

-4
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
import pytest
1212
import pytz
1313

14-
from pandas._config import using_string_dtype
15-
1614
from pandas._libs.tslibs.timezones import maybe_get_tz
1715
from pandas.errors import SettingWithCopyError
1816

@@ -571,7 +569,6 @@ def test_strftime(self):
571569
)
572570
tm.assert_series_equal(result, expected)
573571

574-
@pytest.mark.xfail(using_string_dtype(), reason="TODO(infer_string)")
575572
def test_strftime_dt64_days(self):
576573
ser = Series(date_range("20130101", periods=5))
577574
ser.iloc[0] = pd.NaT
@@ -586,7 +583,6 @@ def test_strftime_dt64_days(self):
586583

587584
expected = Index(
588585
["2015/03/01", "2015/03/02", "2015/03/03", "2015/03/04", "2015/03/05"],
589-
dtype=np.object_,
590586
)
591587
# dtype may be S10 or U10 depending on python version
592588
tm.assert_index_equal(result, expected)

pandas/tests/series/indexing/test_indexing.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import numpy as np
66
import pytest
77

8-
from pandas._config import using_string_dtype
9-
108
from pandas.errors import IndexingError
119

1210
from pandas import (
@@ -270,18 +268,29 @@ def test_slice(string_series, object_series, using_copy_on_write, warn_copy_on_w
270268
assert (string_series[10:20] == 0).all()
271269

272270

273-
@pytest.mark.xfail(using_string_dtype(), reason="TODO(infer_string)")
274271
def test_timedelta_assignment():
275272
# GH 8209
276273
s = Series([], dtype=object)
277274
s.loc["B"] = timedelta(1)
278-
tm.assert_series_equal(s, Series(Timedelta("1 days"), index=["B"]))
275+
expected = Series(
276+
Timedelta("1 days"), dtype="timedelta64[ns]", index=Index(["B"], dtype=object)
277+
)
278+
tm.assert_series_equal(s, expected)
279279

280280
s = s.reindex(s.index.insert(0, "A"))
281-
tm.assert_series_equal(s, Series([np.nan, Timedelta("1 days")], index=["A", "B"]))
281+
expected = Series(
282+
[np.nan, Timedelta("1 days")],
283+
dtype="timedelta64[ns]",
284+
index=Index(["A", "B"], dtype=object),
285+
)
286+
tm.assert_series_equal(s, expected)
282287

283288
s.loc["A"] = timedelta(1)
284-
expected = Series(Timedelta("1 days"), index=["A", "B"])
289+
expected = Series(
290+
Timedelta("1 days"),
291+
dtype="timedelta64[ns]",
292+
index=Index(["A", "B"], dtype=object),
293+
)
285294
tm.assert_series_equal(s, expected)
286295

287296

pandas/tests/series/indexing/test_setitem.py

+18-20
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
import numpy as np
99
import pytest
1010

11-
from pandas._config import using_string_dtype
12-
13-
from pandas.compat import HAS_PYARROW
1411
from pandas.compat.numpy import (
1512
np_version_gt2,
1613
np_version_gte1p24,
@@ -37,6 +34,7 @@
3734
concat,
3835
date_range,
3936
interval_range,
37+
isna,
4038
period_range,
4139
timedelta_range,
4240
)
@@ -564,14 +562,16 @@ def test_append_timedelta_does_not_cast(self, td, using_infer_string, request):
564562
tm.assert_series_equal(ser, expected)
565563
assert isinstance(ser["td"], Timedelta)
566564

567-
@pytest.mark.xfail(using_string_dtype(), reason="TODO(infer_string)")
568565
def test_setitem_with_expansion_type_promotion(self):
569566
# GH#12599
570567
ser = Series(dtype=object)
571568
ser["a"] = Timestamp("2016-01-01")
572569
ser["b"] = 3.0
573570
ser["c"] = "foo"
574-
expected = Series([Timestamp("2016-01-01"), 3.0, "foo"], index=["a", "b", "c"])
571+
expected = Series(
572+
[Timestamp("2016-01-01"), 3.0, "foo"],
573+
index=Index(["a", "b", "c"], dtype=object),
574+
)
575575
tm.assert_series_equal(ser, expected)
576576

577577
def test_setitem_not_contained(self, string_series):
@@ -850,11 +850,6 @@ def test_mask_key(self, obj, key, expected, warn, val, indexer_sli):
850850
indexer_sli(obj)[mask] = val
851851
tm.assert_series_equal(obj, expected)
852852

853-
@pytest.mark.xfail(
854-
using_string_dtype() and not HAS_PYARROW,
855-
reason="TODO(infer_string)",
856-
strict=False,
857-
)
858853
def test_series_where(self, obj, key, expected, warn, val, is_inplace):
859854
mask = np.zeros(obj.shape, dtype=bool)
860855
mask[key] = True
@@ -870,6 +865,11 @@ def test_series_where(self, obj, key, expected, warn, val, is_inplace):
870865
obj = obj.copy()
871866
arr = obj._values
872867

868+
if obj.dtype == "string" and not (isinstance(val, str) or isna(val)):
869+
with pytest.raises(TypeError, match="Invalid value"):
870+
obj.where(~mask, val)
871+
return
872+
873873
res = obj.where(~mask, val)
874874

875875
if val is NA and res.dtype == object:
@@ -882,29 +882,27 @@ def test_series_where(self, obj, key, expected, warn, val, is_inplace):
882882

883883
self._check_inplace(is_inplace, orig, arr, obj)
884884

885-
@pytest.mark.xfail(using_string_dtype(), reason="TODO(infer_string)", strict=False)
886-
def test_index_where(self, obj, key, expected, warn, val, using_infer_string):
885+
def test_index_where(self, obj, key, expected, warn, val):
887886
mask = np.zeros(obj.shape, dtype=bool)
888887
mask[key] = True
889888

890-
if using_infer_string and obj.dtype == object:
889+
if obj.dtype == "string" and not (isinstance(val, str) or isna(val)):
891890
with pytest.raises(TypeError, match="Invalid value"):
892-
Index(obj).where(~mask, val)
891+
Index(obj, dtype=obj.dtype).where(~mask, val)
893892
else:
894-
res = Index(obj).where(~mask, val)
893+
res = Index(obj, dtype=obj.dtype).where(~mask, val)
895894
expected_idx = Index(expected, dtype=expected.dtype)
896895
tm.assert_index_equal(res, expected_idx)
897896

898-
@pytest.mark.xfail(using_string_dtype(), reason="TODO(infer_string)", strict=False)
899-
def test_index_putmask(self, obj, key, expected, warn, val, using_infer_string):
897+
def test_index_putmask(self, obj, key, expected, warn, val):
900898
mask = np.zeros(obj.shape, dtype=bool)
901899
mask[key] = True
902900

903-
if using_infer_string and obj.dtype == object:
901+
if obj.dtype == "string" and not (isinstance(val, str) or isna(val)):
904902
with pytest.raises(TypeError, match="Invalid value"):
905-
Index(obj).putmask(mask, val)
903+
Index(obj, dtype=obj.dtype).putmask(mask, val)
906904
else:
907-
res = Index(obj).putmask(mask, val)
905+
res = Index(obj, dtype=obj.dtype).putmask(mask, val)
908906
tm.assert_index_equal(res, Index(expected, dtype=expected.dtype))
909907

910908

pandas/tests/series/indexing/test_where.py

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import numpy as np
22
import pytest
33

4-
from pandas._config import using_string_dtype
5-
64
from pandas.core.dtypes.common import is_integer
75

86
import pandas as pd
@@ -232,7 +230,6 @@ def test_where_ndframe_align():
232230
tm.assert_series_equal(out, expected)
233231

234232

235-
@pytest.mark.xfail(using_string_dtype(), reason="can't set ints into string")
236233
def test_where_setitem_invalid():
237234
# GH 2702
238235
# make sure correct exceptions are raised on invalid list assignment
@@ -242,7 +239,7 @@ def test_where_setitem_invalid():
242239
"different length than the value"
243240
)
244241
# slice
245-
s = Series(list("abc"))
242+
s = Series(list("abc"), dtype=object)
246243

247244
with pytest.raises(ValueError, match=msg("slice")):
248245
s[0:3] = list(range(27))
@@ -252,18 +249,18 @@ def test_where_setitem_invalid():
252249
tm.assert_series_equal(s.astype(np.int64), expected)
253250

254251
# slice with step
255-
s = Series(list("abcdef"))
252+
s = Series(list("abcdef"), dtype=object)
256253

257254
with pytest.raises(ValueError, match=msg("slice")):
258255
s[0:4:2] = list(range(27))
259256

260-
s = Series(list("abcdef"))
257+
s = Series(list("abcdef"), dtype=object)
261258
s[0:4:2] = list(range(2))
262259
expected = Series([0, "b", 1, "d", "e", "f"])
263260
tm.assert_series_equal(s, expected)
264261

265262
# neg slices
266-
s = Series(list("abcdef"))
263+
s = Series(list("abcdef"), dtype=object)
267264

268265
with pytest.raises(ValueError, match=msg("slice")):
269266
s[:-1] = list(range(27))
@@ -273,18 +270,18 @@ def test_where_setitem_invalid():
273270
tm.assert_series_equal(s, expected)
274271

275272
# list
276-
s = Series(list("abc"))
273+
s = Series(list("abc"), dtype=object)
277274

278275
with pytest.raises(ValueError, match=msg("list-like")):
279276
s[[0, 1, 2]] = list(range(27))
280277

281-
s = Series(list("abc"))
278+
s = Series(list("abc"), dtype=object)
282279

283280
with pytest.raises(ValueError, match=msg("list-like")):
284281
s[[0, 1, 2]] = list(range(2))
285282

286283
# scalar
287-
s = Series(list("abc"))
284+
s = Series(list("abc"), dtype=object)
288285
s[0] = list(range(10))
289286
expected = Series([list(range(10)), "b", "c"])
290287
tm.assert_series_equal(s, expected)

pandas/tests/series/methods/test_replace.py

+22-12
Original file line numberDiff line numberDiff line change
@@ -391,19 +391,22 @@ def test_replace_mixed_types_with_string(self):
391391
expected = pd.Series([1, np.nan, 3, np.nan, 4, 5])
392392
tm.assert_series_equal(expected, result)
393393

394-
@pytest.mark.xfail(using_string_dtype(), reason="TODO(infer_string)")
395394
@pytest.mark.parametrize(
396395
"categorical, numeric",
397396
[
398397
(pd.Categorical(["A"], categories=["A", "B"]), [1]),
399398
(pd.Categorical(["A", "B"], categories=["A", "B"]), [1, 2]),
400399
],
401400
)
402-
def test_replace_categorical(self, categorical, numeric):
401+
def test_replace_categorical(self, categorical, numeric, using_infer_string):
403402
# GH 24971, GH#23305
404403
ser = pd.Series(categorical)
405404
msg = "Downcasting behavior in `replace`"
406405
msg = "with CategoricalDtype is deprecated"
406+
if using_infer_string:
407+
with pytest.raises(TypeError, match="Invalid value"):
408+
ser.replace({"A": 1, "B": 2})
409+
return
407410
with tm.assert_produces_warning(FutureWarning, match=msg):
408411
result = ser.replace({"A": 1, "B": 2})
409412
expected = pd.Series(numeric).astype("category")
@@ -731,17 +734,25 @@ def test_replace_nullable_numeric(self):
731734
with pytest.raises(TypeError, match="Invalid value"):
732735
ints.replace(1, 9.5)
733736

734-
@pytest.mark.xfail(using_string_dtype(), reason="can't fill 1 in string")
735737
@pytest.mark.parametrize("regex", [False, True])
736738
def test_replace_regex_dtype_series(self, regex):
737739
# GH-48644
738-
series = pd.Series(["0"])
740+
series = pd.Series(["0"], dtype=object)
739741
expected = pd.Series([1])
740742
msg = "Downcasting behavior in `replace`"
741743
with tm.assert_produces_warning(FutureWarning, match=msg):
742744
result = series.replace(to_replace="0", value=1, regex=regex)
743745
tm.assert_series_equal(result, expected)
744746

747+
@pytest.mark.parametrize("regex", [False, True])
748+
def test_replace_regex_dtype_series_string(self, regex, using_infer_string):
749+
if not using_infer_string:
750+
# then this is object dtype which is already tested above
751+
return
752+
series = pd.Series(["0"], dtype="str")
753+
with pytest.raises(TypeError, match="Invalid value"):
754+
series.replace(to_replace="0", value=1, regex=regex)
755+
745756
def test_replace_different_int_types(self, any_int_numpy_dtype):
746757
# GH#45311
747758
labs = pd.Series([1, 1, 1, 0, 0, 2, 2, 2], dtype=any_int_numpy_dtype)
@@ -761,20 +772,19 @@ def test_replace_value_none_dtype_numeric(self, val):
761772
expected = pd.Series([1, None], dtype=object)
762773
tm.assert_series_equal(result, expected)
763774

764-
def test_replace_change_dtype_series(self, using_infer_string):
775+
@pytest.mark.xfail(using_string_dtype(), reason="TODO(infer_string)")
776+
def test_replace_change_dtype_series(self):
765777
# GH#25797
766-
df = pd.DataFrame.from_dict({"Test": ["0.5", True, "0.6"]})
767-
warn = FutureWarning if using_infer_string else None
768-
with tm.assert_produces_warning(warn, match="Downcasting"):
769-
df["Test"] = df["Test"].replace([True], [np.nan])
770-
expected = pd.DataFrame.from_dict({"Test": ["0.5", np.nan, "0.6"]})
778+
df = pd.DataFrame({"Test": ["0.5", True, "0.6"]}, dtype=object)
779+
df["Test"] = df["Test"].replace([True], [np.nan])
780+
expected = pd.DataFrame({"Test": ["0.5", np.nan, "0.6"]}, dtype=object)
771781
tm.assert_frame_equal(df, expected)
772782

773-
df = pd.DataFrame.from_dict({"Test": ["0.5", None, "0.6"]})
783+
df = pd.DataFrame({"Test": ["0.5", None, "0.6"]}, dtype=object)
774784
df["Test"] = df["Test"].replace([None], [np.nan])
775785
tm.assert_frame_equal(df, expected)
776786

777-
df = pd.DataFrame.from_dict({"Test": ["0.5", None, "0.6"]})
787+
df = pd.DataFrame({"Test": ["0.5", None, "0.6"]}, dtype=object)
778788
df["Test"] = df["Test"].fillna(np.nan)
779789
tm.assert_frame_equal(df, expected)
780790

pandas/tests/series/methods/test_unstack.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import numpy as np
22
import pytest
33

4-
from pandas._config import using_string_dtype
5-
64
import pandas as pd
75
from pandas import (
86
DataFrame,
@@ -136,11 +134,10 @@ def test_unstack_mixed_type_name_in_multiindex(
136134
tm.assert_frame_equal(result, expected)
137135

138136

139-
@pytest.mark.xfail(using_string_dtype(), reason="TODO(infer_string)")
140137
def test_unstack_multi_index_categorical_values():
141138
df = DataFrame(
142139
np.random.default_rng(2).standard_normal((10, 4)),
143-
columns=Index(list("ABCD"), dtype=object),
140+
columns=Index(list("ABCD")),
144141
index=date_range("2000-01-01", periods=10, freq="B"),
145142
)
146143
mi = df.stack(future_stack=True).index.rename(["major", "minor"])

pandas/tests/series/test_logical_ops.py

+1
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ def test_logical_ops_label_based(self, using_infer_string):
431431
for e in [Series(["z"])]:
432432
if using_infer_string:
433433
# TODO(infer_string) should this behave differently?
434+
# -> https://github.com/pandas-dev/pandas/issues/60234
434435
with pytest.raises(
435436
TypeError, match="not supported for dtype|unsupported operand type"
436437
):

0 commit comments

Comments
 (0)