Skip to content

Commit

Permalink
BUG: fixed bug where None would appear as 'None' (#57702)
Browse files Browse the repository at this point in the history
  • Loading branch information
DuarteMarques510 committed Apr 15, 2024
1 parent fe2cf0a commit 24f72b6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
22 changes: 10 additions & 12 deletions pandas/core/arrays/numpy_.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,22 +124,20 @@ def _from_sequence(
# None]"; expected "Union[dtype[Any], None, type, _SupportsDType, str,
# Union[Tuple[Any, int], Tuple[Any, Union[int, Sequence[int]]], List[Any],
# _DTypeDict, Tuple[Any, Any]]]"
if (None not in scalars):
if None not in scalars:
result = np.asarray(scalars, dtype=dtype)
else:
try:
scalars_not_none=[item for item in scalars if item is not None]
except:
raise ValueError("NumpyExtensionArray must be 1-dimensional")
indexes_not_none=[]
indexed_data=[None]* len(scalars)
scalars_not_none_casted=np.asarray(scalars_not_none, dtype=dtype)
for i in range(len(scalars)):
if scalars[i] is not None:
indexes_not_none.append(i)
scalars_not_none = [item for item in scalars if item is not None]
except Exception as err:
raise ValueError("NumpyExtensionArray must be 1-dimensional") from err
indexes_not_none = []
indexed_data = [None] * len(scalars)
scalars_not_none_casted = np.asarray(scalars_not_none, dtype=dtype)
indexes_not_none = [i for i, item in enumerate(scalars) if item is not None]
for i in range(len(scalars_not_none)):
indexed_data[indexes_not_none[i]]=scalars_not_none_casted[i]
result=np.asarray(indexed_data, dtype="object")
indexed_data[indexes_not_none[i]] = scalars_not_none_casted[i]
result = np.asarray(indexed_data, dtype="object")
if (
result.ndim > 1
and not hasattr(scalars, "dtype")
Expand Down
8 changes: 5 additions & 3 deletions pandas/tests/arrays/numpy_/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,14 @@ def test_from_sequence_dtype():
result = NumpyExtensionArray._from_sequence(arr, dtype="uint64")
expected = NumpyExtensionArray(np.array([1, 2, 3], dtype="uint64"))
tm.assert_extension_array_equal(result, expected)



def test_from_sequence_with_none():
result=pd.array([1, None], dtype=str)
expected=pd.array(['1', None], dtype="object")
result = pd.array([1, None], dtype=str)
expected = pd.array(["1", None], dtype="object")
tm.assert_extension_array_equal(result, expected)


def test_constructor_copy():
arr = np.array([0, 1])
result = NumpyExtensionArray(arr, copy=True)
Expand Down
5 changes: 2 additions & 3 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1273,11 +1273,10 @@ def test_constructor_list_of_lists(self, using_infer_string):

def test_nested_pandasarray_matches_nested_ndarray(self):
# GH#43986
ser = Series([1, 2])

arr = np.array([None, None], dtype=object)
arr[0] = ser
arr[1] = ser * 2
arr[0] = [1, 2]
arr[1] = [2, 4]

df = DataFrame(arr)
expected = DataFrame(pd.array(arr))
Expand Down

0 comments on commit 24f72b6

Please sign in to comment.