Skip to content

Commit cf562f0

Browse files
committed
WIP
1 parent 9d24903 commit cf562f0

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

pandas/core/frame.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13045,6 +13045,7 @@ def quantile(
1304513045
C 1 days 12:00:00
1304613046
Name: 0.5, dtype: object
1304713047
"""
13048+
from pandas.core.dtypes.common import is_object_dtype
1304813049
validate_percentile(q)
1304913050
axis = self._get_axis_number(axis)
1305013051

@@ -13064,7 +13065,7 @@ def quantile(
1306413065
# GH#41544 try to get an appropriate dtype
1306513066
dtype = "float64"
1306613067
cdtype = find_common_type(list(self.dtypes))
13067-
if needs_i8_conversion(cdtype):
13068+
if needs_i8_conversion(cdtype) or is_object_dtype(cdtype):
1306813069
dtype = cdtype
1306913070
return res.astype(dtype)
1307013071
return res
@@ -13083,7 +13084,7 @@ def quantile(
1308313084
if axis == 1:
1308413085
# GH#41544 try to get an appropriate dtype
1308513086
cdtype = find_common_type(list(self.dtypes))
13086-
if needs_i8_conversion(cdtype):
13087+
if needs_i8_conversion(cdtype) or is_object_dtype(cdtype):
1308713088
dtype = cdtype
1308813089

1308913090
res = self._constructor([], index=q, columns=cols, dtype=dtype)
@@ -13094,6 +13095,18 @@ def quantile(
1309413095
raise ValueError(
1309513096
f"Invalid method: {method}. Method must be in {valid_method}."
1309613097
)
13098+
13099+
# handle degenerate case
13100+
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)
13109+
1309713110
if method == "single":
1309813111
res = data._mgr.quantile(qs=q, interpolation=interpolation)
1309913112
elif method == "table":
@@ -13103,13 +13116,6 @@ def quantile(
1310313116
f"Invalid interpolation: {interpolation}. "
1310413117
f"Interpolation must be in {valid_interpolation}"
1310513118
)
13106-
# handle degenerate case
13107-
if len(data) == 0:
13108-
if data.ndim == 2:
13109-
dtype = find_common_type(list(self.dtypes))
13110-
else:
13111-
dtype = self.dtype
13112-
return self._constructor([], index=q, columns=data.columns, dtype=dtype)
1311313119

1311413120
q_idx = np.quantile(np.arange(len(data)), q, method=interpolation)
1311513121

pandas/tests/frame/methods/test_quantile.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,8 @@ 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"
322+
# dtype = "float64" if method == "single" else "object"
323+
dtype = "object"
323324
expected = DataFrame(
324325
{"x": [np.nan, np.nan], "y": [np.nan, np.nan]},
325326
index=[0.1, 0.9],
@@ -692,7 +693,7 @@ def test_quantile_empty_no_rows_dt64(self, interp_method):
692693
0.5, numeric_only=False, interpolation=interpolation, method=method
693694
)
694695
exp = exp.astype(object)
695-
if interpolation == "nearest":
696+
if True or interpolation == "nearest":
696697
# GH#18463 TODO: would we prefer NaTs here?
697698
exp = exp.fillna(np.nan)
698699
tm.assert_series_equal(res, exp)
@@ -911,7 +912,7 @@ def test_empty_datelike(
911912
@pytest.mark.parametrize(
912913
"expected_data, expected_index, axis",
913914
[
914-
[[np.nan, np.nan], range(2), 1],
915+
[[pd.NaT, pd.NaT], range(2), 1],
915916
[[], [], 0],
916917
],
917918
)
@@ -926,6 +927,6 @@ def test_datelike_numeric_only(self, expected_data, expected_index, axis):
926927
)
927928
result = df[["a", "c"]].quantile(0.5, axis=axis, numeric_only=True)
928929
expected = Series(
929-
expected_data, name=0.5, index=Index(expected_index), dtype=np.float64
930+
expected_data, name=0.5, index=Index(expected_index)
930931
)
931932
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)