Skip to content

ENH: Implement CoW for convert_dtypes #51265

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 4 commits into from
Feb 10, 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 @@ -225,6 +225,7 @@ Copy-on-Write improvements
- :meth:`DataFrame.tz_convert` / :meth:`Series.tz_localize`
- :meth:`DataFrame.infer_objects` / :meth:`Series.infer_objects`
- :meth:`DataFrame.astype` / :meth:`Series.astype`
- :meth:`DataFrame.convert_dtypes` / :meth:`Series.convert_dtypes`
- :func:`concat`

These methods return views when Copy-on-Write is enabled, which provides a significant
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6642,7 +6642,7 @@ def convert_dtypes(
# https://github.com/python/mypy/issues/8354
return cast(NDFrameT, result)
else:
return self.copy()
return self.copy(deep=None)

# ----------------------------------------------------------------------
# Filling NA's
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -5468,7 +5468,7 @@ def _convert_dtypes(
if infer_objects:
input_series = input_series.infer_objects()
if is_object_dtype(input_series):
input_series = input_series.copy()
input_series = input_series.copy(deep=None)

if convert_string or convert_integer or convert_boolean or convert_floating:
dtype_backend = get_option("mode.dtype_backend")
Expand All @@ -5483,7 +5483,7 @@ def _convert_dtypes(
)
result = input_series.astype(inferred_dtype)
else:
result = input_series.copy()
result = input_series.copy(deep=None)
return result

# error: Cannot determine type of 'isna'
Expand Down
39 changes: 39 additions & 0 deletions pandas/tests/copy_view/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,42 @@ def test_astype_arrow_timestamp(using_copy_on_write):
if using_copy_on_write:
assert not result._mgr._has_no_reference(0)
assert np.shares_memory(get_array(df, "a").asi8, get_array(result, "a")._data)


def test_convert_dtypes_infer_objects(using_copy_on_write):
ser = Series(["a", "b", "c"])
ser_orig = ser.copy()
result = ser.convert_dtypes(
convert_integer=False,
convert_boolean=False,
convert_floating=False,
convert_string=False,
)

if using_copy_on_write:
assert np.shares_memory(get_array(ser), get_array(result))
else:
assert not np.shares_memory(get_array(ser), get_array(result))

result.iloc[0] = "x"
tm.assert_series_equal(ser, ser_orig)


def test_convert_dtypes(using_copy_on_write):
df = DataFrame({"a": ["a", "b"], "b": [1, 2], "c": [1.5, 2.5], "d": [True, False]})
df_orig = df.copy()
df2 = df.convert_dtypes()

if using_copy_on_write:
assert np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
assert np.shares_memory(get_array(df2, "d"), get_array(df, "d"))
assert np.shares_memory(get_array(df2, "b"), get_array(df, "b"))
assert np.shares_memory(get_array(df2, "c"), get_array(df, "c"))
else:
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
assert not np.shares_memory(get_array(df2, "b"), get_array(df, "b"))
assert not np.shares_memory(get_array(df2, "c"), get_array(df, "c"))
assert not np.shares_memory(get_array(df2, "d"), get_array(df, "d"))

df2.iloc[0, 0] = "x"
tm.assert_frame_equal(df, df_orig)
5 changes: 3 additions & 2 deletions pandas/tests/copy_view/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
from pandas.core.arrays import BaseMaskedArray


def get_array(obj, col):
def get_array(obj, col=None):
"""
Helper method to get array for a DataFrame column or a Series.

Equivalent of df[col].values, but without going through normal getitem,
which triggers tracking references / CoW (and we might be testing that
this is done by some other operation).
"""
if isinstance(obj, Series) and obj.name == col:
if isinstance(obj, Series) and (obj is None or obj.name == col):
return obj._values
assert col is not None
icol = obj.columns.get_loc(col)
assert isinstance(icol, int)
arr = obj._get_column_array(icol)
Expand Down