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

feat: make pandas and NumPy optional dependencies, don't require PyArrow for plotting with Polars/Modin/cuDF #3452

Merged
merged 24 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
special-case Polars in .dt.to_string for Date
  • Loading branch information
MarcoGorelli committed Jul 15, 2024
commit cd813854d1edf0bc63e85babc6c51a6701a62059
6 changes: 4 additions & 2 deletions altair/utils/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

import jsonschema
import narwhals.stable.v1 as nw
from narwhals.dependencies import is_pandas_dataframe
from narwhals.dependencies import is_pandas_dataframe, get_polars
from narwhals.typing import IntoDataFrame

from altair.utils.schemapi import SchemaBase, Undefined
Expand Down Expand Up @@ -448,12 +448,14 @@ def sanitize_narwhals_dataframe(
# See https://github.com/vega/altair/issues/1027 for why this is necessary.
local_iso_fmt_string = "%Y-%m-%dT%H:%M:%S"
for name, dtype in schema.items():
if dtype == nw.Date:
if dtype == nw.Date and nw.get_native_namespace(data) is get_polars():
# Polars doesn't allow formatting `Date` with time directives.
# The date -> datetime cast is extremely fast compared with `to_string`
columns.append(
nw.col(name).cast(nw.Datetime).dt.to_string(local_iso_fmt_string)
)
elif dtype == nw.Date:
columns.append(nw.col(name).dt.to_string(local_iso_fmt_string))
elif dtype == nw.Datetime:
columns.append(nw.col(name).dt.to_string(f"{local_iso_fmt_string}%.f"))
elif dtype == nw.Duration:
Expand Down
2 changes: 1 addition & 1 deletion tests/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def test_sanitize_pyarrow_table_columns() -> None:
"f": 0.0,
"i": 0,
"b": True,
"d": "2012-01-01T00:00:00.000000",
"d": "2012-01-01T00:00:00",
"c": "a",
"p": "2012-01-01T00:00:00.000000000",
}
Expand Down
6 changes: 3 additions & 3 deletions tests/vegalite/v5/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,7 @@ def test_ibis_with_date_32():
tbl = ibis.memtable(df)
result = alt.Chart(tbl).mark_line().encode(x="a", y="b").to_dict()
assert next(iter(result["datasets"].values())) == [
{"a": 1, "b": "2020-01-01T00:00:00.000000"},
{"a": 2, "b": "2020-01-02T00:00:00.000000"},
{"a": 3, "b": "2020-01-03T00:00:00.000000"},
{"a": 1, "b": "2020-01-01T00:00:00"},
{"a": 2, "b": "2020-01-02T00:00:00"},
{"a": 3, "b": "2020-01-03T00:00:00"},
]