|
26 | 26 | ([np.nan, 4.0, np.nan, 2.0, np.nan], [np.nan, 4.0, np.nan, 2.0, np.nan]), |
27 | 27 | # Timestamps |
28 | 28 | ( |
29 | | - list(pd.date_range("1/1/18", freq="D", periods=5)), |
30 | | - list(pd.date_range("1/1/18", freq="D", periods=5))[::-1], |
| 29 | + pd.date_range("1/1/18", freq="D", periods=5), |
| 30 | + pd.date_range("1/1/18", freq="D", periods=5)[::-1], |
| 31 | + ), |
| 32 | + ( |
| 33 | + pd.date_range("1/1/18", freq="D", periods=5).as_unit("s"), |
| 34 | + pd.date_range("1/1/18", freq="D", periods=5)[::-1].as_unit("s"), |
31 | 35 | ), |
32 | 36 | # All NA |
33 | 37 | ([np.nan] * 5, [np.nan] * 5), |
34 | 38 | ], |
35 | 39 | ) |
36 | 40 | @pytest.mark.parametrize("q", [0, 0.25, 0.5, 0.75, 1]) |
37 | 41 | def test_quantile(interpolation, a_vals, b_vals, q, request): |
38 | | - if interpolation == "nearest" and q == 0.5 and b_vals == [4, 3, 2, 1]: |
| 42 | + if ( |
| 43 | + interpolation == "nearest" |
| 44 | + and q == 0.5 |
| 45 | + and isinstance(b_vals, list) |
| 46 | + and b_vals == [4, 3, 2, 1] |
| 47 | + ): |
39 | 48 | request.node.add_marker( |
40 | 49 | pytest.mark.xfail( |
41 | 50 | reason="Unclear numpy expectation for nearest " |
42 | 51 | "result with equidistant data" |
43 | 52 | ) |
44 | 53 | ) |
| 54 | + all_vals = pd.concat([pd.Series(a_vals), pd.Series(b_vals)]) |
45 | 55 |
|
46 | 56 | a_expected = pd.Series(a_vals).quantile(q, interpolation=interpolation) |
47 | 57 | b_expected = pd.Series(b_vals).quantile(q, interpolation=interpolation) |
48 | 58 |
|
49 | | - df = DataFrame( |
50 | | - {"key": ["a"] * len(a_vals) + ["b"] * len(b_vals), "val": a_vals + b_vals} |
51 | | - ) |
| 59 | + df = DataFrame({"key": ["a"] * len(a_vals) + ["b"] * len(b_vals), "val": all_vals}) |
52 | 60 |
|
53 | 61 | expected = DataFrame( |
54 | 62 | [a_expected, b_expected], columns=["val"], index=Index(["a", "b"], name="key") |
55 | 63 | ) |
| 64 | + if all_vals.dtype.kind == "M" and expected.dtypes.values[0].kind == "M": |
| 65 | + # TODO(non-nano): this should be unnecessary once array_to_datetime |
| 66 | + # correctly infers non-nano from Timestamp.unit |
| 67 | + expected = expected.astype(all_vals.dtype) |
56 | 68 | result = df.groupby("key").quantile(q, interpolation=interpolation) |
57 | 69 |
|
58 | 70 | tm.assert_frame_equal(result, expected) |
|
0 commit comments