Skip to content

CLN: remove _recast_datetimelike_result #38156

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 1 commit into from
Nov 30, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 1 addition & 39 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
is_integer_dtype,
is_interval_dtype,
is_numeric_dtype,
is_object_dtype,
is_scalar,
needs_i8_conversion,
)
Expand Down Expand Up @@ -1283,7 +1282,7 @@ def _wrap_applied_output_series(
# as we are stacking can easily have object dtypes here
so = self._selected_obj
if so.ndim == 2 and so.dtypes.apply(needs_i8_conversion).any():
result = _recast_datetimelike_result(result)
result = result._convert(datetime=True)
Copy link
Contributor

Choose a reason for hiding this comment

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

.apply(to_datetime) ?

Copy link
Member Author

Choose a reason for hiding this comment

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

we only want to do it on object-dtype columns which is what _convert does

Copy link
Contributor

Choose a reason for hiding this comment

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

kk cool, then this is fine. ping on green.

Copy link
Member Author

Choose a reason for hiding this comment

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

ping

else:
result = result._convert(datetime=True)

Expand Down Expand Up @@ -1836,40 +1835,3 @@ def nunique(self, dropna: bool = True) -> DataFrame:
return results

boxplot = boxplot_frame_groupby


def _recast_datetimelike_result(result: DataFrame) -> DataFrame:
"""
If we have date/time like in the original, then coerce dates
as we are stacking can easily have object dtypes here.

Parameters
----------
result : DataFrame

Returns
-------
DataFrame

Notes
-----
- Assumes Groupby._selected_obj has ndim==2 and at least one
datetimelike column
"""
result = result.copy()

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:
values = result.iloc[:, n].values
converted = lib.maybe_convert_objects(
values, convert_datetime=True, convert_timedelta=True
)

result.iloc[:, n] = converted
return result