Skip to content

Commit

Permalink
Remove DataFrame.select_at_idx (#4338)
Browse files Browse the repository at this point in the history
  • Loading branch information
stinodego committed Aug 9, 2022
1 parent 872b39d commit 63b06e2
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 59 deletions.
1 change: 0 additions & 1 deletion py-polars/docs/source/reference/dataframe.rst
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ Manipulation/ selection
DataFrame.rows
DataFrame.sample
DataFrame.select
DataFrame.select_at_idx
DataFrame.shift
DataFrame.shift_and_fill
DataFrame.shrink_to_fit
Expand Down
36 changes: 1 addition & 35 deletions py-polars/polars/internals/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ class DataFrame:
>>> class MyDataFrame(pl.DataFrame):
... pass
>>>
...
>>> isinstance(MyDataFrame().lazy().collect(), MyDataFrame)
False
Expand Down Expand Up @@ -4027,40 +4027,6 @@ def drop_in_place(self, name: str) -> pli.Series:
"""
return pli.wrap_s(self._df.drop_in_place(name))

def select_at_idx(self, idx: int) -> pli.Series:
"""
Select column at index location.
Parameters
----------
idx
Location of selection.
.. deprecated:: 0.10.20
Examples
--------
>>> df = pl.DataFrame(
... {
... "foo": [1, 2, 3],
... "bar": [6, 7, 8],
... "ham": ["a", "b", "c"],
... }
... )
>>> df.select_at_idx(1)
shape: (3,)
Series: 'bar' [i64]
[
6
7
8
]
"""
if idx < 0:
idx = len(self.columns) + idx
return pli.wrap_s(self._df.select_at_idx(idx))

def cleared(self: DF) -> DF:
"""
Create an empty copy of the current DataFrame, with identical schema but no
Expand Down
33 changes: 12 additions & 21 deletions py-polars/tests/test_df.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def test_selection() -> None:

assert df[[0, 1], "b"].shape == (2, 1)
assert df[[2], ["a", "b"]].shape == (1, 2)
assert df.select_at_idx(0).name == "a"
assert df.to_series(0).name == "a"
assert (df["a"] == df["a"]).sum() == 3
assert (df["c"] == df["a"].cast(str)).sum() == 0
assert df[:, "a":"b"].shape == (3, 2) # type: ignore[misc]
Expand Down Expand Up @@ -204,15 +204,6 @@ def test_assignment() -> None:
assert df["foo"].to_list() == [1, 9, 9]


def test_select_at_idx() -> None:
df = pl.DataFrame({"x": [1, 2, 3], "y": [2, 3, 4], "z": [3, 4, 5]})
for idx in range(len(df.columns)):
assert_series_equal(
df.select_at_idx(idx), # regular positive indexing
df.select_at_idx(idx - len(df.columns)), # equivalent negative index
)


def test_insert_at_idx() -> None:
df = pl.DataFrame({"z": [3, 4, 5]})
df.insert_at_idx(0, pl.Series("x", [1, 2, 3]))
Expand Down Expand Up @@ -821,34 +812,34 @@ def test_lazy_functions() -> None:
]
)
expected = 1.0
assert np.isclose(out.select_at_idx(0), expected)
assert np.isclose(out.to_series(0), expected)
assert np.isclose(pl.var(df["b"]), expected) # type: ignore[arg-type]
expected = 1.0
assert np.isclose(out.select_at_idx(1), expected)
assert np.isclose(out.to_series(1), expected)
assert np.isclose(pl.std(df["b"]), expected) # type: ignore[arg-type]
expected = 3
assert np.isclose(out.select_at_idx(2), expected)
assert np.isclose(out.to_series(2), expected)
assert np.isclose(pl.max(df["b"]), expected)
expected = 1
assert np.isclose(out.select_at_idx(3), expected)
assert np.isclose(out.to_series(3), expected)
assert np.isclose(pl.min(df["b"]), expected)
expected = 6
assert np.isclose(out.select_at_idx(4), expected)
assert np.isclose(out.to_series(4), expected)
assert np.isclose(pl.sum(df["b"]), expected)
expected = 2
assert np.isclose(out.select_at_idx(5), expected)
assert np.isclose(out.to_series(5), expected)
assert np.isclose(pl.mean(df["b"]), expected)
expected = 2
assert np.isclose(out.select_at_idx(6), expected)
assert np.isclose(out.to_series(6), expected)
assert np.isclose(pl.median(df["b"]), expected)
expected = 3
assert np.isclose(out.select_at_idx(7), expected)
assert np.isclose(out.to_series(7), expected)
assert np.isclose(pl.n_unique(df["b"]), expected)
expected = 1
assert np.isclose(out.select_at_idx(8), expected)
assert np.isclose(out.to_series(8), expected)
assert np.isclose(pl.first(df["b"]), expected)
expected = 3
assert np.isclose(out.select_at_idx(9), expected)
assert np.isclose(out.to_series(9), expected)
assert np.isclose(pl.last(df["b"]), expected)


Expand Down Expand Up @@ -883,7 +874,7 @@ def test_describe() -> None:
}
)
assert df.describe().shape != df.shape
assert set(df.describe().select_at_idx(2)) == {1.0, 4.0, 5.0, 6.0}
assert set(df.describe().to_series(2)) == {1.0, 4.0, 5.0, 6.0}


def test_string_cache_eager_lazy() -> None:
Expand Down
2 changes: 1 addition & 1 deletion py-polars/tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ def test_arange_expr() -> None:
df = pl.DataFrame({"a": ["foobar", "barfoo"]})
out = df.select([pl.arange(0, pl.col("a").count() * 10)])
assert out.shape == (20, 1)
assert out.select_at_idx(0)[-1] == 19
assert out.to_series(0)[-1] == 19

# eager arange
out2 = pl.arange(0, 10, 2, eager=True)
Expand Down
2 changes: 1 addition & 1 deletion py-polars/tests_parametric/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_null_count(df: pl.DataFrame) -> None:
else:
assert null_count.shape == (1, ncols)
for idx, count in enumerate(null_count.rows()[0]):
assert count == sum(v is None for v in df.select_at_idx(idx).to_list())
assert count == sum(v is None for v in df.to_series(idx).to_list())


@given(
Expand Down

0 comments on commit 63b06e2

Please sign in to comment.