Skip to content
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

PERF: DataFrame(pytorch_tensor) #45007

Merged
merged 7 commits into from
Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions asv_bench/benchmarks/frame_ctor.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,21 @@ def time_frame_from_arrays_sparse(self):
)


class From3rdParty:
jreback marked this conversation as resolved.
Show resolved Hide resolved
# GH#44616

def setup_cache(self):
try:
import torch
except ImportError:
raise NotImplementedError

row = 700000
col = 64
self.val_tensor = torch.randn(row, col)

def time_from_torch(self):
DataFrame(self.val_tensor)


from .pandas_vb_common import setup # noqa: F401 isort:skip
9 changes: 7 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,11 +705,16 @@ def __init__(
# For data is list-like, or Iterable (will consume into list)
elif is_list_like(data):
if not isinstance(data, (abc.Sequence, ExtensionArray)):
data = list(data)
if hasattr(data, "__array__"):
# GH#44616 big perf improvement for e.g. pytorch tensor
data = np.asarray(data)
else:
data = list(data)
if len(data) > 0:
if is_dataclass(data[0]):
data = dataclasses_to_dicts(data)
if treat_as_nested(data):
if not isinstance(data, np.ndarray) and treat_as_nested(data):
# exclude ndarray as we may have cast it a few lines above
if columns is not None:
# error: Argument 1 to "ensure_index" has incompatible type
# "Collection[Any]"; expected "Union[Union[Union[ExtensionArray,
Expand Down
16 changes: 15 additions & 1 deletion pandas/tests/test_downstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import subprocess
import sys

import numpy as np # noqa:F401 needed in namespace for statsmodels
import numpy as np
import pytest

import pandas.util._test_decorators as td
Expand Down Expand Up @@ -176,6 +176,20 @@ def test_pyarrow(df):
tm.assert_frame_equal(result, df)


def test_torch_frame_construction(using_array_manager):
# GH#44616
torch = import_module("torch")
val_tensor = torch.randn(700, 64)

df = DataFrame(val_tensor)

if not using_array_manager:
assert np.shares_memory(df, val_tensor)

ser = pd.Series(val_tensor[0])
assert np.shares_memory(ser, val_tensor)


def test_yaml_dump(df):
# GH#42748
yaml = import_module("yaml")
Expand Down