Skip to content

Commit

Permalink
python df.to_series
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Nov 14, 2021
1 parent 9eb8d42 commit cd3eef0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
2 changes: 1 addition & 1 deletion py-polars/docs/source/reference/dataframe.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Manipulation/ selection
DataFrame.drop_nulls
DataFrame.drop
DataFrame.drop_in_place
DataFrame.select_at_idx
DataFrame.to_series
DataFrame.clone
DataFrame.get_columns
DataFrame.get_column
Expand Down
46 changes: 37 additions & 9 deletions py-polars/polars/eager/frame.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""
""""
Module containing logic related to eager DataFrames
"""
import os
Expand Down Expand Up @@ -862,9 +862,7 @@ def to_numpy(self) -> np.ndarray:
numpy.ndarray
"""
return np.vstack(
[self.select_at_idx(i).to_numpy() for i in range(self.width)]
).T
return np.vstack([self.to_series(i).to_numpy() for i in range(self.width)]).T

def __getstate__(self): # type: ignore
return self.get_columns()
Expand Down Expand Up @@ -1007,14 +1005,14 @@ def __getitem__(self, item: Any) -> Any:

# df[:, 1]
if isinstance(col_selection, int):
series = self.select_at_idx(col_selection)
series = self.to_series(col_selection)
return series[row_selection]

if isinstance(col_selection, list):
# df[:, [1, 2]]
# select by column indexes
if isinstance(col_selection[0], int):
series = [self.select_at_idx(i) for i in col_selection]
series = [self.to_series(i) for i in col_selection]
df = DataFrame(series)
return df[row_selection]
df = self.__getitem__(col_selection)
Expand Down Expand Up @@ -1162,6 +1160,34 @@ def _repr_html_(self) -> str:
max_rows = int(os.environ.get("POLARS_FMT_MAX_ROWS", default=25))
return "\n".join(NotebookFormatter(self, max_cols, max_rows).render())

def to_series(self, index: int = 0) -> "pl.Series":
"""
Select column as Series at index location.
Parameters
----------
index
Location of selection.
Examples
--------
>>> df = pl.DataFrame({
>>> "foo": [1, 2, 3],
>>> "bar": [6, 7, 8],
>>> "ham": ['a', 'b', 'c']
>>> })
>>> df.to_series(1))
shape: (3,)
Series: 'bar' [i64]
[
6
7
8
]
"""
return pl.eager.series.wrap_s(self._df.select_at_idx(index))

def rename(self, mapping: Dict[str, str]) -> "DataFrame":
"""
Rename column names.
Expand Down Expand Up @@ -2315,6 +2341,8 @@ def select_at_idx(self, idx: int) -> "pl.Series":
idx
Location of selection.
.. deprecated:: 0.10.20
Examples
--------
>>> df = pl.DataFrame({
Expand Down Expand Up @@ -3085,12 +3113,12 @@ def fold(
"""
if self.width == 1:
return self.select_at_idx(0)
return self.to_series(0)
df = self
acc = operation(df.select_at_idx(0), df.select_at_idx(1))
acc = operation(df.to_series(0), df.to_series(1))

for i in range(2, df.width):
acc = operation(acc, df.select_at_idx(i))
acc = operation(acc, df.to_series(i))
return acc

def row(self, index: int) -> Tuple[Any]:
Expand Down

0 comments on commit cd3eef0

Please sign in to comment.