-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: fix concat of Sparse with non-sparse dtypes #34338
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 6 commits
76618a8
29e63a5
d6373f8
7db5a9b
b4b48f2
20e7e4c
50fb8bc
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
""" | ||
Utility functions related to concat. | ||
""" | ||
from typing import cast | ||
|
||
import numpy as np | ||
|
||
|
@@ -20,7 +21,7 @@ | |
) | ||
from pandas.core.dtypes.generic import ABCCategoricalIndex, ABCRangeIndex, ABCSeries | ||
|
||
from pandas.core.arrays import ExtensionArray | ||
from pandas.core.arrays import ExtensionArray, SparseArray | ||
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. 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. Yeah, I already pushed a fix. |
||
from pandas.core.construction import array | ||
|
||
|
||
|
@@ -81,6 +82,13 @@ def _cast_to_common_type(arr: ArrayLike, dtype: DtypeObj) -> ArrayLike: | |
except ValueError: | ||
return arr.astype(object, copy=False) | ||
|
||
if is_sparse(arr) and not is_sparse(dtype): | ||
jorisvandenbossche marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# problem case: SparseArray.astype(dtype) doesn't follow the specified | ||
# dtype exactly, but converts this to Sparse[dtype] -> first manually | ||
# convert to dense array | ||
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. Opened #34457 for this |
||
arr = cast(SparseArray, arr) | ||
return arr.to_dense().astype(dtype, copy=False) | ||
|
||
if ( | ||
isinstance(arr, np.ndarray) | ||
and arr.dtype.kind in ["m", "M"] | ||
|
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.
Opened #34456 for this (and added link in the comment)