Skip to content
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

FIX-#2195: fix describe error for datasets with datetimes #2272

Merged
merged 4 commits into from
Oct 20, 2020
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
16 changes: 15 additions & 1 deletion modin/backends/pandas/query_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1559,6 +1559,20 @@ def describe(self, **kwargs):
.astype(self.dtypes)
.describe(**kwargs)
)
new_index = empty_df.index

# Note: `describe` convert timestamp type to object type
# which results in the loss of two values in index: `first` and `last`
# for empty DataFrame.
datetime_is_numeric = kwargs.get("datetime_is_numeric") or False
if not any(map(is_numeric_dtype, empty_df.dtypes)) and not datetime_is_numeric:
for col_name in empty_df.dtypes.index:
# if previosly type of `col_name` was datetime or timedelta
if is_datetime_or_timedelta_dtype(self.dtypes[col_name]):
new_index = pandas.Index(
empty_df.index.to_list() + ["first"] + ["last"]
)
anmyachev marked this conversation as resolved.
Show resolved Hide resolved
break

def describe_builder(df, internal_indices=[]):
return df.iloc[:, internal_indices].describe(**kwargs)
Expand All @@ -1568,7 +1582,7 @@ def describe_builder(df, internal_indices=[]):
0,
describe_builder,
empty_df.columns,
new_index=empty_df.index,
new_index=new_index,
new_columns=empty_df.columns,
)
)
Expand Down
20 changes: 20 additions & 0 deletions modin/pandas/test/dataframe/test_reduction.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,26 @@ def test_describe(data, percentiles):
)


@pytest.mark.parametrize("has_numeric_column", [False, True])
@pytest.mark.parametrize("datetime_is_numeric", [True, False, None])
def test_2195(datetime_is_numeric, has_numeric_column):
data = {
"categorical": pd.Categorical(["d"] * 10 ** 2),
"date": [np.datetime64("2000-01-01")] * 10 ** 2,
}

if has_numeric_column:
data.update({"numeric": [5] * 10 ** 2})

modin_df, pandas_df = pd.DataFrame(data), pandas.DataFrame(data)

eval_general(
modin_df,
pandas_df,
lambda df: df.describe(datetime_is_numeric=datetime_is_numeric),
)


@pytest.mark.parametrize(
"exclude,include",
[
Expand Down