|
3 | 3 |
|
4 | 4 | import numpy as np
|
5 | 5 |
|
6 |
| -from pandas import (DataFrame, Series, MultiIndex, Timestamp) |
| 6 | +from pandas import (DataFrame, Series, MultiIndex, Timestamp, Timedelta, |
| 7 | + Period) |
7 | 8 | from pandas.util.testing import (assert_series_equal, assert_frame_equal)
|
8 | 9 | from pandas.compat import (range, product as cart_product)
|
9 | 10 |
|
@@ -196,13 +197,27 @@ def test_ngroup_respects_groupby_order(self):
|
196 | 197 | assert_series_equal(Series(df['group_index'].values),
|
197 | 198 | g.cumcount())
|
198 | 199 |
|
199 |
| - def test_count_with_datetime(self): |
200 |
| - # test for #13393 |
201 |
| - df = DataFrame({'x': ['a', 'a', 'b'], |
202 |
| - 'y': [Timestamp('2016-05-07 20:09:25+00:00'), |
203 |
| - Timestamp('2016-05-07 20:09:29+00:00'), |
204 |
| - Timestamp('2016-05-07 20:09:29+00:00')]}) |
| 200 | + def test_count_with_datetimelike(self): |
| 201 | + # test for #13393, where DataframeGroupBy.count() fails |
| 202 | + # when counting a datetimelike column. |
| 203 | + |
205 | 204 | expected = DataFrame({'y': [2, 1]}, index=['a', 'b'])
|
206 | 205 | expected.index.name = "x"
|
207 |
| - res = df.groupby('x').count() |
208 |
| - assert_frame_equal(expected, res) |
| 206 | + |
| 207 | + def _test(datetimelike): |
| 208 | + df = DataFrame({'x': ['a', 'a', 'b'], 'y': datetimelike}) |
| 209 | + res = df.groupby('x').count() |
| 210 | + assert_frame_equal(expected, res) |
| 211 | + |
| 212 | + d1 = [Timestamp('2016-05-07 20:09:25+00:00'), |
| 213 | + Timestamp('2016-05-07 20:09:29+00:00'), |
| 214 | + Timestamp('2016-05-07 20:09:29+00:00')] |
| 215 | + _test(d1) |
| 216 | + d2 = [Timedelta(x, unit="h") for x in range(1, 4)] |
| 217 | + _test(d2) |
| 218 | + d3 = [Period(freq="2W", year=2017, month=x) for x in range(1, 4)] |
| 219 | + _test(d3) |
| 220 | + d4 = [Timestamp('2016-05-07 20:09:25'), |
| 221 | + Timestamp('2016-05-07 20:09:29'), |
| 222 | + Timestamp('2016-05-07 20:09:29')] |
| 223 | + _test(d4) |
0 commit comments