Repository: pandas-dev/pandas
File: pandas/core/construction.py (Python)

Code before:

    if lib.is_scalar(data):
        msg = f"Cannot pass scalar '{data}' to 'pandas.array'."
        raise ValueError(msg)
    elif isinstance(data, ABCDataFrame):
        raise TypeError("Cannot pass DataFrame to 'pandas.array'")

    if dtype is None and isinstance(data, (ABCSeries, ABCIndex, ExtensionArray)):
        # Note: we exclude np.ndarray here, will do type inference on it
        dtype = data.dtype

    data = extract_array(data, extract_numpy=True)

    # Handle numpy masked arrays: convert masked values to NA
    # GH#63879
    if isinstance(data, ma.MaskedArray):
        mask = ma.getmaskarray(data)
        if mask.any():
            # Convert masked array to list with NA for masked positions
            data_list = []
            for i, val in enumerate(data):
                if mask[i]:
                    data_list.append(NA)
                else:
                    data_list.append(val)
            data = data_list
        else:
            # No mask, convert to regular array
            data = np.asarray(data)

    # this returns None for not-found dtypes.
    if dtype is not None:
        dtype = pandas_dtype(dtype)

    if isinstance(data, ExtensionArray) and (dtype is None or data.dtype == dtype):
        # e.g. TimedeltaArray[s], avoid casting to NumpyExtensionArray
        if copy:
            return data.copy()
        return data

    if isinstance(dtype, ExtensionDtype):
        cls = dtype.construct_array_type()
        return cls._from_sequence(data, dtype=dtype, copy=copy)

    if dtype is None:
        was_ndarray = isinstance(data, np.ndarray)
        # error: Item "Sequence[object]" of "Sequence[object] | ExtensionArray |
        # ndarray[Any, Any]" has no attribute "dtype"
        if not was_ndarray or data.dtype == object:  # type: ignore[union-attr]
            result = lib.maybe_convert_objects(
                ensure_object(data),

Review comment (performance):
This looks inefficient. Instead can you use `ensure_dtype_can_hold_na`.

```python
from pandas.core.dtypes.cast import ensure_dtype_can_hold_na

a = np.ma.array([1, 2, 3, 4], mask=[False, False, False, False])
dtype = ensure_dtype_can_hold_na(a.dtype)
if dtype.char in "SU":
    dtype = "object"
data = a.astype(dtype).filled(np.nan)
```

Code after:

    if lib.is_scalar(data):
        msg = f"Cannot pass scalar '{data}' to 'pandas.array'."
        raise ValueError(msg)
    elif isinstance(data, ABCDataFrame):
        raise TypeError("Cannot pass DataFrame to 'pandas.array'")

    if dtype is None and isinstance(data, (ABCSeries, ABCIndex, ExtensionArray)):
        # Note: we exclude np.ndarray here, will do type inference on it
        dtype = data.dtype

    data = extract_array(data, extract_numpy=True)

    # Handle numpy masked arrays: convert masked values to NA
    # GH#63879
    if isinstance(data, ma.MaskedArray):
        if data.dtype.names is not None:
            raise ValueError(
                "Cannot construct an array from an ndarray with compound dtype. "
                "Use DataFrame instead."
            )
        elif data.mask is not np.False_ and ma.getmaskarray(data).any():
            na_dtype = ensure_dtype_can_hold_na(data.dtype)
            if na_dtype.char in "SU":
                na_dtype = np.dtype("object")
            data = cast(ma.MaskedArray, data.astype(na_dtype)).filled(np.nan)
        else:
            # No mask, convert to regular array
            data = np.asarray(data)

    # this returns None for not-found dtypes.
    if dtype is not None:
        dtype = pandas_dtype(dtype)

    if isinstance(data, ExtensionArray) and (dtype is None or data.dtype == dtype):
        # e.g. TimedeltaArray[s], avoid casting to NumpyExtensionArray
        if copy:
            return data.copy()
        return data

    if isinstance(dtype, ExtensionDtype):
        cls = dtype.construct_array_type()
        return cls._from_sequence(data, dtype=dtype, copy=copy)

    if dtype is None:
        was_ndarray = isinstance(data, np.ndarray)
        # error: Item "Sequence[object]" of "Sequence[object] | ExtensionArray |
        # ndarray[Any, Any]" has no attribute "dtype"
        if not was_ndarray or data.dtype == object:  # type: ignore[union-attr]
            result = lib.maybe_convert_objects(
                ensure_object(data),