Skip to content

Bugfix/groupby datetime issue #28569

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

Merged
merged 10 commits into from
Oct 3, 2019
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +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` integer indexes for columns referenced by name (: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.

Let's discuss how a publicly-facing function (i.e. one that isn't prefixed by an underscore) was affected by this bug instead in the entry.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I've updated.

-

Timedelta
Expand Down
5 changes: 2 additions & 3 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1904,9 +1904,8 @@ 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])
]
obj_cols = [idx for idx in range(len(result.columns))
if is_object_dtype(result.dtypes.iloc[idx])]

# See GH#26285
for n in obj_cols:
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/groupby/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,3 +657,14 @@ 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()]})
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)