-
-
Notifications
You must be signed in to change notification settings - Fork 18k
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
BUG (string dtype): fix inplace mutation with copy=False in ensure_string_array #59756
Changes from all commits
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 |
---|---|---|
|
@@ -7,7 +7,6 @@ | |
|
||
from pandas.compat import HAS_PYARROW | ||
from pandas.compat.pyarrow import pa_version_under12p0 | ||
import pandas.util._test_decorators as td | ||
|
||
from pandas import ( | ||
DataFrame, | ||
|
@@ -111,7 +110,8 @@ def test_astype_string_and_object_update_original(dtype, new_dtype): | |
tm.assert_frame_equal(df2, df_orig) | ||
|
||
|
||
def test_astype_string_copy_on_pickle_roundrip(): | ||
def test_astype_str_copy_on_pickle_roundrip(): | ||
# TODO(infer_string) this test can be removed after 3.0 (once str is the default) | ||
# https://github.com/pandas-dev/pandas/issues/54654 | ||
# ensure_string_array may alter array inplace | ||
base = Series(np.array([(1, 2), None, 1], dtype="object")) | ||
|
@@ -120,14 +120,22 @@ def test_astype_string_copy_on_pickle_roundrip(): | |
tm.assert_series_equal(base, base_copy) | ||
|
||
|
||
@td.skip_if_no("pyarrow") | ||
def test_astype_string_read_only_on_pickle_roundrip(): | ||
def test_astype_string_copy_on_pickle_roundrip(any_string_dtype): | ||
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. i cant find it now, but i thought one of the various string-dtype-like fixtures included 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. The 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. Will add a comment |
||
# https://github.com/pandas-dev/pandas/issues/54654 | ||
# ensure_string_array may alter array inplace | ||
base = Series(np.array([(1, 2), None, 1], dtype="object")) | ||
base_copy = pickle.loads(pickle.dumps(base)) | ||
base_copy.astype(any_string_dtype) | ||
tm.assert_series_equal(base, base_copy) | ||
|
||
|
||
def test_astype_string_read_only_on_pickle_roundrip(any_string_dtype): | ||
# https://github.com/pandas-dev/pandas/issues/54654 | ||
# ensure_string_array may alter read-only array inplace | ||
base = Series(np.array([(1, 2), None, 1], dtype="object")) | ||
base_copy = pickle.loads(pickle.dumps(base)) | ||
base_copy._values.flags.writeable = False | ||
base_copy.astype("string[pyarrow]") | ||
base_copy.astype(any_string_dtype) | ||
tm.assert_series_equal(base, base_copy) | ||
|
||
|
||
|
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.
any particular reason for may_share_memory instead of shares_memory?
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.
Because that is what was recommended by the numpy developer: #54654 (comment)
My understanding is that we have here either always a full copy or either exactly the same memory, so just checking the bounds should be sufficient, and there is less risk to this being a costly check (although I don't really know how this works under the hood, so not sure if there is any risk in practice)