Skip to content

Commit 9113529

Browse files
sidharthannproost
authored andcommitted
Bugfix/groupby datetime issue (pandas-dev#28569)
1 parent ee4910e commit 9113529

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

doc/source/whatsnew/v1.0.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ Datetimelike
191191
- Bug in :class:`Series` and :class:`DataFrame` with integer dtype failing to raise ``TypeError`` when adding or subtracting a ``np.datetime64`` object (:issue:`28080`)
192192
- Bug in :class:`Week` with ``weekday`` incorrectly raising ``AttributeError`` instead of ``TypeError`` when adding or subtracting an invalid type (:issue:`28530`)
193193
- Bug in :class:`DataFrame` arithmetic operations when operating with a :class:`Series` with dtype `'timedelta64[ns]'` (:issue:`28049`)
194-
-
194+
- Bug in :func:`pandas.core.groupby.generic.SeriesGroupBy.apply` raising ``ValueError`` when a column in the original DataFrame is a datetime and the column labels are not standard integers (:issue:`28247`)
195195

196196
Timedelta
197197
^^^^^^^^^

pandas/core/groupby/generic.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1913,7 +1913,9 @@ def _recast_datetimelike_result(result: DataFrame) -> DataFrame:
19131913
result = result.copy()
19141914

19151915
obj_cols = [
1916-
idx for idx in range(len(result.columns)) if is_object_dtype(result.dtypes[idx])
1916+
idx
1917+
for idx in range(len(result.columns))
1918+
if is_object_dtype(result.dtypes.iloc[idx])
19171919
]
19181920

19191921
# See GH#26285

pandas/tests/groupby/test_apply.py

+19
Original file line numberDiff line numberDiff line change
@@ -657,3 +657,22 @@ 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+
@pytest.mark.parametrize(
663+
"group_column_dtlike",
664+
[datetime.today(), datetime.today().date(), datetime.today().time()],
665+
)
666+
def test_apply_datetime_issue(group_column_dtlike):
667+
# GH-28247
668+
# groupby-apply throws an error if one of the columns in the DataFrame
669+
# is a datetime object and the column labels are different from
670+
# standard int values in range(len(num_columns))
671+
672+
df = pd.DataFrame({"a": ["foo"], "b": [group_column_dtlike]})
673+
result = df.groupby("a").apply(lambda x: pd.Series(["spam"], index=[42]))
674+
675+
expected = pd.DataFrame(
676+
["spam"], Index(["foo"], dtype="object", name="a"), columns=[42]
677+
)
678+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)