Skip to content

Commit 24f72b6

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

File tree

3 files changed

+17
-18
lines changed

3 files changed

+17
-18
lines changed

pandas/core/arrays/numpy_.py

+10-12
Original file line numberDiff line numberDiff line change
@@ -124,22 +124,20 @@ 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-
if (None not in scalars):
127+
if None not in scalars:
128128
result = np.asarray(scalars, dtype=dtype)
129129
else:
130130
try:
131-
scalars_not_none=[item for item in scalars if item is not None]
132-
except:
133-
raise ValueError("NumpyExtensionArray must be 1-dimensional")
134-
indexes_not_none=[]
135-
indexed_data=[None]* len(scalars)
136-
scalars_not_none_casted=np.asarray(scalars_not_none, dtype=dtype)
137-
for i in range(len(scalars)):
138-
if scalars[i] is not None:
139-
indexes_not_none.append(i)
131+
scalars_not_none = [item for item in scalars if item is not None]
132+
except Exception as err:
133+
raise ValueError("NumpyExtensionArray must be 1-dimensional") from err
134+
indexes_not_none = []
135+
indexed_data = [None] * len(scalars)
136+
scalars_not_none_casted = np.asarray(scalars_not_none, dtype=dtype)
137+
indexes_not_none = [i for i, item in enumerate(scalars) if item is not None]
140138
for i in range(len(scalars_not_none)):
141-
indexed_data[indexes_not_none[i]]=scalars_not_none_casted[i]
142-
result=np.asarray(indexed_data, dtype="object")
139+
indexed_data[indexes_not_none[i]] = scalars_not_none_casted[i]
140+
result = np.asarray(indexed_data, dtype="object")
143141
if (
144142
result.ndim > 1
145143
and not hasattr(scalars, "dtype")

pandas/tests/arrays/numpy_/test_numpy.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,14 @@ def test_from_sequence_dtype():
123123
result = NumpyExtensionArray._from_sequence(arr, dtype="uint64")
124124
expected = NumpyExtensionArray(np.array([1, 2, 3], dtype="uint64"))
125125
tm.assert_extension_array_equal(result, expected)
126-
126+
127+
127128
def test_from_sequence_with_none():
128-
result=pd.array([1, None], dtype=str)
129-
expected=pd.array(['1', None], dtype="object")
129+
result = pd.array([1, None], dtype=str)
130+
expected = pd.array(["1", None], dtype="object")
130131
tm.assert_extension_array_equal(result, expected)
131132

133+
132134
def test_constructor_copy():
133135
arr = np.array([0, 1])
134136
result = NumpyExtensionArray(arr, copy=True)

pandas/tests/frame/test_constructors.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -1273,11 +1273,10 @@ def test_constructor_list_of_lists(self, using_infer_string):
12731273

12741274
def test_nested_pandasarray_matches_nested_ndarray(self):
12751275
# GH#43986
1276-
ser = Series([1, 2])
12771276

12781277
arr = np.array([None, None], dtype=object)
1279-
arr[0] = ser
1280-
arr[1] = ser * 2
1278+
arr[0] = [1, 2]
1279+
arr[1] = [2, 4]
12811280

12821281
df = DataFrame(arr)
12831282
expected = DataFrame(pd.array(arr))

0 commit comments

Comments
 (0)