Skip to content

Commit c9f6d50

Browse files
committed
Update infer_dtype_from
1 parent 078aca1 commit c9f6d50

File tree

3 files changed

+10
-15
lines changed

3 files changed

+10
-15
lines changed

pandas/core/dtypes/cast.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,8 @@ 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+
dtype = StringDtype()
688689

689690
elif isinstance(val, (np.datetime64, datetime)):
690691
val = tslibs.Timestamp(val)

pandas/core/internals/blocks.py

Lines changed: 2 additions & 8 deletions
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

Lines changed: 6 additions & 6 deletions
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,19 @@ 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+
(["1", "2", "3"], {"1": "2", "2": "3", "3": "4"}, ["2", "3", "4"], "string"),
357357
],
358358
)
359-
def test_replace_commutative(self, ser, to_replace, exp):
359+
def test_replace_commutative(self, ser, to_replace, exp, dtype):
360360
# GH 16051
361361
# DataFrame.replace() overwrites when values are non-numeric
362362

363363
series = pd.Series(ser)
364364

365-
expected = pd.Series(exp)
365+
expected = pd.Series(exp, dtype=dtype)
366366
result = series.replace(to_replace)
367367

368368
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)