Skip to content

Commit f96fc03

Browse files
TST (string dtype): resolve xfails in pandas/tests/series
1 parent feaa963 commit f96fc03

File tree

5 files changed

+49
-38
lines changed

5 files changed

+49
-38
lines changed

pandas/tests/series/accessors/test_dt_accessor.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,6 @@ def test_strftime(self):
556556
)
557557
tm.assert_series_equal(result, expected)
558558

559-
@pytest.mark.xfail(using_string_dtype(), reason="TODO(infer_string)")
560559
def test_strftime_dt64_days(self):
561560
ser = Series(date_range("20130101", periods=5))
562561
ser.iloc[0] = pd.NaT
@@ -571,7 +570,6 @@ def test_strftime_dt64_days(self):
571570

572571
expected = Index(
573572
["2015/03/01", "2015/03/02", "2015/03/03", "2015/03/04", "2015/03/05"],
574-
dtype=np.object_,
575573
)
576574
# dtype may be S10 or U10 depending on python version
577575
tm.assert_index_equal(result, expected)

pandas/tests/series/indexing/test_indexing.py

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

9-
from pandas._config import using_string_dtype
10-
119
from pandas.errors import IndexingError
1210

1311
from pandas import (
@@ -251,18 +249,29 @@ def test_slice(string_series, object_series):
251249
tm.assert_series_equal(string_series, original)
252250

253251

254-
@pytest.mark.xfail(using_string_dtype(), reason="TODO(infer_string)")
255252
def test_timedelta_assignment():
256253
# GH 8209
257254
s = Series([], dtype=object)
258255
s.loc["B"] = timedelta(1)
259-
tm.assert_series_equal(s, Series(Timedelta("1 days"), index=["B"]))
256+
expected = Series(
257+
Timedelta("1 days"), dtype="timedelta64[ns]", index=Index(["B"], dtype=object)
258+
)
259+
tm.assert_series_equal(s, expected)
260260

261261
s = s.reindex(s.index.insert(0, "A"))
262-
tm.assert_series_equal(s, Series([np.nan, Timedelta("1 days")], index=["A", "B"]))
262+
expected = Series(
263+
[np.nan, Timedelta("1 days")],
264+
dtype="timedelta64[ns]",
265+
index=Index(["A", "B"], dtype=object),
266+
)
267+
tm.assert_series_equal(s, expected)
263268

264269
s.loc["A"] = timedelta(1)
265-
expected = Series(Timedelta("1 days"), index=["A", "B"])
270+
expected = Series(
271+
Timedelta("1 days"),
272+
dtype="timedelta64[ns]",
273+
index=Index(["A", "B"], dtype=object),
274+
)
266275
tm.assert_series_equal(s, expected)
267276

268277

pandas/tests/series/indexing/test_setitem.py

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@
99
import numpy as np
1010
import pytest
1111

12-
from pandas._config import using_string_dtype
13-
14-
from pandas.compat import (
15-
HAS_PYARROW,
16-
WASM,
17-
)
12+
from pandas.compat import WASM
1813
from pandas.compat.numpy import np_version_gte1p24
1914
from pandas.errors import IndexingError
2015

@@ -535,14 +530,16 @@ def test_append_timedelta_does_not_cast(self, td, using_infer_string, request):
535530
tm.assert_series_equal(ser, expected)
536531
assert isinstance(ser["td"], Timedelta)
537532

538-
@pytest.mark.xfail(using_string_dtype(), reason="TODO(infer_string)")
539533
def test_setitem_with_expansion_type_promotion(self):
540534
# GH#12599
541535
ser = Series(dtype=object)
542536
ser["a"] = Timestamp("2016-01-01")
543537
ser["b"] = 3.0
544538
ser["c"] = "foo"
545-
expected = Series([Timestamp("2016-01-01"), 3.0, "foo"], index=["a", "b", "c"])
539+
expected = Series(
540+
[Timestamp("2016-01-01"), 3.0, "foo"],
541+
index=Index(["a", "b", "c"], dtype=object),
542+
)
546543
tm.assert_series_equal(ser, expected)
547544

548545
def test_setitem_not_contained(self, string_series):
@@ -826,11 +823,6 @@ def test_mask_key(self, obj, key, expected, raises, val, indexer_sli):
826823
else:
827824
indexer_sli(obj)[mask] = val
828825

829-
@pytest.mark.xfail(
830-
using_string_dtype() and not HAS_PYARROW,
831-
reason="TODO(infer_string)",
832-
strict=False,
833-
)
834826
def test_series_where(self, obj, key, expected, raises, val, is_inplace):
835827
mask = np.zeros(obj.shape, dtype=bool)
836828
mask[key] = True
@@ -846,6 +838,11 @@ def test_series_where(self, obj, key, expected, raises, val, is_inplace):
846838
obj = obj.copy()
847839
arr = obj._values
848840

841+
if raises and obj.dtype == "string":
842+
with pytest.raises(TypeError, match="Invalid value"):
843+
obj.where(~mask, val)
844+
return
845+
849846
res = obj.where(~mask, val)
850847

851848
if val is NA and res.dtype == object:
@@ -858,25 +855,23 @@ def test_series_where(self, obj, key, expected, raises, val, is_inplace):
858855

859856
self._check_inplace(is_inplace, orig, arr, obj)
860857

861-
@pytest.mark.xfail(using_string_dtype(), reason="TODO(infer_string)", strict=False)
862-
def test_index_where(self, obj, key, expected, raises, val, using_infer_string):
858+
def test_index_where(self, obj, key, expected, raises, val):
863859
mask = np.zeros(obj.shape, dtype=bool)
864860
mask[key] = True
865861

866-
if using_infer_string and obj.dtype == object:
862+
if raises and obj.dtype == "string":
867863
with pytest.raises(TypeError, match="Invalid value"):
868864
Index(obj).where(~mask, val)
869865
else:
870866
res = Index(obj).where(~mask, val)
871867
expected_idx = Index(expected, dtype=expected.dtype)
872868
tm.assert_index_equal(res, expected_idx)
873869

874-
@pytest.mark.xfail(using_string_dtype(), reason="TODO(infer_string)", strict=False)
875-
def test_index_putmask(self, obj, key, expected, raises, val, using_infer_string):
870+
def test_index_putmask(self, obj, key, expected, raises, val):
876871
mask = np.zeros(obj.shape, dtype=bool)
877872
mask[key] = True
878873

879-
if using_infer_string and obj.dtype == object:
874+
if raises and obj.dtype == "string":
880875
with pytest.raises(TypeError, match="Invalid value"):
881876
Index(obj).putmask(mask, val)
882877
else:
@@ -1372,6 +1367,19 @@ def raises(self):
13721367
return False
13731368

13741369

1370+
@pytest.mark.parametrize(
1371+
"val,exp_dtype,raises",
1372+
[
1373+
(1, object, True),
1374+
("e", "str", False),
1375+
],
1376+
)
1377+
class TestCoercionString(CoercionTest):
1378+
@pytest.fixture
1379+
def obj(self):
1380+
return Series(["a", "b", "c", "d"])
1381+
1382+
13751383
@pytest.mark.parametrize(
13761384
"val,exp_dtype,raises",
13771385
[

pandas/tests/series/methods/test_replace.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -656,21 +656,18 @@ def test_replace_value_none_dtype_numeric(self, val):
656656
expected = pd.Series([1, None], dtype=object)
657657
tm.assert_series_equal(result, expected)
658658

659-
@pytest.mark.xfail(using_string_dtype(), reason="TODO(infer_string)")
660-
def test_replace_change_dtype_series(self, using_infer_string):
659+
def test_replace_change_dtype_series(self):
661660
# GH#25797
662-
df = pd.DataFrame.from_dict({"Test": ["0.5", True, "0.6"]})
663-
warn = FutureWarning if using_infer_string else None
664-
with tm.assert_produces_warning(warn, match="Downcasting"):
665-
df["Test"] = df["Test"].replace([True], [np.nan])
666-
expected = pd.DataFrame.from_dict({"Test": ["0.5", np.nan, "0.6"]})
661+
df = pd.DataFrame({"Test": ["0.5", True, "0.6"]}, dtype=object)
662+
df["Test"] = df["Test"].replace([True], [np.nan])
663+
expected = pd.DataFrame({"Test": ["0.5", np.nan, "0.6"]}, dtype=object)
667664
tm.assert_frame_equal(df, expected)
668665

669-
df = pd.DataFrame.from_dict({"Test": ["0.5", None, "0.6"]})
666+
df = pd.DataFrame({"Test": ["0.5", None, "0.6"]}, dtype=object)
670667
df["Test"] = df["Test"].replace([None], [np.nan])
671668
tm.assert_frame_equal(df, expected)
672669

673-
df = pd.DataFrame.from_dict({"Test": ["0.5", None, "0.6"]})
670+
df = pd.DataFrame({"Test": ["0.5", None, "0.6"]}, dtype=object)
674671
df["Test"] = df["Test"].fillna(np.nan)
675672
tm.assert_frame_equal(df, expected)
676673

pandas/tests/series/methods/test_unstack.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,10 @@ def test_unstack_mixed_type_name_in_multiindex(
136136
tm.assert_frame_equal(result, expected)
137137

138138

139-
@pytest.mark.xfail(using_string_dtype(), reason="TODO(infer_string)")
140139
def test_unstack_multi_index_categorical_values():
141140
df = DataFrame(
142141
np.random.default_rng(2).standard_normal((10, 4)),
143-
columns=Index(list("ABCD"), dtype=object),
142+
columns=Index(list("ABCD")),
144143
index=date_range("2000-01-01", periods=10, freq="B"),
145144
)
146145
mi = df.stack().index.rename(["major", "minor"])

0 commit comments

Comments
 (0)