Skip to content

Commit 088273b

Browse files
committed
Rework quantile
1 parent cf562f0 commit 088273b

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

pandas/core/frame.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
can_hold_element,
8484
construct_1d_arraylike_from_scalar,
8585
construct_2d_arraylike_from_scalar,
86+
ensure_dtype_can_hold_na,
8687
find_common_type,
8788
infer_dtype_from_scalar,
8889
invalidate_string_dtypes,
@@ -13046,6 +13047,7 @@ def quantile(
1304613047
Name: 0.5, dtype: object
1304713048
"""
1304813049
from pandas.core.dtypes.common import is_object_dtype
13050+
1304913051
validate_percentile(q)
1305013052
axis = self._get_axis_number(axis)
1305113053

@@ -13075,6 +13077,10 @@ def quantile(
1307513077

1307613078
if axis == 1:
1307713079
data = data.T
13080+
if data.shape[0] == 0:
13081+
# The transpose has no rows, so the original has no columns, meaning we
13082+
# have no dtype information. Since this is quantile, default to float64
13083+
data = data.astype("float64")
1307813084

1307913085
if len(data.columns) == 0:
1308013086
# GH#23925 _get_numeric_data may have dropped all columns
@@ -13098,14 +13104,17 @@ def quantile(
1309813104

1309913105
# handle degenerate case
1310013106
if len(data) == 0:
13101-
dtype = np.float64
13102-
if data.ndim == 2:
13103-
cdtype = find_common_type(list(self.dtypes))
13104-
else:
13105-
cdtype = self.dtype
13106-
if needs_i8_conversion(cdtype) or is_object_dtype(cdtype):
13107-
dtype = cdtype
13108-
return self._constructor([], index=q, columns=data.columns, dtype=dtype)
13107+
from pandas import array
13108+
13109+
result = self._constructor(
13110+
{
13111+
idx: array(len(q) * [np.nan], dtype=ensure_dtype_can_hold_na(dtype))
13112+
for idx, dtype in enumerate(data.dtypes)
13113+
},
13114+
index=q,
13115+
)
13116+
result.columns = data.columns
13117+
return result
1310913118

1311013119
if method == "single":
1311113120
res = data._mgr.quantile(qs=q, interpolation=interpolation)

pandas/tests/frame/methods/test_quantile.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -319,12 +319,10 @@ def test_quantile_multi_empty(self, interp_method):
319319
result = DataFrame({"x": [], "y": []}).quantile(
320320
[0.1, 0.9], axis=0, interpolation=interpolation, method=method
321321
)
322-
# dtype = "float64" if method == "single" else "object"
323-
dtype = "object"
324322
expected = DataFrame(
325323
{"x": [np.nan, np.nan], "y": [np.nan, np.nan]},
326324
index=[0.1, 0.9],
327-
dtype=dtype,
325+
dtype="object",
328326
)
329327
tm.assert_frame_equal(result, expected)
330328

@@ -692,10 +690,8 @@ def test_quantile_empty_no_rows_dt64(self, interp_method):
692690
res = df.quantile(
693691
0.5, numeric_only=False, interpolation=interpolation, method=method
694692
)
695-
exp = exp.astype(object)
696-
if True or interpolation == "nearest":
697-
# GH#18463 TODO: would we prefer NaTs here?
698-
exp = exp.fillna(np.nan)
693+
# GH#18463 TODO: would we prefer NaTs here?
694+
exp = exp.astype(object).fillna(pd.NaT)
699695
tm.assert_series_equal(res, exp)
700696

701697
# both dt64tz
@@ -912,7 +908,7 @@ def test_empty_datelike(
912908
@pytest.mark.parametrize(
913909
"expected_data, expected_index, axis",
914910
[
915-
[[pd.NaT, pd.NaT], range(2), 1],
911+
[[np.nan, np.nan], range(2), 1],
916912
[[], [], 0],
917913
],
918914
)
@@ -927,6 +923,6 @@ def test_datelike_numeric_only(self, expected_data, expected_index, axis):
927923
)
928924
result = df[["a", "c"]].quantile(0.5, axis=axis, numeric_only=True)
929925
expected = Series(
930-
expected_data, name=0.5, index=Index(expected_index)
926+
expected_data, name=0.5, index=Index(expected_index), dtype=np.float64
931927
)
932928
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)