Skip to content

Commit 062786f

Browse files
committed
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 f069fc2 commit 062786f

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

pandas/core/dtypes/cast.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -1217,11 +1217,14 @@ def construct_1d_arraylike_from_scalar(value, length, dtype):
12171217
dtype = dtype.dtype
12181218

12191219
# coerce if we have nan for an integer dtype
1220-
# GH 22858: only cast to float if an index
1221-
# (passed here as length) is specified
1222-
if length and is_integer_dtype(dtype) and isna(value):
1223-
dtype = np.float64
1224-
subarr = np.empty(length, dtype=dtype)
1220+
if is_integer_dtype(dtype) and isna(value):
1221+
dtype = np.dtype('float64')
1222+
if isinstance(dtype, np.dtype) and dtype.kind in ("U", "S"):
1223+
subarr = np.empty(length, dtype=object)
1224+
if not isna(value):
1225+
value = str(value)
1226+
else:
1227+
subarr = np.empty(length, dtype=dtype)
12251228
subarr.fill(value)
12261229

12271230
return subarr

pandas/core/series.py

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

149149
def __init__(self, data=None, index=None, dtype=None, name=None,
150150
copy=False, fastpath=False):
151-
152151
# we are called internally, so short-circuit
153152
if fastpath:
154153

@@ -4210,7 +4209,6 @@ def _sanitize_array(data, index, dtype=None, copy=False,
42104209
""" sanitize input data to an ndarray, copy if specified, coerce to the
42114210
dtype if specified
42124211
"""
4213-
42144212
if dtype is not None:
42154213
dtype = pandas_dtype(dtype)
42164214

pandas/tests/series/test_constructors.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def test_constructor_no_data_index_order(self):
137137
def test_constructor_no_data_string_type(self):
138138
result = pd.Series(index=[1], dtype=str)
139139
assert result.isna().all()
140-
140+
141141
def test_constructor_single_element_string_type(self):
142142
result = pd.Series(13, index=[1], dtype=str)
143143
assert result.values.tolist() == ['13']

0 commit comments

Comments
 (0)