Skip to content

Commit

Permalink
Comments
Browse files Browse the repository at this point in the history
  • Loading branch information
stinodego committed May 26, 2024
1 parent 4281d47 commit 05bc79d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
17 changes: 10 additions & 7 deletions py-polars/polars/_utils/getitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def get_df_item_by_key(
),
) -> DataFrame | Series | Any:
"""Get part of the DataFrame as a new DataFrame, Series, or scalar."""
# Two inputs
# Two inputs, e.g. df[1, 2:5]
if isinstance(key, tuple) and len(key) == 2:
row_key, col_key = key
selection = _select_columns(df, col_key)
Expand All @@ -140,17 +140,20 @@ def get_df_item_by_key(
else:
return _select_rows(selection, row_key) # type: ignore[arg-type]

# TODO: Deprecate `_select_rows` path here - code below can be replaced by just
# the `_select_columns` column call.
# https://github.com/pola-rs/polars/issues/4924
if isinstance(key, str):
# Single input, e.g. df[1]
elif isinstance(key, str):
# This case is required because empty strings are otherwise treated
# as an empty Sequence in `_select_rows`
return df.get_column(key)
if isinstance(key, Sequence) and len(key) == 0:
elif isinstance(key, Sequence) and len(key) == 0:
# df[[]]
# TODO: This removes all columns, but it should remove all rows.
# https://github.com/pola-rs/polars/issues/4924
return df.__class__()
try:
return _select_rows(df, key) # type: ignore[arg-type]
except TypeError:
return _select_columns(df, key)
return _select_columns(df, key) # type: ignore[arg-type]


# `str` overlaps with `Sequence[str]`
Expand Down
1 change: 1 addition & 0 deletions py-polars/tests/unit/dataframe/test_df.py
Original file line number Diff line number Diff line change
Expand Up @@ -1997,6 +1997,7 @@ def test_add_string() -> None:
)
assert_frame_equal(("hello " + df), expected)


def test_df_broadcast() -> None:
df = pl.DataFrame({"a": [1, 2, 3]}, schema_overrides={"a": pl.UInt8})
out = df.with_columns(pl.Series("s", [[1, 2]]))
Expand Down

0 comments on commit 05bc79d

Please sign in to comment.