Skip to content

Commit 4276939

Browse files
authored
BUG: pd.array preserve PandasArray (#43887)
1 parent 7796ac5 commit 4276939

File tree

4 files changed

+12
-2
lines changed

4 files changed

+12
-2
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,7 @@ Sparse
526526

527527
ExtensionArray
528528
^^^^^^^^^^^^^^
529+
- Bug in :func:`array` failing to preserve :class:`PandasArray` (:issue:`43887`)
529530
- NumPy ufuncs ``np.abs``, ``np.positive``, ``np.negative`` now correctly preserve dtype when called on ExtensionArrays that implement ``__abs__, __pos__, __neg__``, respectively. In particular this is fixed for :class:`TimedeltaArray` (:issue:`43899`)
530531
-
531532

pandas/core/arrays/datetimelike.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -718,8 +718,9 @@ def _validate_listlike(self, value, allow_object: bool = False):
718718
msg = self._validation_error_message(value, True)
719719
raise TypeError(msg)
720720

721-
# Do type inference if necessary up front
721+
# Do type inference if necessary up front (after unpacking PandasArray)
722722
# e.g. we passed PeriodIndex.values and got an ndarray of Periods
723+
value = extract_array(value, extract_numpy=True)
723724
value = pd_array(value)
724725
value = extract_array(value, extract_numpy=True)
725726

pandas/core/construction.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ def array(
297297
from pandas.core.arrays import (
298298
BooleanArray,
299299
DatetimeArray,
300+
ExtensionArray,
300301
FloatingArray,
301302
IntegerArray,
302303
IntervalArray,
@@ -310,7 +311,7 @@ def array(
310311
msg = f"Cannot pass scalar '{data}' to 'pandas.array'."
311312
raise ValueError(msg)
312313

313-
if dtype is None and isinstance(data, (ABCSeries, ABCIndex, ABCExtensionArray)):
314+
if dtype is None and isinstance(data, (ABCSeries, ABCIndex, ExtensionArray)):
314315
# Note: we exclude np.ndarray here, will do type inference on it
315316
dtype = data.dtype
316317

pandas/tests/arrays/test_array.py

+7
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@
5050
),
5151
# String alias passes through to NumPy
5252
([1, 2], "float32", PandasArray(np.array([1, 2], dtype="float32"))),
53+
([1, 2], "int64", PandasArray(np.array([1, 2], dtype=np.int64))),
54+
# idempotency with e.g. pd.array(pd.array([1, 2], dtype="int64"))
55+
(
56+
PandasArray(np.array([1, 2], dtype=np.int32)),
57+
None,
58+
PandasArray(np.array([1, 2], dtype=np.int32)),
59+
),
5360
# Period alias
5461
(
5562
[pd.Period("2000", "D"), pd.Period("2001", "D")],

0 commit comments

Comments
 (0)