Skip to content

Commit af37225

Browse files
committed
BUG: Fix ts precision issue with groupby and NaT (#19526)
1 parent 3f3b4e0 commit af37225

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

doc/source/whatsnew/v0.23.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ Groupby/Resample/Rolling
548548
- Bug in :func:`DataFrame.resample` which silently ignored unsupported (or mistyped) options for ``label``, ``closed`` and ``convention`` (:issue:`19303`)
549549
- Bug in :func:`DataFrame.groupby` where tuples were interpreted as lists of keys rather than as keys (:issue:`17979`, :issue:`18249`)
550550
- Bug in ``transform`` where particular aggregation functions were being incorrectly cast to match the dtype(s) of the grouped data (:issue:`19200`)
551-
-
551+
- Bug in :func:`DataFrame.groupby` where aggregation by ``first``/``last``/``min``/``max`` was causing timestamps to lose precision (:issue:`19526`)
552552

553553
Sparse
554554
^^^^^^

pandas/core/groupby.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2324,7 +2324,7 @@ def _cython_operation(self, kind, values, how, axis, min_count=-1):
23242324
result = self._transform(
23252325
result, values, labels, func, is_numeric, is_datetimelike)
23262326

2327-
if is_integer_dtype(result):
2327+
if is_integer_dtype(result) and not is_datetimelike:
23282328
mask = result == iNaT
23292329
if mask.any():
23302330
result = result.astype('float64')

pandas/tests/groupby/aggregate/test_cython.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
from numpy import nan
1313
import pandas as pd
1414

15-
from pandas import bdate_range, DataFrame, Index, Series
15+
from pandas import (bdate_range, DataFrame, Index, Series, Timestamp,
16+
Timedelta, NaT)
1617
from pandas.core.groupby import DataError
18+
from pandas.core.indexes.numeric import Int64Index
1719
import pandas.util.testing as tm
1820

1921

@@ -187,3 +189,19 @@ def test_cython_agg_empty_buckets_nanops():
187189
{"a": [1, 1, 1716, 1]},
188190
index=pd.CategoricalIndex(intervals, name='a', ordered=True))
189191
tm.assert_frame_equal(result, expected)
192+
193+
194+
@pytest.mark.parametrize('op', ['first', 'last', 'max', 'min'])
195+
@pytest.mark.parametrize('data', [
196+
Timestamp('2016-10-14 21:00:44.557'),
197+
Timedelta('17088 days 21:00:44.557'), ])
198+
def test_cython_with_timestamp_and_nat(op, data):
199+
# https://github.com/pandas-dev/pandas/issues/19526
200+
df = DataFrame({'a': [0, 1], 'b': [data, NaT]})
201+
index = Int64Index([0, 1], dtype='int64', name='a')
202+
203+
# We will group by a and test the cython aggregations
204+
expected = DataFrame({'b': [data, NaT]}, index=index)
205+
206+
result = df.groupby('a').aggregate(op)
207+
tm.assert_frame_equal(expected, result)

0 commit comments

Comments
 (0)