Skip to content

Commit f1befd1

Browse files
committed
BUG: integer indexes incorrectly referenced by name pandas-dev#28247
1 parent f08a1e6 commit f1befd1

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

doc/source/whatsnew/v1.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ Datetimelike
181181
- Bug in :class:`Series` and :class:`DataFrame` with integer dtype failing to raise ``TypeError`` when adding or subtracting a ``np.datetime64`` object (:issue:`28080`)
182182
- Bug in :class:`Week` with ``weekday`` incorrectly raising ``AttributeError`` instead of ``TypeError`` when adding or subtracting an invalid type (:issue:`28530`)
183183
- Bug in :class:`DataFrame` arithmetic operations when operating with a :class:`Series` with dtype `'timedelta64[ns]'` (:issue:`28049`)
184+
- Bug in :func:`pandas.core.groupby.generic._recast_datetimelike_result` integer indexes for columns referenced by name (:issue:`28247`)
184185
-
185186

186187
Timedelta

pandas/core/groupby/generic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1905,7 +1905,7 @@ def _recast_datetimelike_result(result: DataFrame) -> DataFrame:
19051905
result = result.copy()
19061906

19071907
obj_cols = [
1908-
idx for idx in range(len(result.columns)) if is_object_dtype(result.dtypes[idx])
1908+
idx for idx in range(len(result.columns)) if is_object_dtype(result.dtypes.iloc[idx])
19091909
]
19101910

19111911
# See GH#26285

pandas/tests/groupby/test_apply.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,3 +657,13 @@ def test_apply_with_mixed_types():
657657

658658
result = g.apply(lambda x: x / x.sum())
659659
tm.assert_frame_equal(result, expected)
660+
661+
662+
def test_apply_datetime_issue():
663+
# GH-28247
664+
665+
df = pd.DataFrame({'a': ['foo'], 'b': [datetime.today()]})
666+
result = df.groupby('a').apply(lambda x: pd.Series(['spam'], index=[42]))
667+
668+
expected = pd.DataFrame(['spam'], Index(['foo'], dtype='object', name='a'), columns=[42])
669+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)