Skip to content

Commit d3428c1

Browse files
jbrockmendelmeeseeksmachine
authored andcommitted
Backport PR pandas-dev#38272: BUG: DataFrame.apply with axis=1 and EA dtype
1 parent 3e92680 commit d3428c1

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
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
@@ -407,12 +412,20 @@ def series_generator(self):
407412
mgr = ser._mgr
408413
blk = mgr.blocks[0]
409414

410-
for (arr, name) in zip(values, self.index):
411-
# GH#35462 re-pin mgr in case setitem changed it
412-
ser._mgr = mgr
413-
blk.values = arr
414-
ser.name = name
415-
yield ser
415+
if is_extension_array_dtype(blk.dtype):
416+
# values will be incorrect for this block
417+
# TODO(EA2D): special case would be unnecessary with 2D EAs
418+
obj = self.obj
419+
for i in range(len(obj)):
420+
yield obj._ixs(i, axis=0)
421+
422+
else:
423+
for (arr, name) in zip(values, self.index):
424+
# GH#35462 re-pin mgr in case setitem changed it
425+
ser._mgr = mgr
426+
blk.values = arr
427+
ser.name = name
428+
yield ser
416429

417430
@property
418431
def result_index(self) -> "Index":

pandas/tests/frame/apply/test_frame_apply.py

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

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

0 commit comments

Comments
 (0)