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

refactor(python): Minor refactor of DataFrame.to_numpy structured code #14348

Merged
merged 3 commits into from
Feb 7, 2024
Merged
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
75 changes: 37 additions & 38 deletions py-polars/polars/dataframe/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2069,15 +2069,16 @@ def to_numpy(
use_pyarrow: bool = True,
) -> np.ndarray[Any, Any]:
"""
Convert DataFrame to a 2D NumPy array.

This operation clones data.
Convert this DataFrame to a NumPy ndarray.

Parameters
----------
structured
Optionally return a structured array, with field names and
dtypes that correspond to the DataFrame schema.
Return a `structured array`_ with a data type that corresponds to the
DataFrame schema. If set to `False` (default), a 2D ndarray is
returned instead.

.. _structured array: https://numpy.org/doc/stable/user/basics.rec.html
order
The index order of the returned NumPy array, either C-like or
Fortran-like. In general, using the Fortran-like index order is faster.
Expand Down Expand Up @@ -2130,36 +2131,33 @@ def to_numpy(
dtype=[('foo', 'u1'), ('bar', '<f4'), ('ham', '<U1')])
"""
if structured:
# see: https://numpy.org/doc/stable/user/basics.rec.html
arrays = []
for c, tp in self.schema.items():
s = self[c]
a = s.to_numpy(use_pyarrow=use_pyarrow)
arrays.append(
a.astype(str, copy=False)
if tp == String and not s.null_count()
else a
)

out = np.empty(
len(self), dtype=list(zip(self.columns, (a.dtype for a in arrays)))
)
struct_dtype = []
for s in self.iter_columns():
arr = s.to_numpy(use_pyarrow=use_pyarrow)
if s.dtype == String and s.null_count() == 0:
arr = arr.astype(str, copy=False)
arrays.append(arr)
struct_dtype.append((s.name, arr.dtype))

out = np.empty(self.height, dtype=struct_dtype)
for idx, c in enumerate(self.columns):
out[c] = arrays[idx]
else:
if order == "fortran":
array = self._df.to_numpy_view()
if array is not None:
return array

out = self._df.to_numpy(order)
if out is None:
return np.vstack(
[
self.to_series(i).to_numpy(use_pyarrow=use_pyarrow)
for i in range(self.width)
]
).T
return out

if order == "fortran":
array = self._df.to_numpy_view()
if array is not None:
return array

out = self._df.to_numpy(order)
if out is None:
return np.vstack(
[
self.to_series(i).to_numpy(use_pyarrow=use_pyarrow)
for i in range(self.width)
]
).T

return out

Expand Down Expand Up @@ -9888,17 +9886,17 @@ def iter_rows(

def iter_columns(self) -> Iterator[Series]:
"""
Returns an iterator over the DataFrame's columns.
Returns an iterator over the columns of this DataFrame.

Yields
------
Series

Notes
-----
Consider whether you can use :func:`all` instead.
If you can, it will be more efficient.

Returns
-------
Iterator of Series.

Examples
--------
>>> df = pl.DataFrame(
Expand Down Expand Up @@ -9939,7 +9937,8 @@ def iter_columns(self) -> Iterator[Series]:
│ 10 ┆ 12 │
└─────┴─────┘
"""
return (wrap_s(s) for s in self._df.get_columns())
for s in self._df.get_columns():
yield wrap_s(s)

def iter_slices(self, n_rows: int = 10_000) -> Iterator[DataFrame]:
r"""
Expand Down
Loading