Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/groupby datetime issue #28569

Merged
merged 10 commits into from
Oct 3, 2019
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ Datetimelike
- Bug in :class:`Series` and :class:`DataFrame` with integer dtype failing to raise ``TypeError`` when adding or subtracting a ``np.datetime64`` object (:issue:`28080`)
- Bug in :class:`Week` with ``weekday`` incorrectly raising ``AttributeError`` instead of ``TypeError`` when adding or subtracting an invalid type (:issue:`28530`)
- Bug in :class:`DataFrame` arithmetic operations when operating with a :class:`Series` with dtype `'timedelta64[ns]'` (:issue:`28049`)
-
- Bug in :func:`pandas.core.groupby.generic._recast_datetimelike_result` causing ``KeyError`` in :func:`pandas.core.groupby.generic.SeriesGroupBy.apply` when a column index in the original DataFrame is datetime like(:issue:`28247`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no space between "datetime" and "like", space after "like".

The only test is for a datetime object. Does this also work for the other things we usually call "datetimelike"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi, i have updated as per your suggestion, and added a few more test cases


Timedelta
^^^^^^^^^
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1905,7 +1905,9 @@ def _recast_datetimelike_result(result: DataFrame) -> DataFrame:
result = result.copy()

obj_cols = [
idx for idx in range(len(result.columns)) if is_object_dtype(result.dtypes[idx])
idx
for idx in range(len(result.columns))
if is_object_dtype(result.dtypes.iloc[idx])
]

# See GH#26285
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/groupby/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,3 +657,15 @@ def test_apply_with_mixed_types():

result = g.apply(lambda x: x / x.sum())
tm.assert_frame_equal(result, expected)


def test_apply_datetime_issue():
# GH-28247
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a 1-line comment on what the issue is


df = pd.DataFrame({"a": ["foo"], "b": [datetime.today()]})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you parameterize these tests, only thing changing is the b column which you can pass in the paramters

result = df.groupby("a").apply(lambda x: pd.Series(["spam"], index=[42]))

expected = pd.DataFrame(
["spam"], Index(["foo"], dtype="object", name="a"), columns=[42]
)
tm.assert_frame_equal(result, expected)