Skip to content

Commit 571ab8a

Browse files
committed
Update infer_dtype_from
1 parent 078aca1 commit 571ab8a

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

pandas/core/dtypes/cast.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,9 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False):
684684
# instead of np.empty (but then you still don't want things
685685
# coming out as np.str_!
686686

687-
dtype = np.object_
687+
from pandas.core.arrays.string_ import StringDtype
688+
689+
dtype = StringDtype()
688690

689691
elif isinstance(val, (np.datetime64, datetime)):
690692
val = tslibs.Timestamp(val)

pandas/core/internals/blocks.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -1006,14 +1006,8 @@ def coerce_to_target_dtype(self, other):
10061006
we can also safely try to coerce to the same dtype
10071007
and will receive the same block
10081008
"""
1009-
if isinstance(other, str):
1010-
if is_string_dtype(self.dtype):
1011-
return self
1012-
else:
1013-
dtype = np.object_
1014-
else:
1015-
# if we cannot then coerce to object
1016-
dtype, _ = infer_dtype_from(other, pandas_dtype=True)
1009+
# if we cannot then coerce to object
1010+
dtype, _ = infer_dtype_from(other, pandas_dtype=True)
10171011

10181012
if is_dtype_equal(self.dtype, dtype):
10191013
return self

pandas/tests/series/methods/test_replace.py

+11-6
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ def test_replace_replacer_equals_replacement(self):
278278
# GH 20656
279279
# make sure all replacers are matching against original values
280280
s = pd.Series(["a", "b"])
281-
expected = pd.Series(["b", "a"])
281+
expected = pd.Series(["b", "a"], dtype="string")
282282
result = s.replace({"a": "b", "b": "a"})
283283
tm.assert_series_equal(expected, result)
284284

@@ -350,19 +350,24 @@ def test_replace_with_no_overflowerror(self):
350350
tm.assert_series_equal(result, expected)
351351

352352
@pytest.mark.parametrize(
353-
"ser, to_replace, exp",
353+
"ser, to_replace, exp, dtype",
354354
[
355-
([1, 2, 3], {1: 2, 2: 3, 3: 4}, [2, 3, 4]),
356-
(["1", "2", "3"], {"1": "2", "2": "3", "3": "4"}, ["2", "3", "4"]),
355+
([1, 2, 3], {1: 2, 2: 3, 3: 4}, [2, 3, 4], "int64"),
356+
(
357+
["1", "2", "3"],
358+
{"1": "2", "2": "3", "3": "4"},
359+
["2", "3", "4"],
360+
"string",
361+
),
357362
],
358363
)
359-
def test_replace_commutative(self, ser, to_replace, exp):
364+
def test_replace_commutative(self, ser, to_replace, exp, dtype):
360365
# GH 16051
361366
# DataFrame.replace() overwrites when values are non-numeric
362367

363368
series = pd.Series(ser)
364369

365-
expected = pd.Series(exp)
370+
expected = pd.Series(exp, dtype=dtype)
366371
result = series.replace(to_replace)
367372

368373
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)