Skip to content

Commit

Permalink
BUG: Fix ts precision issue with groupby and NaT (pandas-dev#19526)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbandlow committed Feb 6, 2018
1 parent 3f3b4e0 commit af37225
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ Groupby/Resample/Rolling
- Bug in :func:`DataFrame.resample` which silently ignored unsupported (or mistyped) options for ``label``, ``closed`` and ``convention`` (:issue:`19303`)
- Bug in :func:`DataFrame.groupby` where tuples were interpreted as lists of keys rather than as keys (:issue:`17979`, :issue:`18249`)
- Bug in ``transform`` where particular aggregation functions were being incorrectly cast to match the dtype(s) of the grouped data (:issue:`19200`)
-
- Bug in :func:`DataFrame.groupby` where aggregation by ``first``/``last``/``min``/``max`` was causing timestamps to lose precision (:issue:`19526`)

Sparse
^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2324,7 +2324,7 @@ def _cython_operation(self, kind, values, how, axis, min_count=-1):
result = self._transform(
result, values, labels, func, is_numeric, is_datetimelike)

if is_integer_dtype(result):
if is_integer_dtype(result) and not is_datetimelike:
mask = result == iNaT
if mask.any():
result = result.astype('float64')
Expand Down
20 changes: 19 additions & 1 deletion pandas/tests/groupby/aggregate/test_cython.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
from numpy import nan
import pandas as pd

from pandas import bdate_range, DataFrame, Index, Series
from pandas import (bdate_range, DataFrame, Index, Series, Timestamp,
Timedelta, NaT)
from pandas.core.groupby import DataError
from pandas.core.indexes.numeric import Int64Index
import pandas.util.testing as tm


Expand Down Expand Up @@ -187,3 +189,19 @@ def test_cython_agg_empty_buckets_nanops():
{"a": [1, 1, 1716, 1]},
index=pd.CategoricalIndex(intervals, name='a', ordered=True))
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize('op', ['first', 'last', 'max', 'min'])
@pytest.mark.parametrize('data', [
Timestamp('2016-10-14 21:00:44.557'),
Timedelta('17088 days 21:00:44.557'), ])
def test_cython_with_timestamp_and_nat(op, data):
# https://github.com/pandas-dev/pandas/issues/19526
df = DataFrame({'a': [0, 1], 'b': [data, NaT]})
index = Int64Index([0, 1], dtype='int64', name='a')

# We will group by a and test the cython aggregations
expected = DataFrame({'b': [data, NaT]}, index=index)

result = df.groupby('a').aggregate(op)
tm.assert_frame_equal(expected, result)

0 comments on commit af37225

Please sign in to comment.