|
2 | 2 | date,
|
3 | 3 | datetime,
|
4 | 4 | )
|
5 |
| -from io import StringIO |
6 | 5 |
|
7 | 6 | import numpy as np
|
8 | 7 | import pytest
|
@@ -38,39 +37,80 @@ def store(group):
|
38 | 37 | tm.assert_frame_equal(groups[0], expected_value)
|
39 | 38 |
|
40 | 39 |
|
41 |
| -def test_apply_issues(): |
| 40 | +def test_apply_index_date(): |
42 | 41 | # GH 5788
|
43 |
| - |
44 |
| - s = """2011.05.16,00:00,1.40893 |
45 |
| -2011.05.16,01:00,1.40760 |
46 |
| -2011.05.16,02:00,1.40750 |
47 |
| -2011.05.16,03:00,1.40649 |
48 |
| -2011.05.17,02:00,1.40893 |
49 |
| -2011.05.17,03:00,1.40760 |
50 |
| -2011.05.17,04:00,1.40750 |
51 |
| -2011.05.17,05:00,1.40649 |
52 |
| -2011.05.18,02:00,1.40893 |
53 |
| -2011.05.18,03:00,1.40760 |
54 |
| -2011.05.18,04:00,1.40750 |
55 |
| -2011.05.18,05:00,1.40649""" |
56 |
| - |
57 |
| - df = pd.read_csv( |
58 |
| - StringIO(s), |
59 |
| - header=None, |
60 |
| - names=["date", "time", "value"], |
61 |
| - parse_dates=[["date", "time"]], |
| 42 | + ts = [ |
| 43 | + "2011-05-16 00:00", |
| 44 | + "2011-05-16 01:00", |
| 45 | + "2011-05-16 02:00", |
| 46 | + "2011-05-16 03:00", |
| 47 | + "2011-05-17 02:00", |
| 48 | + "2011-05-17 03:00", |
| 49 | + "2011-05-17 04:00", |
| 50 | + "2011-05-17 05:00", |
| 51 | + "2011-05-18 02:00", |
| 52 | + "2011-05-18 03:00", |
| 53 | + "2011-05-18 04:00", |
| 54 | + "2011-05-18 05:00", |
| 55 | + ] |
| 56 | + df = DataFrame( |
| 57 | + { |
| 58 | + "value": [ |
| 59 | + 1.40893, |
| 60 | + 1.40760, |
| 61 | + 1.40750, |
| 62 | + 1.40649, |
| 63 | + 1.40893, |
| 64 | + 1.40760, |
| 65 | + 1.40750, |
| 66 | + 1.40649, |
| 67 | + 1.40893, |
| 68 | + 1.40760, |
| 69 | + 1.40750, |
| 70 | + 1.40649, |
| 71 | + ], |
| 72 | + }, |
| 73 | + index=Index(pd.to_datetime(ts), name="date_time"), |
62 | 74 | )
|
63 |
| - df = df.set_index("date_time") |
64 |
| - |
65 | 75 | expected = df.groupby(df.index.date).idxmax()
|
66 | 76 | result = df.groupby(df.index.date).apply(lambda x: x.idxmax())
|
67 | 77 | tm.assert_frame_equal(result, expected)
|
68 | 78 |
|
| 79 | + |
| 80 | +def test_apply_index_date_object(): |
69 | 81 | # GH 5789
|
70 | 82 | # don't auto coerce dates
|
71 |
| - df = pd.read_csv(StringIO(s), header=None, names=["date", "time", "value"]) |
| 83 | + ts = [ |
| 84 | + "2011-05-16 00:00", |
| 85 | + "2011-05-16 01:00", |
| 86 | + "2011-05-16 02:00", |
| 87 | + "2011-05-16 03:00", |
| 88 | + "2011-05-17 02:00", |
| 89 | + "2011-05-17 03:00", |
| 90 | + "2011-05-17 04:00", |
| 91 | + "2011-05-17 05:00", |
| 92 | + "2011-05-18 02:00", |
| 93 | + "2011-05-18 03:00", |
| 94 | + "2011-05-18 04:00", |
| 95 | + "2011-05-18 05:00", |
| 96 | + ] |
| 97 | + df = DataFrame([row.split() for row in ts], columns=["date", "time"]) |
| 98 | + df["value"] = [ |
| 99 | + 1.40893, |
| 100 | + 1.40760, |
| 101 | + 1.40750, |
| 102 | + 1.40649, |
| 103 | + 1.40893, |
| 104 | + 1.40760, |
| 105 | + 1.40750, |
| 106 | + 1.40649, |
| 107 | + 1.40893, |
| 108 | + 1.40760, |
| 109 | + 1.40750, |
| 110 | + 1.40649, |
| 111 | + ] |
72 | 112 | exp_idx = Index(
|
73 |
| - ["2011.05.16", "2011.05.17", "2011.05.18"], dtype=object, name="date" |
| 113 | + ["2011-05-16", "2011-05-17", "2011-05-18"], dtype=object, name="date" |
74 | 114 | )
|
75 | 115 | expected = Series(["00:00", "02:00", "02:00"], index=exp_idx)
|
76 | 116 | msg = "DataFrameGroupBy.apply operated on the grouping columns"
|
|
0 commit comments