Skip to content

Commit 6ffa4b7

Browse files
authored
BUG: pd.array([]) should return masked array (#54372)
* BUG: pd.array([]) should return masked array * BUG: pd.array([]) should return masked array II * BUG: pd.array([]) should return masked array III
1 parent 152595c commit 6ffa4b7

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

doc/source/whatsnew/v2.1.0.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,8 @@ Numeric
668668
Conversion
669669
^^^^^^^^^^
670670
- Bug in :func:`DataFrame.style.to_latex` and :func:`DataFrame.style.to_html` if the DataFrame contains integers with more digits than can be represented by floating point double precision (:issue:`52272`)
671-
- Bug in :func:`array` when given a ``datetime64`` or ``timedelta64`` dtype with unit of "s", "us", or "ms" returning :class:`PandasArray` instead of :class:`DatetimeArray` or :class:`TimedeltaArray` (:issue:`52859`)
671+
- Bug in :func:`array` when given a ``datetime64`` or ``timedelta64`` dtype with unit of "s", "us", or "ms" returning :class:`NumpyExtensionArray` instead of :class:`DatetimeArray` or :class:`TimedeltaArray` (:issue:`52859`)
672+
- Bug in :func:`array` when given an empty list and no dtype returning :class:`NumpyExtensionArray` instead of :class:`FloatingArray` (:issue:`54371`)
672673
- Bug in :meth:`ArrowDtype.numpy_dtype` returning nanosecond units for non-nanosecond ``pyarrow.timestamp`` and ``pyarrow.duration`` types (:issue:`51800`)
673674
- Bug in :meth:`DataFrame.__repr__` incorrectly raising a ``TypeError`` when the dtype of a column is ``np.record`` (:issue:`48526`)
674675
- Bug in :meth:`DataFrame.info` raising ``ValueError`` when ``use_numba`` is set (:issue:`51922`)

pandas/core/construction.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,8 @@ def array(
350350

351351
elif inferred_dtype == "integer":
352352
return IntegerArray._from_sequence(data, copy=copy)
353-
353+
elif inferred_dtype == "empty" and not hasattr(data, "dtype") and not len(data):
354+
return FloatingArray._from_sequence(data, copy=copy)
354355
elif (
355356
inferred_dtype in ("floating", "mixed-integer-float")
356357
and getattr(data, "dtype", None) != np.float16

pandas/tests/arrays/test_array.py

+6
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,19 @@ def test_dt64_array(dtype_unit):
4747
"data, dtype, expected",
4848
[
4949
# Basic NumPy defaults.
50+
([], None, FloatingArray._from_sequence([])),
5051
([1, 2], None, IntegerArray._from_sequence([1, 2])),
5152
([1, 2], object, NumpyExtensionArray(np.array([1, 2], dtype=object))),
5253
(
5354
[1, 2],
5455
np.dtype("float32"),
5556
NumpyExtensionArray(np.array([1.0, 2.0], dtype=np.dtype("float32"))),
5657
),
58+
(
59+
np.array([], dtype=object),
60+
None,
61+
NumpyExtensionArray(np.array([], dtype=object)),
62+
),
5763
(np.array([1, 2], dtype="int64"), None, IntegerArray._from_sequence([1, 2])),
5864
(
5965
np.array([1.0, 2.0], dtype="float64"),

0 commit comments

Comments
 (0)