-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -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 | ||
|
@@ -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 | ||
asint = data.dropna().values.view("i8") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you don't need the .values.view, just use data.min() and so on all work and return NaT correctly, none of these gymnastics are needed There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
There was a problem hiding this comment.
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.