Skip to content

Commit

Permalink
python DataFrame.with_column -> pl.Series
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Sep 16, 2021
1 parent 8ba8840 commit 554b1c6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions py-polars/docs/source/reference/dataframe.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Manipulation/ selection
DataFrame.select_at_idx
DataFrame.clone
DataFrame.get_columns
DataFrame.get_column
DataFrame.fill_null
DataFrame.fill_nan
DataFrame.explode
Expand Down
6 changes: 6 additions & 0 deletions py-polars/polars/eager/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -2209,6 +2209,12 @@ def get_columns(self) -> tp.List["pl.Series"]:
"""
return list(map(lambda s: pl.eager.series.wrap_s(s), self._df.get_columns()))

def get_column(self, name: str) -> "pl.Series":
"""
Get a single column as Series by name.
"""
return self[name]

def fill_null(self, strategy: Union[str, "pl.Expr"]) -> "DataFrame":
"""
Fill None/missing values by a filling strategy or an Expression evaluation.
Expand Down
9 changes: 6 additions & 3 deletions py-polars/tests/test_df.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,13 @@ def test_init_records():
def test_selection():
df = pl.DataFrame({"a": [1, 2, 3], "b": [1.0, 2.0, 3.0], "c": ["a", "b", "c"]})

# get_column by name
assert df.get_column("a").to_list() == [1, 2, 3]

# column selection by string(s) in first dimension
assert df["a"] == [1, 2, 3]
assert df["b"] == [1.0, 2.0, 3.0]
assert df["c"] == ["a", "b", "c"]
assert df["a"].to_list() == [1, 2, 3]
assert df["b"].to_list() == [1.0, 2.0, 3.0]
assert df["c"].to_list() == ["a", "b", "c"]

# row selection by integers(s) in first dimension
assert df[0].frame_equal(pl.DataFrame({"a": [1], "b": [1.0], "c": ["a"]}))
Expand Down

0 comments on commit 554b1c6

Please sign in to comment.