Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(python): let read_csv take Sequence as columns, remove several type: ignore #7028

Merged
merged 5 commits into from
Feb 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion py-polars/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions py-polars/polars/_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import os
from textwrap import dedent
from types import TracebackType
from typing import Iterable
from typing import TYPE_CHECKING, Iterable

if TYPE_CHECKING:
from polars.internals import DataFrame


class Tag:
Expand Down Expand Up @@ -43,7 +46,7 @@ def __exit__(
class HTMLFormatter:
def __init__(
self,
df: DataFrame, # type: ignore[name-defined] # noqa: F821
df: DataFrame,
max_cols: int = 75,
max_rows: int = 40,
from_series: bool = False,
Expand Down Expand Up @@ -75,7 +78,7 @@ def __init__(

def write_header(self) -> None:
"""Write the header of an HTML table."""
shape = self.df.shape
shape: tuple[int, ...] = self.df.shape
stinodego marked this conversation as resolved.
Show resolved Hide resolved
if self.series:
shape = shape[:1]

Expand Down
5 changes: 2 additions & 3 deletions py-polars/polars/internals/series/datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __getitem__(self, item: int) -> date | datetime:
s = pli.wrap_s(self._s)
return s[item]

def min(self) -> date | datetime | timedelta:
def min(self) -> date | datetime | timedelta | None:
"""
Return minimum as python DateTime.

Expand All @@ -52,10 +52,9 @@ def min(self) -> date | datetime | timedelta:
datetime.datetime(2001, 1, 1, 0, 0)

"""
# we can ignore types because we are certain we get a logical type
return pli.wrap_s(self._s).min() # type: ignore[return-value]

def max(self) -> date | datetime | timedelta:
def max(self) -> date | datetime | timedelta | None:
"""
Return maximum as python DateTime.

Expand Down
11 changes: 6 additions & 5 deletions py-polars/polars/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
BinaryIO,
Callable,
Mapping,
Sequence,
TextIO,
cast,
overload,
Expand Down Expand Up @@ -58,7 +59,7 @@ def _check_arg_is_1byte(
def read_csv(
file: str | TextIO | BytesIO | Path | BinaryIO | bytes,
has_header: bool = True,
columns: list[int] | list[str] | None = None,
columns: Sequence[int] | Sequence[str] | None = None,
new_columns: list[str] | None = None,
sep: str = ",",
comment_char: str | None = None,
Expand Down Expand Up @@ -230,7 +231,7 @@ def read_csv(
and not low_memory
and null_values is None
):
include_columns = None
include_columns: Sequence[str] | None = None

if columns:
if not has_header:
Expand Down Expand Up @@ -1235,7 +1236,7 @@ def _read_excel_sheet(
parser: Any,
sheet_id: int | None,
sheet_name: str | None,
read_csv_options: dict[str, Any] | None,
read_csv_options: dict[str, Any],
) -> DataFrame:
csv_buffer = StringIO()

Expand All @@ -1246,7 +1247,7 @@ def _read_excel_sheet(
csv_buffer.seek(0)

# Parse CSV output.
return read_csv(csv_buffer, **read_csv_options) # type: ignore[arg-type]
return read_csv(csv_buffer, **read_csv_options)


def _get_delta_lake_table(
Expand Down Expand Up @@ -1660,7 +1661,7 @@ def scan_ds(ds: pa.dataset.dataset, allow_pyarrow_filter: bool = True) -> LazyFr
def read_csv_batched(
file: str | Path,
has_header: bool = True,
columns: list[int] | list[str] | None = None,
columns: Sequence[int] | Sequence[str] | None = None,
new_columns: list[str] | None = None,
sep: str = ",",
comment_char: str | None = None,
Expand Down
19 changes: 11 additions & 8 deletions py-polars/polars/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,20 +201,22 @@ def range_to_slice(rng: range) -> slice:

def handle_projection_columns(
columns: Sequence[str] | Sequence[int] | str | None,
) -> tuple[list[int] | None, list[str] | None]:
) -> tuple[list[int] | None, Sequence[str] | None]:
"""Disambiguates between columns specified as integers vs. strings."""
projection: list[int] | None = None
if columns:
new_columns: Sequence[str] | None = None
if columns is not None:
if isinstance(columns, str):
columns = [columns]
new_columns = [columns]
elif is_int_sequence(columns):
projection = list(columns)
columns = None
elif not is_str_sequence(columns):
raise ValueError(
"'columns' arg should contain a list of all integers or all strings"
" values."
)
else:
new_columns = columns
if columns and len(set(columns)) != len(columns):
raise ValueError(
f"'columns' arg should only have unique values. Got '{columns}'."
Expand All @@ -223,7 +225,7 @@ def handle_projection_columns(
raise ValueError(
f"'columns' arg should only have unique values. Got '{projection}'."
)
return projection, columns # type: ignore[return-value]
return projection, new_columns


def _to_python_time(value: int) -> time:
Expand Down Expand Up @@ -333,13 +335,14 @@ def _parse_fixed_tz_offset(offset: str) -> tzinfo:

def _localize(dt: datetime, tz: str) -> datetime:
# zone info installation should already be checked
_tzinfo: ZoneInfo | tzinfo
try:
tzinfo = ZoneInfo(tz)
_tzinfo = ZoneInfo(tz)
except zoneinfo.ZoneInfoNotFoundError:
# try fixed offset, which is not supported by ZoneInfo
tzinfo = _parse_fixed_tz_offset(tz) # type: ignore[assignment]
_tzinfo = _parse_fixed_tz_offset(tz)

return dt.astimezone(tzinfo)
return dt.astimezone(_tzinfo)


def _in_notebook() -> bool:
Expand Down