Skip to content

Commit e6024c1

Browse files
authored
BUG: preserve EA in pd.array (#48889)
* BUG: preserve EA in pd.array * fix 32bit builds
1 parent b116c10 commit e6024c1

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

pandas/core/construction.py

+9
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
)
5050
from pandas.core.dtypes.common import (
5151
is_datetime64_ns_dtype,
52+
is_dtype_equal,
5253
is_extension_array_dtype,
5354
is_float_dtype,
5455
is_integer_dtype,
@@ -328,6 +329,14 @@ def array(
328329
if isinstance(dtype, str):
329330
dtype = registry.find(dtype) or dtype
330331

332+
if isinstance(data, ExtensionArray) and (
333+
dtype is None or is_dtype_equal(dtype, data.dtype)
334+
):
335+
# e.g. TimedeltaArray[s], avoid casting to PandasArray
336+
if copy:
337+
return data.copy()
338+
return data
339+
331340
if is_extension_array_dtype(dtype):
332341
cls = cast(ExtensionDtype, dtype).construct_array_type()
333342
return cls._from_sequence(data, dtype=dtype, copy=copy)

pandas/tests/arrays/test_array.py

+20
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,26 @@
124124
None,
125125
TimedeltaArray._from_sequence(["1H", "2H"]),
126126
),
127+
(
128+
# preserve non-nano, i.e. don't cast to PandasArray
129+
TimedeltaArray._simple_new(
130+
np.arange(5, dtype=np.int64).view("m8[s]"), dtype=np.dtype("m8[s]")
131+
),
132+
None,
133+
TimedeltaArray._simple_new(
134+
np.arange(5, dtype=np.int64).view("m8[s]"), dtype=np.dtype("m8[s]")
135+
),
136+
),
137+
(
138+
# preserve non-nano, i.e. don't cast to PandasArray
139+
TimedeltaArray._simple_new(
140+
np.arange(5, dtype=np.int64).view("m8[s]"), dtype=np.dtype("m8[s]")
141+
),
142+
np.dtype("m8[s]"),
143+
TimedeltaArray._simple_new(
144+
np.arange(5, dtype=np.int64).view("m8[s]"), dtype=np.dtype("m8[s]")
145+
),
146+
),
127147
# Category
128148
(["a", "b"], "category", pd.Categorical(["a", "b"])),
129149
(

0 commit comments

Comments
 (0)