Skip to content

Commit fcd29ed

Browse files
bugfix 26390 assigning pandas array to column works
1 parent ff4437e commit fcd29ed

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

pandas/core/frame.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
is_sequence,
7171
is_named_tuple)
7272
from pandas.core.dtypes.generic import (
73-
ABCSeries, ABCDataFrame, ABCIndexClass, ABCMultiIndex)
73+
ABCSeries, ABCDataFrame, ABCIndexClass, ABCMultiIndex, ABCPandasArray)
7474
from pandas.core.dtypes.missing import isna, notna
7575

7676
from pandas.core import algorithms
@@ -3620,6 +3620,11 @@ def reindexer(value):
36203620
value = cast_scalar_to_array(len(self.index), value)
36213621
value = maybe_cast_to_datetime(value, infer_dtype)
36223622

3623+
# convert pandas array to numpy array
3624+
if isinstance(value, ABCPandasArray):
3625+
value = value.to_numpy()
3626+
return np.atleast_2d(np.asarray(value))
3627+
36233628
# return internal types directly
36243629
if is_extension_type(value) or is_extension_array_dtype(value):
36253630
return value

pandas/core/internals/blocks.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3035,6 +3035,7 @@ def make_block(values, placement, klass=None, ndim=None, dtype=None,
30353035
# For now, blocks should be backed by ndarrays when possible.
30363036
if isinstance(values, ABCPandasArray):
30373037
values = values.to_numpy()
3038+
30383039
if isinstance(dtype, PandasDtype):
30393040
dtype = dtype.numpy_dtype
30403041

pandas/tests/internals/test_internals.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,3 +1310,12 @@ def test_make_block_no_pandas_array():
13101310
result = make_block(arr.to_numpy(), slice(len(arr)), dtype=arr.dtype)
13111311
assert result.is_integer is True
13121312
assert result.is_extension is False
1313+
1314+
1315+
def test_add_column_with_pandas_array():
1316+
# GH 26390
1317+
df = pd.DataFrame({'a': [1, 2, 3, 4], 'b': ['a', 'b', 'c', 'd']})
1318+
df['c'] = pd.array([1, 2, None, 3])
1319+
df2 = pd.DataFrame({'a': [1, 2, 3, 4], 'b': ['a', 'b', 'c', 'd'],
1320+
'c': pd.array([1, 2, None, 3])})
1321+
assert_frame_equal(df, df2)

0 commit comments

Comments
 (0)