Skip to content

BUG: 2D ndarray of dtype 'object' is always copied upon construction #39272

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 20 commits into from
Jul 15, 2021
Merged
Show file tree
Hide file tree
Changes from 10 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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.3.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Fixed regressions

Bug fixes
~~~~~~~~~
-
- Bug in :class:`DataFrame` constructor always copying 2D object arrays (:issue:`39272`)
-

.. ---------------------------------------------------------------------------
Expand Down
21 changes: 13 additions & 8 deletions pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,14 +351,19 @@ def ndarray_to_mgr(
# transpose and separate blocks

dtlike_vals = [maybe_infer_to_datetimelike(row) for row in values]
dvals_list = [ensure_block_shape(dval, 2) for dval in dtlike_vals]

# TODO: What about re-joining object columns?
block_values = [
new_block(dvals_list[n], placement=n, ndim=2)
for n in range(len(dvals_list))
]

# don't convert (and copy) the objects if no type inference occurs
if any(
not is_dtype_equal(instance.dtype, values.dtype)
for instance in dtlike_vals
):
dvals_list = [ensure_block_shape(dval, 2) for dval in dtlike_vals]
block_values = [
new_block(dvals_list[n], placement=n, ndim=2)
for n in range(len(dvals_list))
]
else:
nb = new_block(values, placement=slice(len(columns)), ndim=2)
block_values = [nb]
else:
datelike_vals = maybe_infer_to_datetimelike(values)
nb = new_block(datelike_vals, placement=slice(len(columns)), ndim=2)
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2080,6 +2080,14 @@ def test_constructor_series_copy(self, float_frame):

assert not (series["A"] == 5).all()

def test_object_array_does_not_copy(self):
a = np.array(["a", "b"], dtype="object")
Copy link
Member

Choose a reason for hiding this comment

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

so if we have any columns that are inferred as datetimelike, we get copies of everything, i.e. dont retain any of the original array?

Copy link
Contributor Author

@irgolic irgolic Jan 27, 2021

Choose a reason for hiding this comment

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

If one of the columns is inferred as datetimelike, each column is created as its own block. From my understanding, upon consolidation, these blocks get copied to a(nother) contiguous location.

Copy link
Contributor

Choose a reason for hiding this comment

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

add the issue number as a comment

b = np.array([["a", "b"], ["c", "d"]], dtype="object")
df = DataFrame(a)
assert np.shares_memory(df.values, a)
df2 = DataFrame(b)
assert np.shares_memory(df2.values, b)

Copy link
Contributor

Choose a reason for hiding this comment

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

assert the frames are equal as well

Copy link
Contributor Author

Choose a reason for hiding this comment

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

did you mean assert the numpy arrays equal? They're not equal, but they do share memory:

>       np.testing.assert_array_equal(df.values, a)
E       AssertionError: 
E       Arrays are not equal
E       
E       (shapes (2, 1), (2,) mismatch)
E        x: array([['a'],
E                  ['b']], dtype=object)
E        y: array(['a', 'b'], dtype=object)

(note also I've split the test into two)

def test_constructor_with_nas(self):
# GH 5016
# na's in indices
Expand Down