Skip to content

ENH: show percentiles in timestamp describe (#30164) #30209

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 3 commits into from
Jan 20, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
43 changes: 22 additions & 21 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

from pandas._config import config

from pandas._libs import Timestamp, iNaT, lib, properties
from pandas._libs import NaT, Timestamp, iNaT, lib, properties
from pandas._typing import (
Axis,
Dtype,
Expand Down Expand Up @@ -9626,26 +9626,8 @@ def describe_categorical_1d(data):
dtype = None
if result[1] > 0:
top, freq = objcounts.index[0], objcounts.iloc[0]

if is_datetime64_any_dtype(data):
tz = data.dt.tz
asint = data.dropna().values.view("i8")
top = Timestamp(top)
if top.tzinfo is not None and tz is not None:
# Don't tz_localize(None) if key is already tz-aware
top = top.tz_convert(tz)
else:
top = top.tz_localize(tz)
names += ["top", "freq", "first", "last"]
result += [
top,
freq,
Timestamp(asint.min(), tz=tz),
Timestamp(asint.max(), tz=tz),
]
else:
names += ["top", "freq"]
result += [top, freq]
names += ["top", "freq"]
result += [top, freq]

# If the DataFrame is empty, set 'top' and 'freq' to None
# to maintain output shape consistency
Expand All @@ -9656,11 +9638,30 @@ def describe_categorical_1d(data):

return pd.Series(result, index=names, name=data.name, dtype=dtype)

def describe_timestamp_1d(data):
# GH-30164
tz = data.dt.tz
Copy link
Member

@gfyoung gfyoung Dec 12, 2019

Choose a reason for hiding this comment

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

Reference issue number above this line.

asint = data.dropna().values.view("i8")
Copy link
Contributor

Choose a reason for hiding this comment

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

you don't need the .values.view, just use len(data.dropna()) for the shape check

data.min() and so on all work and return NaT correctly, none of these gymnastics are needed

Copy link
Contributor

Choose a reason for hiding this comment

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

I know that's what the current code does, but let's simplify

is_empty = asint.shape[0] == 0
stat_index = ["count", "mean", "min"] + formatted_percentiles + ["max"]
d = (
[
asint.shape[0],
Timestamp(asint.mean(), tz=tz) if not is_empty else NaT,
Timestamp(asint.min(), tz=tz) if not is_empty else NaT,
]
+ data.quantile(percentiles).tolist()
+ [Timestamp(asint.max(), tz=tz) if not is_empty else NaT]
)
return pd.Series(d, index=stat_index, name=data.name)

def describe_1d(data):
if is_bool_dtype(data):
return describe_categorical_1d(data)
elif is_numeric_dtype(data):
return describe_numeric_1d(data)
elif is_datetime64_any_dtype(data):
return describe_timestamp_1d(data)
elif is_timedelta64_dtype(data):
return describe_numeric_1d(data)
else:
Expand Down
45 changes: 6 additions & 39 deletions pandas/tests/frame/methods/test_describe.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,52 +253,19 @@ def test_describe_tz_values(self, tz_naive_fixture):

expected = DataFrame(
{
"s1": [
5,
np.nan,
np.nan,
np.nan,
np.nan,
np.nan,
2,
1.581139,
0,
1,
2,
3,
4,
],
"s1": [5, 2, 0, 1, 2, 3, 4, 1.581139],
Copy link
Contributor

Choose a reason for hiding this comment

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

do we have sufficient tests for timedeltas?

can you create a test for Period (which likely don't work), if you'd just xfail it (alt if you'd create an issue)

"s2": [
5,
5,
s2.value_counts().index[0],
1,
Timestamp(2018, 1, 3).tz_localize(tz),
start.tz_localize(tz),
s2[1],
s2[2],
s2[3],
end.tz_localize(tz),
np.nan,
np.nan,
np.nan,
np.nan,
np.nan,
np.nan,
np.nan,
],
},
index=[
"count",
"unique",
"top",
"freq",
"first",
"last",
"mean",
"std",
"min",
"25%",
"50%",
"75%",
"max",
],
index=["count", "mean", "min", "25%", "50%", "75%", "max", "std"],
)
result = df.describe(include="all")
tm.assert_frame_equal(result, expected)
Expand Down
9 changes: 5 additions & 4 deletions pandas/tests/series/methods/test_describe.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,14 @@ def test_describe_with_tz(self, tz_naive_fixture):
expected = Series(
[
5,
5,
s.value_counts().index[0],
1,
Timestamp(2018, 1, 3).tz_localize(tz),
start.tz_localize(tz),
s[1],
s[2],
s[3],
end.tz_localize(tz),
],
name=name,
index=["count", "unique", "top", "freq", "first", "last"],
index=["count", "mean", "min", "25%", "50%", "75%", "max"],
)
tm.assert_series_equal(result, expected)