Skip to content

Commit b227024

Browse files
committed
Move conditional branch out of infer_dtype_from()
1 parent 6ae7a09 commit b227024

File tree

3 files changed

+11
-15
lines changed

3 files changed

+11
-15
lines changed

pandas/core/dtypes/cast.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -660,9 +660,7 @@ def infer_dtype_from_scalar(val, pandas_dtype: bool = False) -> Tuple[DtypeObj,
660660
# instead of np.empty (but then you still don't want things
661661
# coming out as np.str_!
662662

663-
from pandas.core.arrays.string_ import StringDtype
664-
665-
dtype = StringDtype()
663+
dtype = np.dtype(object)
666664

667665
elif isinstance(val, (np.datetime64, datetime)):
668666
val = tslibs.Timestamp(val)

pandas/core/internals/blocks.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
is_re,
4646
is_re_compilable,
4747
is_sparse,
48-
is_string_dtype,
4948
is_timedelta64_dtype,
5049
pandas_dtype,
5150
)
@@ -73,6 +72,7 @@
7372
PandasDtype,
7473
TimedeltaArray,
7574
)
75+
from pandas.core.arrays.string_ import StringDtype
7676
from pandas.core.base import PandasObject
7777
import pandas.core.common as com
7878
from pandas.core.construction import extract_array
@@ -1008,6 +1008,9 @@ def coerce_to_target_dtype(self, other):
10081008
# if we cannot then coerce to object
10091009
dtype, _ = infer_dtype_from(other, pandas_dtype=True)
10101010

1011+
if isinstance(self.dtype, StringDtype):
1012+
dtype = StringDtype()
1013+
10111014
if is_dtype_equal(self.dtype, dtype):
10121015
return self
10131016

pandas/tests/series/methods/test_replace.py

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

@@ -346,24 +346,19 @@ def test_replace_with_no_overflowerror(self):
346346
tm.assert_series_equal(result, expected)
347347

348348
@pytest.mark.parametrize(
349-
"ser, to_replace, exp, dtype",
349+
"ser, to_replace, exp",
350350
[
351-
([1, 2, 3], {1: 2, 2: 3, 3: 4}, [2, 3, 4], "int64"),
352-
(
353-
["1", "2", "3"],
354-
{"1": "2", "2": "3", "3": "4"},
355-
["2", "3", "4"],
356-
"string",
357-
),
351+
([1, 2, 3], {1: 2, 2: 3, 3: 4}, [2, 3, 4]),
352+
(["1", "2", "3"], {"1": "2", "2": "3", "3": "4"}, ["2", "3", "4"]),
358353
],
359354
)
360-
def test_replace_commutative(self, ser, to_replace, exp, dtype):
355+
def test_replace_commutative(self, ser, to_replace, exp):
361356
# GH 16051
362357
# DataFrame.replace() overwrites when values are non-numeric
363358

364359
series = pd.Series(ser)
365360

366-
expected = pd.Series(exp, dtype=dtype)
361+
expected = pd.Series(exp)
367362
result = series.replace(to_replace)
368363

369364
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)