Skip to content

BUG: SparseArray.astype(np.int64) #49704

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

Merged
merged 3 commits into from
Nov 17, 2022

Conversation

jbrockmendel
Copy link
Member

@jbrockmendel jbrockmendel commented Nov 14, 2022

  • closes #xxxx (Replace xxxx with the GitHub issue number)
  • Tests added and passed if fixing a bug or adding a new feature
  • All code checks passed.
  • Added type annotations to new arguments/methods/functions.
  • Added an entry in the latest doc/source/whatsnew/vX.X.X.rst file if fixing a bug or adding a new feature.

@mroeschke mroeschke added Dtype Conversions Unexpected or buggy dtype conversions Sparse Sparse Data Type labels Nov 16, 2022
dtype = cast(ExtensionDtype, dtype)
cls = dtype.construct_array_type()
return cls._from_sequence(self, dtype=dtype, copy=copy)
values = np.asarray(self)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this robust if the original type was pd.SparseDtype(pd.Int64Dtype) with an NA and astype(pd.Int32Dtype) was called?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh looks like there an existing bug here too (missing import)

In [1]: arr = pd.array([1, pd.NA], dtype=pd.SparseDtype(pd.Int64Dtype()))

In [2]: arr
Out[2]:
[1, <NA>]
Fill: <NA>
IntIndex
Indices: array([0], dtype=int32)

In [3]: arr.dtype
Out[3]: Sparse[Int64, <NA>]

In [4]: arr.astype(pd.Int16Dtype())
NameError: name 'ExtensionDtype' is not defined

Copy link
Member Author

@jbrockmendel jbrockmendel Nov 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yah, though i think that was introduced by a pylint PR really recently. updatenope it was introduced by me 15 days ago

also IIUC pd.SparseDtype(EADtype) shouldn't work.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you raise a NotImplementedError for the EADtypes here for now then?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

id rather do that as separately to Do It Right along with a couple other bugs mentioned in #49631

@mroeschke mroeschke added this to the 2.0 milestone Nov 17, 2022
@mroeschke mroeschke merged commit cca262d into pandas-dev:main Nov 17, 2022
@mroeschke
Copy link
Member

Thanks @jbrockmendel

@jbrockmendel jbrockmendel deleted the bug-sparse-astype branch November 17, 2022 22:44
MarcoGorelli pushed a commit to MarcoGorelli/pandas that referenced this pull request Nov 18, 2022
* BUG: SparseArray.astype(np.int64)

* whatsnew
mliu08 pushed a commit to mliu08/pandas that referenced this pull request Nov 27, 2022
* BUG: SparseArray.astype(np.int64)

* whatsnew
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Astype Dtype Conversions Unexpected or buggy dtype conversions Sparse Sparse Data Type
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants