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

[REVIEW] Enable implicit casting when concatenating mixed types #8276

Merged
merged 3 commits into from
May 23, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
20 changes: 12 additions & 8 deletions python/cudf/cudf/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
is_numerical_dtype,
is_scalar,
min_scalar_type,
_find_common_type_decimal,
)

T = TypeVar("T", bound="Frame")
Expand Down Expand Up @@ -4048,14 +4049,17 @@ def _find_common_dtypes_and_categories(non_null_columns, dtypes):
elif all(
isinstance(col, cudf.core.column.DecimalColumn) for col in cols
):
# Find the largest scale and the largest difference between
# precision and scale of the columns to be concatenated
s = max([col.dtype.scale for col in cols])
lhs = max([col.dtype.precision - col.dtype.scale for col in cols])
# Combine to get the necessary precision and clip at the maximum
# precision
p = min(cudf.Decimal64Dtype.MAX_PRECISION, s + lhs)
dtypes[idx] = cudf.Decimal64Dtype(p, s)
dtypes[idx] = _find_common_type_decimal(
[col.dtype for col in cols]
)
elif all(
isinstance(col, cudf.core.column.DecimalColumn)
or is_numerical_dtype(col.dtype)
for col in cols
):
dtypes[idx] = _find_common_type_decimal(
[col.dtype for col in cols if is_decimal_dtype(col.dtype)]
)
# Otherwise raise an error if columns have different dtypes
elif not all(is_dtype_equal(c.dtype, dtypes[idx]) for c in cols):
raise ValueError("All columns must be the same type")
Expand Down
26 changes: 24 additions & 2 deletions python/cudf/cudf/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@
from cudf.utils.dtypes import (
_decimal_normalize_types,
can_convert_to_column,
is_numerical_dtype,
is_decimal_dtype,
is_list_dtype,
is_list_like,
is_mixed_with_object_dtype,
is_scalar,
min_scalar_type,
numeric_normalize_types,
_find_common_type_decimal,
)
from cudf.utils.utils import (
get_appropriate_dispatched_func,
Expand Down Expand Up @@ -2402,10 +2404,30 @@ def _concat(cls, objs, axis=0, index=True):
)

if dtype_mismatch:
if isinstance(objs[0]._column, cudf.core.column.DecimalColumn):
if all(
[
isinstance(obj._column, cudf.core.column.DecimalColumn)
for obj in objs
]
):
objs = _decimal_normalize_types(*objs)
else:
elif all([is_numerical_dtype(obj.dtype) for obj in objs]):
objs = numeric_normalize_types(*objs)
elif all(
[
isinstance(obj._column, cudf.core.column.DecimalColumn)
or is_numerical_dtype(obj.dtype)
for obj in objs
]
):
decimal_type = _find_common_type_decimal(
[
obj.dtype
for obj in objs
if is_decimal_dtype(obj.dtype)
]
)
objs = [obj.astype(decimal_type) for obj in objs]
ChrisJar marked this conversation as resolved.
Show resolved Hide resolved

col = _concat_columns([o._column for o in objs])

Expand Down
194 changes: 194 additions & 0 deletions python/cudf/cudf/tests/test_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import numpy as np
import pandas as pd
import pytest
from decimal import Decimal

import cudf as gd
from cudf.tests.utils import assert_eq, assert_exceptions_equal
Expand Down Expand Up @@ -1262,3 +1263,196 @@ def test_concat_decimal_series(ltype, rtype):
expected = pd.concat([ps1, ps2])

assert_eq(expected, got)


@pytest.mark.parametrize(
"df1, df2, df3, expected",
[
(
gd.DataFrame(
{"val": [Decimal("42.5"), Decimal("8.7")]},
dtype=Decimal64Dtype(5, 2),
),
gd.DataFrame(
{"val": [Decimal("9.23"), Decimal("-67.49")]},
dtype=Decimal64Dtype(6, 4),
),
gd.DataFrame({"val": [8, -5]}, dtype="int32"),
gd.DataFrame(
{
"val": [
Decimal("42.5"),
Decimal("8.7"),
Decimal("9.23"),
Decimal("-67.49"),
Decimal("8"),
Decimal("-5"),
]
},
dtype=Decimal64Dtype(7, 4),
index=[0, 1, 0, 1, 0, 1],
),
),
(
gd.DataFrame(
{"val": [Decimal("95.2"), Decimal("23.4")]},
dtype=Decimal64Dtype(5, 2),
),
gd.DataFrame({"val": [54, 509]}, dtype="uint16"),
gd.DataFrame({"val": [24, -48]}, dtype="int32"),
gd.DataFrame(
{
"val": [
Decimal("95.2"),
Decimal("23.4"),
Decimal("54"),
Decimal("509"),
Decimal("24"),
Decimal("-48"),
]
},
dtype=Decimal64Dtype(5, 2),
index=[0, 1, 0, 1, 0, 1],
),
),
(
gd.DataFrame(
{"val": [Decimal("36.56"), Decimal("-59.24")]},
dtype=Decimal64Dtype(9, 4),
),
gd.DataFrame({"val": [403.21, 45.13]}, dtype="float32"),
gd.DataFrame({"val": [52.262, -49.25]}, dtype="float64"),
gd.DataFrame(
{
"val": [
Decimal("36.56"),
Decimal("-59.24"),
Decimal("403.21"),
Decimal("45.13"),
Decimal("52.262"),
Decimal("-49.25"),
]
},
dtype=Decimal64Dtype(9, 4),
index=[0, 1, 0, 1, 0, 1],
),
),
(
gd.DataFrame(
{"val": [Decimal("9563.24"), Decimal("236.633")]},
dtype=Decimal64Dtype(9, 4),
),
gd.DataFrame({"val": [5393, -95832]}, dtype="int64"),
gd.DataFrame({"val": [-29.234, -31.945]}, dtype="float64"),
gd.DataFrame(
{
"val": [
Decimal("9563.24"),
Decimal("236.633"),
Decimal("5393"),
Decimal("-95832"),
Decimal("-29.234"),
Decimal("-31.945"),
]
},
dtype=Decimal64Dtype(9, 4),
index=[0, 1, 0, 1, 0, 1],
),
),
],
)
def test_concat_decimal_numeric_dataframe(df1, df2, df3, expected):
df = gd.concat([df1, df2, df3])
assert_eq(df, expected)
assert_eq(df.val.dtype, expected.val.dtype)


