Skip to content

Commit

Permalink
Wrap long strings (#4144)
Browse files Browse the repository at this point in the history
  • Loading branch information
stinodego committed Jul 25, 2022
1 parent b326afd commit 69d1339
Show file tree
Hide file tree
Showing 14 changed files with 115 additions and 49 deletions.
6 changes: 4 additions & 2 deletions py-polars/polars/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ def sequence_to_pyseries(
if dtype_ in py_temporal_types:
if not _PYARROW_AVAILABLE: # pragma: no cover
raise ImportError(
"'pyarrow' is required for converting a Sequence of date or datetime values to a PySeries."
"'pyarrow' is required for converting a Sequence of date or"
" datetime values to a PySeries."
)
# let arrow infer dtype if not timedelta
# arrow uses microsecond durations by default, not supported yet.
Expand Down Expand Up @@ -670,7 +671,8 @@ def pandas_to_pydf(
"""
if not _PYARROW_AVAILABLE: # pragma: no cover
raise ImportError(
"'pyarrow' is required when constructing a PyDataFrame from a pandas DataFrame."
"'pyarrow' is required when constructing a PyDataFrame from a pandas"
" DataFrame."
)
length = data.shape[0]
arrow_dict = {
Expand Down
3 changes: 2 additions & 1 deletion py-polars/polars/internals/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -7036,7 +7036,8 @@ def expr_to_lit_or_expr(
return pli.lit(pli.Series("", [expr]))
else:
raise ValueError(
f"did not expect value {expr} of type {type(expr)}, maybe disambiguate with pl.lit or pl.col"
f"did not expect value {expr} of type {type(expr)}, maybe disambiguate with"
" pl.lit or pl.col"
)


Expand Down
48 changes: 32 additions & 16 deletions py-polars/polars/internals/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@ def __init__(
elif _PANDAS_AVAILABLE and isinstance(data, pd.DataFrame):
if not _PYARROW_AVAILABLE: # pragma: no cover
raise ImportError(
"'pyarrow' is required for converting a pandas DataFrame to a polars DataFrame."
"'pyarrow' is required for converting a pandas DataFrame to a"
" polars DataFrame."
)
self._df = pandas_to_pydf(data, columns=columns)

Expand Down Expand Up @@ -580,7 +581,8 @@ def _read_csv(
dtypes_dict = {name: dt for (name, dt) in dtype_list}
if dtype_slice is not None:
raise ValueError(
"cannot use glob patterns and unnamed dtypes as `dtypes` argument; Use dtypes: Mapping[str, Type[DataType]"
"cannot use glob patterns and unnamed dtypes as `dtypes` argument;"
" Use dtypes: Mapping[str, Type[DataType]"
)
from polars import scan_csv

Expand Down Expand Up @@ -609,7 +611,8 @@ def _read_csv(
return self._from_pydf(scan.select(columns).collect()._df)
else:
raise ValueError(
"cannot use glob patterns and integer based projection as `columns` argument; Use columns: List[str]"
"cannot use glob patterns and integer based projection as `columns`"
" argument; Use columns: List[str]"
)

projection, columns = handle_projection_columns(columns)
Expand Down Expand Up @@ -683,7 +686,8 @@ def _read_parquet(
return cls._from_pydf(scan.select(columns).collect()._df)
else:
raise ValueError(
"cannot use glob patterns and integer based projection as `columns` argument; Use columns: List[str]"
"cannot use glob patterns and integer based projection as `columns`"
" argument; Use columns: List[str]"
)

projection, columns = handle_projection_columns(columns)
Expand Down Expand Up @@ -775,7 +779,8 @@ def _read_ipc(
return scan.select(columns).collect()
else:
raise ValueError(
"cannot use glob patterns and integer based projection as `columns` argument; Use columns: List[str]"
"cannot use glob patterns and integer based projection as `columns`"
" argument; Use columns: List[str]"
)

projection, columns = handle_projection_columns(columns)
Expand Down Expand Up @@ -819,7 +824,8 @@ def to_arrow(self) -> pa.Table:
"""
if not _PYARROW_AVAILABLE: # pragma: no cover
raise ImportError(
"'pyarrow' is required for converting a polars DataFrame to an Arrow Table."
"'pyarrow' is required for converting a polars DataFrame to an Arrow"
" Table."
)
record_batches = self._df.to_arrow()
return pa.Table.from_batches(record_batches)
Expand Down Expand Up @@ -1437,7 +1443,8 @@ def write_parquet(
if use_pyarrow:
if not _PYARROW_AVAILABLE: # pragma: no cover
raise ImportError(
"'pyarrow' is required when using 'write_parquet(..., use_pyarrow=True)'."
"'pyarrow' is required when using"
" 'write_parquet(..., use_pyarrow=True)'."
)

tbl = self.to_arrow()
Expand Down Expand Up @@ -1765,7 +1772,8 @@ def __getitem__(
return self._from_pydf(self._df.select(item))
if item.dtype == bool:
warnings.warn(
"index notation '[]' is deprecated for boolean masks. Consider using 'filter'.",
"index notation '[]' is deprecated for boolean masks. Consider"
" using 'filter'.",
DeprecationWarning,
)
return self._from_pydf(self._df.filter(pli.Series("", item).inner()))
Expand Down Expand Up @@ -1798,7 +1806,8 @@ def __setitem__(
self, key: str | list | tuple[Any, str | int], value: Any
) -> None: # pragma: no cover
warnings.warn(
"setting a DataFrame by indexing is deprecated; Consider using DataFrame.with_column",
"setting a DataFrame by indexing is deprecated; Consider using"
" DataFrame.with_column",
DeprecationWarning,
)
# df["foo"] = series
Expand All @@ -1817,7 +1826,8 @@ def __setitem__(
raise ValueError("can only set multiple columns with 2D matrix")
if value.shape[1] != len(key):
raise ValueError(
"matrix columns should be equal to list use to determine column names"
"matrix columns should be equal to list use to determine column"
" names"
)
for (i, name) in enumerate(key):
self[name] = value[:, i]
Expand Down Expand Up @@ -3660,7 +3670,8 @@ def join(
"""
if how == "asof": # pragma: no cover
warnings.warn(
"using asof join via DataFrame.join is deprecated, please use DataFrame.join_asof",
"using asof join via DataFrame.join is deprecated, please use"
" DataFrame.join_asof",
DeprecationWarning,
)
if how == "cross":
Expand Down Expand Up @@ -3842,7 +3853,8 @@ def with_column(self: DF, column: pli.Series | pli.Expr) -> DF:
"""
if isinstance(column, list):
raise ValueError(
"`with_column` expects a single expression, not a list. Consider using `with_columns`"
"`with_column` expects a single expression, not a list. Consider using"
" `with_columns`"
)
if isinstance(column, pli.Expr):
return self.with_columns([column])
Expand Down Expand Up @@ -6102,7 +6114,8 @@ def _select(self, columns: str | list[str]) -> GBSelection[DF]: # pragma: no co
One or multiple columns.
"""
warnings.warn(
"accessing GroupBy by index is deprecated, consider using the `.agg` method",
"accessing GroupBy by index is deprecated, consider using the `.agg`"
" method",
DeprecationWarning,
)
if isinstance(columns, str):
Expand Down Expand Up @@ -6218,7 +6231,8 @@ def groups(self) -> DF: # pragma: no cover
"""
warnings.warn(
"accessing GroupBy by index is deprecated, consider using the `.agg` method",
"accessing GroupBy by index is deprecated, consider using the `.agg`"
" method",
DeprecationWarning,
)
return self._dataframe_class._from_pydf(
Expand Down Expand Up @@ -6377,11 +6391,13 @@ def _wrangle(x: Any) -> list:
)
else:
raise ValueError(
f"argument: {column_to_agg} not understood, have you passed a list of expressions?"
f"argument: {column_to_agg} not understood, have you passed a list"
" of expressions?"
)
else:
raise ValueError(
f"argument: {column_to_agg} not understood, have you passed a list of expressions?"
f"argument: {column_to_agg} not understood, have you passed a list of"
" expressions?"
)

return self._dataframe_class._from_pydf(
Expand Down
20 changes: 14 additions & 6 deletions py-polars/polars/internals/lazy_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,8 @@ def _scan_python_function(
def __getitem__(self: LDF, item: int | range | slice) -> LazyFrame:
if not isinstance(item, slice):
raise TypeError(
"'LazyFrame' object is not subscriptable (aside from slicing). Use 'select()' or 'filter()' instead."
"'LazyFrame' object is not subscriptable (aside from slicing). Use"
" 'select()' or 'filter()' instead."
)
return LazyPolarsSlice(self).apply(item)

Expand Down Expand Up @@ -411,7 +412,10 @@ def _repr_html_(self) -> str:
svg = subprocess.check_output(
["dot", "-Nshape=box", "-Tsvg"], input=f"{dot}".encode()
)
return f"<h4>NAIVE QUERY PLAN</h4><p>run <b>LazyFrame.show_graph()</b> to see the optimized version</p>{svg.decode()}"
return (
"<h4>NAIVE QUERY PLAN</h4><p>run <b>LazyFrame.show_graph()</b> to see"
f" the optimized version</p>{svg.decode()}"
)
except Exception:
insert = self.describe_plan().replace("\n", "<p></p>")

Expand Down Expand Up @@ -491,14 +495,16 @@ def show_graph(
return display(SVG(svg))
except Exception as exc:
raise ImportError(
"Graphviz dot binary should be on your PATH and matplotlib should be installed to show graph."
"Graphviz dot binary should be on your PATH and matplotlib should"
" be installed to show graph."
) from exc
try:
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
except ImportError:
raise ImportError(
"Graphviz dot binary should be on your PATH and matplotlib should be installed to show graph."
"Graphviz dot binary should be on your PATH and matplotlib should be"
" installed to show graph."
) from None
dot = self._ldf.to_dot(optimized)
if raw_output:
Expand Down Expand Up @@ -1404,7 +1410,8 @@ def join(

if how == "asof":
warnings.warn(
"using asof join via LazyFrame.join is deprecated, please use LazyFrame.join_asof",
"using asof join via LazyFrame.join is deprecated, please use"
" LazyFrame.join_asof",
DeprecationWarning,
)
if how == "cross":
Expand Down Expand Up @@ -1567,7 +1574,8 @@ def with_columns(
"""
if named_exprs and not Config.with_columns_kwargs:
raise RuntimeError(
"**kwargs support is experimental; requires opt-in via `pl.Config.set_with_columns_kwargs(True)`"
"**kwargs support is experimental; requires opt-in via"
" `pl.Config.set_with_columns_kwargs(True)`"
)
elif exprs is None and not named_exprs:
raise ValueError("Expected at least one of 'exprs' or **named_exprs")
Expand Down
3 changes: 2 additions & 1 deletion py-polars/polars/internals/lazy_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1753,7 +1753,8 @@ def arg_where(
if eager:
if not isinstance(condition, pli.Series):
raise ValueError(
f"expected 'Series' in 'arg_where' if 'eager=True', got {type(condition)}"
"expected 'Series' in 'arg_where' if 'eager=True', got"
f" {type(condition)}"
)
return (
condition.to_frame().select(arg_where(pli.col(condition.name))).to_series()
Expand Down
15 changes: 10 additions & 5 deletions py-polars/polars/internals/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,8 @@ def _arithmetic(self, other: Any, op_s: str, op_ffi: str) -> Series:
f = get_ffi_func(op_ffi, self.dtype, self._s)
if f is None:
raise ValueError(
f"cannot do arithmetic with series of dtype: {self.dtype} and argument of type: {type(other)}"
f"cannot do arithmetic with series of dtype: {self.dtype} and argument"
f" of type: {type(other)}"
)
return wrap_s(f(other))

Expand Down Expand Up @@ -495,7 +496,8 @@ def __setitem__(
self.set_at_idx(key, value) # type: ignore[arg-type]
return None
raise ValueError(
f"cannot set Series of dtype: {self.dtype} with list/tuple as value; use a scalar value"
f"cannot set Series of dtype: {self.dtype} with list/tuple as value;"
" use a scalar value"
)
if isinstance(key, Series):
if key.dtype == Boolean:
Expand Down Expand Up @@ -2260,7 +2262,8 @@ def __array_ufunc__(
return wrap_s(series)
else:
raise NotImplementedError(
f"Only `__call__` is implemented for numpy ufuncs on a Series, got `{method}`."
"Only `__call__` is implemented for numpy ufuncs on a Series, got"
f" `{method}`."
)

def to_numpy(
Expand Down Expand Up @@ -2344,7 +2347,8 @@ def to_pandas(self) -> pd.Series:
"""
if not _PYARROW_AVAILABLE: # pragma: no cover
raise ImportError(
"'pyarrow' is required for converting a 'polars' Series to a 'pandas' Series."
"'pyarrow' is required for converting a 'polars' Series to a 'pandas'"
" Series."
)
return self.to_arrow().to_pandas()

Expand Down Expand Up @@ -2419,7 +2423,8 @@ def set_at_idx(
f = get_ffi_func("set_at_idx_<>", self.dtype, self._s)
if f is None:
raise ValueError(
f"could not find the FFI function needed to set at idx for series {self._s}"
"could not find the FFI function needed to set at idx for series"
f" {self._s}"
)
if isinstance(idx, Series):
# make sure the dtype matches
Expand Down
2 changes: 1 addition & 1 deletion py-polars/polars/internals/slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,5 +205,5 @@ def apply(self, s: slice) -> "pli.LazyFrame":

raise ValueError(
f"The given slice {s} is not supported by lazy computation; consider a "
f"more efficient approach, or construct explicitly with other methods"
"more efficient approach, or construct explicitly with other methods"
)
18 changes: 12 additions & 6 deletions py-polars/polars/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ def _check_arg_is_1byte(
if can_be_empty:
if arg_byte_length > 1:
raise ValueError(
f'{arg_name}="{arg}" should be a single byte character or empty, but is {arg_byte_length} bytes long.'
f'{arg_name}="{arg}" should be a single byte character or empty,'
f" but is {arg_byte_length} bytes long."
)
elif arg_byte_length != 1:
raise ValueError(
f'{arg_name}="{arg}" should be a single byte character, but is {arg_byte_length} bytes long.'
f'{arg_name}="{arg}" should be a single byte character, but is'
f" {arg_byte_length} bytes long."
)


Expand Down Expand Up @@ -305,7 +307,8 @@ def read_csv(
if columns:
if len(columns) < len(new_columns):
raise ValueError(
"More new column names are specified than there are selected columns."
"More new column names are specified than there are selected"
" columns."
)

# Get column names of requested columns.
Expand All @@ -316,7 +319,8 @@ def read_csv(
if projection:
if columns and len(columns) < len(new_columns):
raise ValueError(
"More new column names are specified than there are selected columns."
"More new column names are specified than there are selected"
" columns."
)
# Convert column indices from projection to 'column_1', 'column_2', ... column names.
current_columns = [
Expand Down Expand Up @@ -770,7 +774,8 @@ def read_ipc(
if use_pyarrow:
if not _PYARROW_AVAILABLE:
raise ImportError(
"'pyarrow' is required when using 'read_ipc(..., use_pyarrow=True)'."
"'pyarrow' is required when using"
" 'read_ipc(..., use_pyarrow=True)'."
)

tbl = pa.feather.read_table(data, memory_map=memory_map, columns=columns)
Expand Down Expand Up @@ -851,7 +856,8 @@ def read_parquet(
if use_pyarrow:
if not _PYARROW_AVAILABLE:
raise ImportError(
"'pyarrow' is required when using 'read_parquet(..., use_pyarrow=True)'."
"'pyarrow' is required when using"
" 'read_parquet(..., use_pyarrow=True)'."
)

return from_arrow( # type: ignore[return-value]
Expand Down
6 changes: 4 additions & 2 deletions py-polars/polars/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,8 @@ def __post_init__(self) -> None:
self.null_probability < 0 or self.null_probability > 1
):
raise InvalidArgument(
f"null_probability should be between 0.0 and 1.0, or None; found {self.null_probability}"
"null_probability should be between 0.0 and 1.0, or None; found"
f" {self.null_probability}"
)
if self.dtype is None and not self.strategy:
self.dtype = random.choice(strategy_dtypes)
Expand Down Expand Up @@ -590,7 +591,8 @@ def series(
]
if null_probability and (null_probability < 0 or null_probability > 1):
raise InvalidArgument(
f"null_probability should be between 0.0 and 1.0; found {null_probability}"
"null_probability should be between 0.0 and 1.0; found"
f" {null_probability}"
)
null_probability = float(null_probability or 0.0)

Expand Down
3 changes: 2 additions & 1 deletion py-polars/polars/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ def handle_projection_columns(
columns = None
elif not is_str_sequence(columns):
raise ValueError(
"columns arg should contain a list of all integers or all strings values."
"columns arg should contain a list of all integers or all strings"
" values."
)
return projection, columns # type: ignore[return-value]

Expand Down

0 comments on commit 69d1339

Please sign in to comment.