Skip to content

Commit f44ac06

Browse files
shantanu-gontiajreback
authored andcommitted
BUG: bugfix 26390 assigning PandasArray to DataFrame error (#26417)
1 parent 5a286e4 commit f44ac06

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

doc/source/whatsnew/v0.25.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ Indexing
379379
- Bug in :meth:`DataFrame.loc` and :meth:`Series.loc` where ``KeyError`` was not raised for a ``MultiIndex`` when the key was less than or equal to the number of levels in the :class:`MultiIndex` (:issue:`14885`).
380380
- Bug in which :meth:`DataFrame.append` produced an erroneous warning indicating that a ``KeyError`` will be thrown in the future when the data to be appended contains new columns (:issue:`22252`).
381381
- Bug in which :meth:`DataFrame.to_csv` caused a segfault for a reindexed data frame, when the indices were single-level :class:`MultiIndex` (:issue:`26303`).
382+
- Fixed bug where assigning a :class:`arrays.PandasArray` to a :class:`pandas.core.frame.DataFrame` would raise error (:issue:`26390`)
382383

383384

384385
Missing
@@ -481,7 +482,6 @@ Other
481482
- Bug in :func:`factorize` when passing an ``ExtensionArray`` with a custom ``na_sentinel`` (:issue:`25696`).
482483
- Allow :class:`Index` and :class:`RangeIndex` to be passed to numpy ``min`` and ``max`` functions.
483484

484-
485485
.. _whatsnew_0.250.contributors:
486486

487487
Contributors

pandas/core/internals/blocks.py

+3
Original file line numberDiff line numberDiff line change
@@ -3039,6 +3039,9 @@ def make_block(values, placement, klass=None, ndim=None, dtype=None,
30393039
# For now, blocks should be backed by ndarrays when possible.
30403040
if isinstance(values, ABCPandasArray):
30413041
values = values.to_numpy()
3042+
if ndim and ndim > 1:
3043+
values = np.atleast_2d(values)
3044+
30423045
if isinstance(dtype, PandasDtype):
30433046
dtype = dtype.numpy_dtype
30443047

pandas/tests/frame/test_block_internals.py

+11
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
Categorical, DataFrame, Series, Timestamp, compat, date_range,
1111
option_context)
1212
from pandas.core.arrays import IntervalArray, integer_array
13+
from pandas.core.internals import ObjectBlock
1314
from pandas.core.internals.blocks import IntBlock
1415
import pandas.util.testing as tm
1516
from pandas.util.testing import (
@@ -584,3 +585,13 @@ def test_constructor_no_pandas_array(self):
584585
expected = pd.DataFrame({"A": [1, 2, 3]})
585586
tm.assert_frame_equal(result, expected)
586587
assert isinstance(result._data.blocks[0], IntBlock)
588+
589+
def test_add_column_with_pandas_array(self):
590+
# GH 26390
591+
df = pd.DataFrame({'a': [1, 2, 3, 4], 'b': ['a', 'b', 'c', 'd']})
592+
df['c'] = pd.array([1, 2, None, 3])
593+
df2 = pd.DataFrame({'a': [1, 2, 3, 4], 'b': ['a', 'b', 'c', 'd'],
594+
'c': pd.array([1, 2, None, 3])})
595+
assert type(df['c']._data.blocks[0]) == ObjectBlock
596+
assert type(df2['c']._data.blocks[0]) == ObjectBlock
597+
assert_frame_equal(df, df2)

0 commit comments

Comments
 (0)