@pytest.mark.parametrize(
"s1, s2, s3, expected",
[
(
gd.Series(
[Decimal("32.8"), Decimal("-87.7")], dtype=Decimal64Dtype(6, 2)
),
gd.Series(
[Decimal("101.243"), Decimal("-92.449")],
dtype=Decimal64Dtype(9, 6),
),
gd.Series([94, -22], dtype="int32"),
gd.Series(
[
Decimal("32.8"),
Decimal("-87.7"),
Decimal("101.243"),
Decimal("-92.449"),
Decimal("94"),
Decimal("-22"),
],
dtype=Decimal64Dtype(10, 6),
index=[0, 1, 0, 1, 0, 1],
),
),
(
gd.Series(
[Decimal("7.2"), Decimal("122.1")], dtype=Decimal64Dtype(5, 2)
),
gd.Series([33, 984], dtype="uint32"),
gd.Series([593, -702], dtype="int32"),
gd.Series(
[
Decimal("7.2"),
Decimal("122.1"),
Decimal("33"),
Decimal("984"),
Decimal("593"),
Decimal("-702"),
],
dtype=Decimal64Dtype(5, 2),
index=[0, 1, 0, 1, 0, 1],
),
),
(
gd.Series(
[Decimal("982.94"), Decimal("-493.626")],
dtype=Decimal64Dtype(9, 4),
),
gd.Series([847.98, 254.442], dtype="float32"),
gd.Series([5299.262, -2049.25], dtype="float64"),
gd.Series(
[
Decimal("982.94"),
Decimal("-493.626"),
Decimal("847.98"),
Decimal("254.442"),
Decimal("5299.262"),
Decimal("-2049.25"),
],
dtype=Decimal64Dtype(9, 4),
index=[0, 1, 0, 1, 0, 1],
),
),
(
gd.Series(
[Decimal("492.204"), Decimal("-72824.455")],
dtype=Decimal64Dtype(9, 4),
),
gd.Series([8438, -27462], dtype="int64"),
gd.Series([-40.292, 49202.953], dtype="float64"),
gd.Series(
[
Decimal("492.204"),
Decimal("-72824.455"),
Decimal("8438"),
Decimal("-27462"),
Decimal("-40.292"),
Decimal("49202.953"),
],
dtype=Decimal64Dtype(9, 4),
index=[0, 1, 0, 1, 0, 1],
),
),
],
)
def test_concat_decimal_numeric_series(s1, s2, s3, expected):
s = gd.concat([s1, s2, s3])
assert_eq(s, expected)
17 changes: 12 additions & 5 deletions python/cudf/cudf/utils/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,14 +291,21 @@ def is_decimal_dtype(obj):


def _decimal_normalize_types(*args):
s = max([a.dtype.scale for a in args])
lhs = max([a.dtype.precision - a.dtype.scale for a in args])
p = min(cudf.Decimal64Dtype.MAX_PRECISION, s + lhs)
dtype = cudf.Decimal64Dtype(p, s)

dtype = _find_common_type_decimal([a.dtype for a in args])
return [a.astype(dtype) for a in args]


def _find_common_type_decimal(dtypes):
ChrisJar marked this conversation as resolved.
Show resolved Hide resolved
# Find the largest scale and the largest difference between
# precision and scale of the columns to be concatenated
s = max([dtype.scale for dtype in dtypes])
lhs = max([dtype.precision - dtype.scale for dtype in dtypes])
# Combine to get the necessary precision and clip at the maximum
# precision
p = min(cudf.Decimal64Dtype.MAX_PRECISION, s + lhs)
return cudf.Decimal64Dtype(p, s)


def cudf_dtype_from_pydata_dtype(dtype):
""" Given a numpy or pandas dtype, converts it into the equivalent cuDF
Python dtype.
Expand Down