Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG/ENH: groupby.quantile support non-nano #50978

Merged
merged 1 commit into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -3196,10 +3196,10 @@ def pre_processor(vals: ArrayLike) -> tuple[np.ndarray, Dtype | None]:
elif is_bool_dtype(vals.dtype) and isinstance(vals, ExtensionArray):
out = vals.to_numpy(dtype=float, na_value=np.nan)
elif is_datetime64_dtype(vals.dtype):
inference = np.dtype("datetime64[ns]")
inference = vals.dtype
out = np.asarray(vals).astype(float)
elif is_timedelta64_dtype(vals.dtype):
inference = np.dtype("timedelta64[ns]")
inference = vals.dtype
out = np.asarray(vals).astype(float)
elif isinstance(vals, ExtensionArray) and is_float_dtype(vals):
inference = np.dtype(np.float64)
Expand Down
24 changes: 18 additions & 6 deletions pandas/tests/groupby/test_quantile.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,45 @@
([np.nan, 4.0, np.nan, 2.0, np.nan], [np.nan, 4.0, np.nan, 2.0, np.nan]),
# Timestamps
(
list(pd.date_range("1/1/18", freq="D", periods=5)),
list(pd.date_range("1/1/18", freq="D", periods=5))[::-1],
pd.date_range("1/1/18", freq="D", periods=5),
pd.date_range("1/1/18", freq="D", periods=5)[::-1],
),
(
pd.date_range("1/1/18", freq="D", periods=5).as_unit("s"),
pd.date_range("1/1/18", freq="D", periods=5)[::-1].as_unit("s"),
),
# All NA
([np.nan] * 5, [np.nan] * 5),
],
)
@pytest.mark.parametrize("q", [0, 0.25, 0.5, 0.75, 1])
def test_quantile(interpolation, a_vals, b_vals, q, request):
if interpolation == "nearest" and q == 0.5 and b_vals == [4, 3, 2, 1]:
if (
interpolation == "nearest"
and q == 0.5
and isinstance(b_vals, list)
and b_vals == [4, 3, 2, 1]
):
request.node.add_marker(
pytest.mark.xfail(
reason="Unclear numpy expectation for nearest "
"result with equidistant data"
)
)
all_vals = pd.concat([pd.Series(a_vals), pd.Series(b_vals)])

a_expected = pd.Series(a_vals).quantile(q, interpolation=interpolation)
b_expected = pd.Series(b_vals).quantile(q, interpolation=interpolation)

df = DataFrame(
{"key": ["a"] * len(a_vals) + ["b"] * len(b_vals), "val": a_vals + b_vals}
)
df = DataFrame({"key": ["a"] * len(a_vals) + ["b"] * len(b_vals), "val": all_vals})

expected = DataFrame(
[a_expected, b_expected], columns=["val"], index=Index(["a", "b"], name="key")
)
if all_vals.dtype.kind == "M" and expected.dtypes.values[0].kind == "M":
# TODO(non-nano): this should be unnecessary once array_to_datetime
# correctly infers non-nano from Timestamp.unit
expected = expected.astype(all_vals.dtype)
result = df.groupby("key").quantile(q, interpolation=interpolation)

tm.assert_frame_equal(result, expected)
Expand Down
8 changes: 5 additions & 3 deletions pandas/tests/resample/test_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,12 @@ def test_resample_with_timedelta_yields_no_empty_groups(duplicates):
tm.assert_frame_equal(result, expected)


def test_resample_quantile_timedelta():
@pytest.mark.parametrize("unit", ["s", "ms", "us", "ns"])
def test_resample_quantile_timedelta(unit):
# GH: 29485
dtype = np.dtype(f"m8[{unit}]")
df = DataFrame(
{"value": pd.to_timedelta(np.arange(4), unit="s")},
{"value": pd.to_timedelta(np.arange(4), unit="s").astype(dtype)},
index=pd.date_range("20200101", periods=4, tz="UTC"),
)
result = df.resample("2D").quantile(0.99)
Expand All @@ -189,7 +191,7 @@ def test_resample_quantile_timedelta():
]
},
index=pd.date_range("20200101", periods=2, tz="UTC", freq="2D"),
)
).astype(dtype)
tm.assert_frame_equal(result, expected)


Expand Down