Skip to content

Commit e94bebe

Browse files
committed
Closes #22477
Add a check so if the dtype is str is will create an empty array type object and then pass the values. Add test for an empty series. To chech that it fills the series with NaN and not with 'n'. Also add a test for cases that no string values are given.
1 parent ed0b679 commit e94bebe

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

pandas/core/dtypes/cast.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -1221,8 +1221,13 @@ def construct_1d_arraylike_from_scalar(value, length, dtype):
12211221

12221222
# coerce if we have nan for an integer dtype
12231223
if is_integer_dtype(dtype) and isna(value):
1224-
dtype = np.float64
1225-
subarr = np.empty(length, dtype=dtype)
1224+
dtype = np.dtype('float64')
1225+
if isinstance(dtype, np.dtype) and dtype.kind in ("U", "S"):
1226+
subarr = np.empty(length, dtype=object)
1227+
if not isna(value):
1228+
value = str(value)
1229+
else:
1230+
subarr = np.empty(length, dtype=dtype)
12261231
subarr.fill(value)
12271232

12281233
return subarr

pandas/core/series.py

-2
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ class Series(base.IndexOpsMixin, generic.NDFrame):
175175

176176
def __init__(self, data=None, index=None, dtype=None, name=None,
177177
copy=False, fastpath=False):
178-
179178
# we are called internally, so short-circuit
180179
if fastpath:
181180

@@ -4056,7 +4055,6 @@ def _sanitize_array(data, index, dtype=None, copy=False,
40564055
""" sanitize input data to an ndarray, copy if specified, coerce to the
40574056
dtype if specified
40584057
"""
4059-
40604058
if dtype is not None:
40614059
dtype = pandas_dtype(dtype)
40624060

pandas/tests/series/test_constructors.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def test_constructor_no_data_index_order(self):
140140
def test_constructor_no_data_string_type(self):
141141
result = pd.Series(index=[1], dtype=str)
142142
assert result.isna().all()
143-
143+
144144
def test_constructor_single_element_string_type(self):
145145
result = pd.Series(13, index=[1], dtype=str)
146146
assert result.values.tolist() == ['13']

0 commit comments

Comments
 (0)