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

API: Harmonize dtype for index levels for Series.sparse.from_coo #50926

Merged
merged 2 commits into from
Jan 23, 2023
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ Other API changes
methods to get a full slice (for example ``df.loc[:]`` or ``df[:]``) (:issue:`49469`)
- Disallow computing ``cumprod`` for :class:`Timedelta` object; previously this returned incorrect values (:issue:`50246`)
- Loading a JSON file with duplicate columns using ``read_json(orient='split')`` renames columns to avoid duplicates, as :func:`read_csv` and the other readers do (:issue:`50370`)
- The levels of the index of the :class:`Series` returned from ``Series.sparse.from_coo`` now always have dtype ``int32``. Previously they had dtype ``int64`` (:issue:`50926`)
- :func:`to_datetime` with ``unit`` of either "Y" or "M" will now raise if a sequence contains a non-round ``float`` value, matching the ``Timestamp`` behavior (:issue:`50301`)
-

Expand Down
5 changes: 1 addition & 4 deletions pandas/core/arrays/sparse/scipy_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,6 @@ def coo_to_sparse_series(
ser = ser.sort_index()
ser = ser.astype(SparseDtype(ser.dtype))
if dense_index:
# is there a better constructor method to use here?
i = range(A.shape[0])
j = range(A.shape[1])
ind = MultiIndex.from_product([i, j])
ind = MultiIndex.from_product([A.row, A.col])
ser = ser.reindex(ind)
return ser
9 changes: 3 additions & 6 deletions pandas/tests/arrays/sparse/test_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,11 @@ def test_series_from_coo(self, dtype, dense_index):
A = scipy.sparse.eye(3, format="coo", dtype=dtype)
result = pd.Series.sparse.from_coo(A, dense_index=dense_index)

# TODO: GH49560: scipy.sparse.eye always has A.row and A.col dtype as int32.
# fix index_dtype to follow scipy.sparse convention (always int32)?
index_dtype = np.int64 if dense_index else np.int32
index = pd.MultiIndex.from_tuples(
[
np.array([0, 0], dtype=index_dtype),
np.array([1, 1], dtype=index_dtype),
np.array([2, 2], dtype=index_dtype),
np.array([0, 0], dtype=np.int32),
np.array([1, 1], dtype=np.int32),
np.array([2, 2], dtype=np.int32),
],
)
expected = pd.Series(SparseArray(np.array([1, 1, 1], dtype=dtype)), index=index)
Expand Down