Skip to content

Commit b55016a

Browse files
BUG: fixed bug where None would appear as 'None' (pandas-dev#57702)
1 parent b63ae8c commit b55016a

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

pandas/core/arrays/numpy_.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,22 @@ def _from_sequence(
124124
# None]"; expected "Union[dtype[Any], None, type, _SupportsDType, str,
125125
# Union[Tuple[Any, int], Tuple[Any, Union[int, Sequence[int]]], List[Any],
126126
# _DTypeDict, Tuple[Any, Any]]]"
127-
result = np.asarray(scalars, dtype=dtype) # type: ignore[arg-type]
127+
128+
if (None not in scalars):
129+
result = np.asarray(scalars, dtype=dtype)
130+
else:
131+
try:
132+
scalars_not_none=[item for item in scalars if item is not None]
133+
except:
134+
raise ValueError("NumpyExtensionArray must be 1-dimensional")
135+
indexes_not_none=[]
136+
indexed_data=[None]* len(scalars)
137+
for i in range(len(scalars)):
138+
if scalars[i] is not None:
139+
indexes_not_none.append(i)
140+
for i in range(len(scalars_not_none)):
141+
indexed_data[indexes_not_none[i]]=result[i]
142+
result=np.asarray(indexed_data, dtype="object")
128143
if (
129144
result.ndim > 1
130145
and not hasattr(scalars, "dtype")

pandas/tests/arrays/numpy_/test_numpy.py

+5
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ def test_from_sequence_dtype():
124124
expected = NumpyExtensionArray(np.array([1, 2, 3], dtype="uint64"))
125125
tm.assert_extension_array_equal(result, expected)
126126

127+
def test_from_sequence_with_none():
128+
result=pd.array([1, None], dtype=str)
129+
expected=pd.array(['1', None], dtype="object")
130+
tm.assert_extension_array_equal(result, expected)
131+
127132

128133
def test_constructor_copy():
129134
arr = np.array([0, 1])

0 commit comments

Comments
 (0)