Skip to content

BUG: DataFrame[dt64].quantile(axis=1) when empty returning f8 #45294

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

Merged
merged 2 commits into from
Jan 13, 2022
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.5.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ Categorical

Datetimelike
^^^^^^^^^^^^
- Bug in :meth:`DataFrame.quantile` with datetime-like dtypes and no rows incorrectly returning ``float64`` dtype instead of retaining datetime-like dtype (:issue:`41544`)
- Bug in :func:`to_datetime` with sequences of ``np.str_`` objects incorrectly raising (:issue:`32264`)
-

Expand Down
25 changes: 20 additions & 5 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
is_object_dtype,
is_scalar,
is_sequence,
needs_i8_conversion,
pandas_dtype,
)
from pandas.core.dtypes.dtypes import ExtensionDtype
Expand Down Expand Up @@ -10462,27 +10463,41 @@ def quantile(
Name: 0.5, dtype: object
"""
validate_percentile(q)
axis = self._get_axis_number(axis)

if not is_list_like(q):
# BlockManager.quantile expects listlike, so we wrap and unwrap here
res = self.quantile(
res_df = self.quantile(
[q], axis=axis, numeric_only=numeric_only, interpolation=interpolation
)
return res.iloc[0]
res = res_df.iloc[0]
if axis == 1 and len(self) == 0:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we do this in quantile itself?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, these cases are only reached when we dont have any blocks to operate on.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm really ought to push this down to eliminate this type of logic in the frame itself, ok for now.

# GH#41544 try to get an appropriate dtype
dtype = find_common_type(list(self.dtypes))
if needs_i8_conversion(dtype):
return res.astype(dtype)
return res

q = Index(q, dtype=np.float64)
data = self._get_numeric_data() if numeric_only else self
axis = self._get_axis_number(axis)

if axis == 1:
data = data.T

if len(data.columns) == 0:
# GH#23925 _get_numeric_data may have dropped all columns
cols = Index([], name=self.columns.name)

dtype = np.float64
if axis == 1:
# GH#41544 try to get an appropriate dtype
cdtype = find_common_type(list(self.dtypes))
if needs_i8_conversion(cdtype):
dtype = cdtype

if is_list_like(q):
return self._constructor([], index=q, columns=cols)
return self._constructor_sliced([], index=cols, name=q, dtype=np.float64)
return self._constructor([], index=q, columns=cols, dtype=dtype)
return self._constructor_sliced([], index=cols, name=q, dtype=dtype)

res = data._mgr.quantile(qs=q, axis=1, interpolation=interpolation)

Expand Down
31 changes: 23 additions & 8 deletions pandas/tests/frame/methods/test_quantile.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,28 @@ def test_quantile_datetime(self):
expected = DataFrame(index=[0.5])
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize(
"dtype",
[
"datetime64[ns]",
"datetime64[ns, US/Pacific]",
"timedelta64[ns]",
"Period[D]",
],
)
def test_quantile_dt64_empty(self, dtype):
# GH#41544
df = DataFrame(columns=["a", "b"], dtype=dtype)

res = df.quantile(0.5, axis=1, numeric_only=False)
expected = Series([], index=[], name=0.5, dtype=dtype)
tm.assert_series_equal(res, expected)

# no columns in result, so no dtype preservation
res = df.quantile([0.5], axis=1, numeric_only=False)
expected = DataFrame(index=[0.5])
tm.assert_frame_equal(res, expected)

def test_quantile_invalid(self, datetime_frame):
msg = "percentiles should all be in the interval \\[0, 1\\]"
for invalid in [-1, 2, [0.5, -1], [0.5, 2]]:
Expand Down Expand Up @@ -722,14 +744,7 @@ def test_empty_numeric(self, dtype, expected_data, expected_index, axis):
@pytest.mark.parametrize(
"dtype, expected_data, expected_index, axis, expected_dtype",
[
pytest.param(
"datetime64[ns]",
[],
[],
1,
"datetime64[ns]",
marks=pytest.mark.xfail(reason="#GH 41544"),
),
["datetime64[ns]", [], [], 1, "datetime64[ns]"],
["datetime64[ns]", [pd.NaT, pd.NaT], ["a", "b"], 0, "datetime64[ns]"],
],
)
Expand Down