|
9 | 9 | import numpy as np
|
10 | 10 | import pytest
|
11 | 11 |
|
12 |
| -from pandas import DataFrame, MultiIndex, Series, date_range |
| 12 | +from pandas import DataFrame, Grouper, MultiIndex, Series, date_range, to_datetime |
13 | 13 | import pandas.util.testing as tm
|
14 | 14 |
|
15 | 15 |
|
@@ -79,3 +79,31 @@ def rebuild_index(df):
|
79 | 79 | # have to sort on index because of unstable sort on values
|
80 | 80 | left, right = map(rebuild_index, (left, right)) # xref GH9212
|
81 | 81 | tm.assert_series_equal(left.sort_index(), right.sort_index())
|
| 82 | + |
| 83 | + |
| 84 | +def test_series_groupby_value_counts_with_grouper(): |
| 85 | + # GH28479 |
| 86 | + df = DataFrame( |
| 87 | + { |
| 88 | + "Timestamp": [ |
| 89 | + 1565083561, |
| 90 | + 1565083561 + 86400, |
| 91 | + 1565083561 + 86500, |
| 92 | + 1565083561 + 86400 * 2, |
| 93 | + 1565083561 + 86400 * 3, |
| 94 | + 1565083561 + 86500 * 3, |
| 95 | + 1565083561 + 86400 * 4, |
| 96 | + ], |
| 97 | + "Food": ["apple", "apple", "banana", "banana", "orange", "orange", "pear"], |
| 98 | + } |
| 99 | + ).drop([3]) |
| 100 | + |
| 101 | + df["Datetime"] = to_datetime(df["Timestamp"].apply(lambda t: str(t)), unit="s") |
| 102 | + dfg = df.groupby(Grouper(freq="1D", key="Datetime")) |
| 103 | + |
| 104 | + # have to sort on index because of unstable sort on values xref GH9212 |
| 105 | + result = dfg["Food"].value_counts().sort_index() |
| 106 | + expected = dfg["Food"].apply(Series.value_counts).sort_index() |
| 107 | + expected.index.names = result.index.names |
| 108 | + |
| 109 | + tm.assert_series_equal(result, expected) |
0 commit comments