Skip to content

BUG: Fix issue with incorrect groupby handling of NaT #10625

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
Sep 3, 2015
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion doc/source/whatsnew/v0.17.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -865,4 +865,5 @@ Bug Fixes
- Bug in ``to_json`` which was causing segmentation fault when serializing 0-rank ndarray (:issue:`9576`)
- Bug in plotting functions may raise ``IndexError`` when plotted on ``GridSpec`` (:issue:`10819`)
- Bug in plot result may show unnecessary minor ticklabels (:issue:`10657`)
- Bug when constructing ``DataFrame`` where passing a dictionary with only scalar values and specifying columns did not raise an error (:issue:`10856`)
- Bug in ``groupby`` incorrect computation for aggregation on ``DataFrame`` with ``NaT`` (E.g ``first``, ``last``, ``min``). (:issue:`10590`)
- Bug when constructing ``DataFrame`` where passing a dictionary with only scalar values and specifying columns did not raise an error (:issue:`10856`)
5 changes: 2 additions & 3 deletions pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1532,6 +1532,7 @@ def aggregate(self, values, how, axis=0):

if is_datetime_or_timedelta_dtype(values.dtype):
values = values.view('int64')
values[values == tslib.iNaT] = np.nan
# GH 7754
is_numeric = True
elif is_bool_dtype(values.dtype):
Expand Down Expand Up @@ -2761,9 +2762,7 @@ def _cython_agg_blocks(self, how, numeric_only=True):

for block in data.blocks:

values = block._try_operate(block.values)

result, _ = self.grouper.aggregate(values, how, axis=agg_axis)
result, _ = self.grouper.aggregate(block.values, how, axis=agg_axis)

# see if we can cast the block back to the original dtype
result = block._try_coerce_and_cast_result(result)
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -5413,6 +5413,24 @@ def test_func(x):
expected = DataFrame()
tm.assert_frame_equal(result, expected)

def test_first_last_max_min_on_time_data(self):
# GH 10295
# Verify that NaT is not in the result of max, min, first and last on
# Dataframe with datetime or timedelta values.
from datetime import timedelta as td
df_test=DataFrame({'dt':[nan,'2015-07-24 10:10','2015-07-25 11:11','2015-07-23 12:12',nan],
'td':[nan,td(days=1),td(days=2),td(days=3),nan]})
df_test.dt=pd.to_datetime(df_test.dt)
df_test['group']='A'
df_ref=df_test[df_test.dt.notnull()]

grouped_test=df_test.groupby('group')
grouped_ref=df_ref.groupby('group')

assert_frame_equal(grouped_ref.max(),grouped_test.max())
assert_frame_equal(grouped_ref.min(),grouped_test.min())
assert_frame_equal(grouped_ref.first(),grouped_test.first())
assert_frame_equal(grouped_ref.last(),grouped_test.last())

def assert_fp_equal(a, b):
assert (np.abs(a - b) < 1e-12).all()
Expand Down