Skip to content

Commit cbe3e20

Browse files
committed
Merge pull request #5870 from jreback/groupby_ts
BUG: Bug in groupby dtype conversion with datetimelike (GH5869)
2 parents e23bd2c + 6e015ee commit cbe3e20

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

doc/source/release.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ Bug Fixes
8080
- Fix issue of boolean comparison on empty DataFrames (:issue:`5808`)
8181
- Bug in isnull handling ``NaT`` in an object array (:issue:`5443`)
8282
- Bug in ``to_datetime`` when passed a ``np.nan`` or integer datelike and a format string (:issue:`5863`)
83+
- Bug in groupby dtype conversion with datetimelike (:issue:`5869`)
8384

8485
pandas 0.13.0
8586
-------------

pandas/core/common.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,14 @@ def _possibly_downcast_to_dtype(result, dtype):
11021102
# hit here
11031103
if (new_result == result).all():
11041104
return new_result
1105+
1106+
# a datetimelike
1107+
elif dtype.kind in ['M','m'] and result.dtype.kind in ['i']:
1108+
try:
1109+
result = result.astype(dtype)
1110+
except:
1111+
pass
1112+
11051113
except:
11061114
pass
11071115

pandas/tests/test_groupby.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2672,6 +2672,14 @@ def test_groupby_first_datetime64(self):
26722672
got_dt = result.dtype
26732673
self.assert_(issubclass(got_dt.type, np.datetime64))
26742674

2675+
def test_groupby_max_datetime64(self):
2676+
# GH 5869
2677+
# datetimelike dtype conversion from int
2678+
df = DataFrame(dict(A = Timestamp('20130101'), B = np.arange(5)))
2679+
expected = df.groupby('A')['A'].apply(lambda x: x.max())
2680+
result = df.groupby('A')['A'].max()
2681+
assert_series_equal(result,expected)
2682+
26752683
def test_groupby_categorical_unequal_len(self):
26762684
import pandas as pd
26772685
#GH3011

0 commit comments

Comments
 (0)