Skip to content

Commit b5233c4

Browse files
authored
BUG: DataFrame.apply with axis=1 and EA dtype (#38272)
1 parent 4699c2b commit b5233c4

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

pandas/core/apply.py

+20-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
from pandas._typing import Axis, FrameOrSeriesUnion
1010
from pandas.util._decorators import cache_readonly
1111

12-
from pandas.core.dtypes.common import is_dict_like, is_list_like, is_sequence
12+
from pandas.core.dtypes.common import (
13+
is_dict_like,
14+
is_extension_array_dtype,
15+
is_list_like,
16+
is_sequence,
17+
)
1318
from pandas.core.dtypes.generic import ABCSeries
1419

1520
from pandas.core.construction import create_series_with_explicit_dtype
@@ -392,12 +397,20 @@ def series_generator(self):
392397
mgr = ser._mgr
393398
blk = mgr.blocks[0]
394399

395-
for (arr, name) in zip(values, self.index):
396-
# GH#35462 re-pin mgr in case setitem changed it
397-
ser._mgr = mgr
398-
blk.values = arr
399-
ser.name = name
400-
yield ser
400+
if is_extension_array_dtype(blk.dtype):
401+
# values will be incorrect for this block
402+
# TODO(EA2D): special case would be unnecessary with 2D EAs
403+
obj = self.obj
404+
for i in range(len(obj)):
405+
yield obj._ixs(i, axis=0)
406+
407+
else:
408+
for (arr, name) in zip(values, self.index):
409+
# GH#35462 re-pin mgr in case setitem changed it
410+
ser._mgr = mgr
411+
blk.values = arr
412+
ser.name = name
413+
yield ser
401414

402415
@property
403416
def result_index(self) -> "Index":

pandas/tests/frame/apply/test_frame_apply.py

+6
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ def test_apply(self, float_frame):
5858
assert isinstance(df["c0"].dtype, CategoricalDtype)
5959
assert isinstance(df["c1"].dtype, CategoricalDtype)
6060

61+
def test_apply_axis1_with_ea(self):
62+
# GH#36785
63+
df = DataFrame({"A": [Timestamp("2013-01-01", tz="UTC")]})
64+
result = df.apply(lambda x: x, axis=1)
65+
tm.assert_frame_equal(result, df)
66+
6167
def test_apply_mixed_datetimelike(self):
6268
# mixed datetimelike
6369
# GH 7778

0 commit comments

Comments
 (0)