Skip to content

BUG: astype to pyarrow does not copy np array #50984

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
Jan 30, 2023

Conversation

phofl
Copy link
Member

@phofl phofl commented Jan 26, 2023

  • 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.

cc @mroeschke

This should copy. You can't update the pyarrow array, but the parent array can get updated which propagates to the pyarrow array.

@phofl phofl added the Arrow pyarrow functionality label Jan 26, 2023
@jbrockmendel
Copy link
Member

worth adding this to tm.shares_memory?

@phofl
Copy link
Member Author

phofl commented Jan 26, 2023

Raises a NotImplementedError (no arrow support)

Will have a look at this in a follow up

@@ -218,6 +218,9 @@ def _from_sequence(cls, scalars, *, dtype: Dtype | None = None, copy: bool = Fal
if isinstance(scalars, cls):
scalars = scalars._data
elif not isinstance(scalars, (pa.Array, pa.ChunkedArray)):
if copy and is_array_like(scalars):
# pa array should not get updated when numpy array is updated
scalars = scalars.copy()
Copy link
Member

Choose a reason for hiding this comment

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

Is it worth specifying np.array(scalars, copy=True) manually? There might be an off chance the arraylike doesn't have a copy method?

Copy link
Member Author

Choose a reason for hiding this comment

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

This would cast our nullables to object, so not ideal

Copy link
Member

Choose a reason for hiding this comment

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

Ah gotcha. Would calling copy.copy(...) be safer 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.

We need deepcopy, but this should do the trick

Copy link
Member

Choose a reason for hiding this comment

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

There might be an off chance the arraylike doesn't have a copy method?

might make more sense to raise than guess if this occurs?

Copy link
Member Author

Choose a reason for hiding this comment

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

No preference here, cc @mroeschke

@mroeschke mroeschke added this to the 2.0 milestone Jan 30, 2023
@mroeschke mroeschke merged commit 365841f into pandas-dev:main Jan 30, 2023
@mroeschke
Copy link
Member

Thanks @phofl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Arrow pyarrow functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants