enh: Adds support for Time datatype#2113
Conversation
FBruzzesi
left a comment
There was a problem hiding this comment.
Thanks for the contribution @marvinl803 - This is a good start 🙏🏼
I would like to aim to support the following in this PR:
- Add a case test in
tests/expr_and_series/cast_test.py - Support more than just polars, as specified in #1989 comment, pyarrow and duckdb should be possible to support
- Make sure that conversion from and to the backend is supported
Hi @FBruzzesi, I have added the test cases where you suggested and also implemented support for PyArrow and DuckDB. However, I encountered an issue in narwhals/dtypes.py—I wasn’t sure how to provide an example for the docs using DuckDB. Additionally, I added Time to all the other supported types in narwhals_to_native_dtype, except for PyArrow. I’m uncertain whether we should return time32 or time64 in that case. Could you provide some guidance on how best to handle this? I appreciate your help! |
Thanks for your adjustments - I think we are very close to have this ready. Regarding the narwhals failing tests:
I think we should convert narwhals Time to pyarrow time64 since that's what polars Time mentions:
As a side note, we will need to make PRs in shiny and marimo to fix their failing tests |
| if dtype.startswith("time") and dtype.endswith("[pyarrow]"): | ||
| return dtypes.Time() |
There was a problem hiding this comment.
Pattern is time<32|64>[<unit>][pyarrow]
Example:
from datetime import time
import pandas as pd
import pyarrow as pa
data = {"a": [time(12, 0, 0), time(12, 0, 5)]}
pd.DataFrame(data).convert_dtypes(dtype_backend="pyarrow").astype(pd.ArrowDtype(pa.time64("ns"))).dtypes
a time64[ns][pyarrow]
dtype: object| if isinstance_or_issubclass( | ||
| dtype, (dtypes.Struct, dtypes.Array, dtypes.List, dtypes.Time) | ||
| ): | ||
| if implementation is Implementation.PANDAS and backend_version >= (2, 2): |
There was a problem hiding this comment.
Unrelated, but... should we check modin backed by pyarrow?
| dtypes.UInt8, | ||
| dtypes.Enum, | ||
| dtypes.Categorical, | ||
| dtypes.Time, |
There was a problem hiding this comment.
I couldn't find a datatype in pyspark for this, so it will just end up in the unsupported list
|
@marvinl803 I added a few commits to reach coverage and add pandas support (if backed by pyarrow dtype). I think this is ready to merge, but we should have ready the PRs to address marimo and shiny whenever we press the merge button. I will try to find a bit of time tomorrow (no promises 🙈) Plotly failure is unrelated, they simply reworked some deps files |
| def test_cast_time(request: pytest.FixtureRequest, constructor: Constructor) -> None: | ||
| if "pandas" in str(constructor) and PANDAS_VERSION < (2, 2): | ||
| request.applymarker(pytest.mark.xfail) | ||
|
|
||
| if any(backend in str(constructor) for backend in ("dask", "pyspark", "modin")): | ||
| request.applymarker(pytest.mark.xfail) | ||
|
|
||
| data = {"a": [time(12, 0, 0), time(12, 0, 5)]} | ||
| df = nw.from_native(constructor(data)) | ||
| result = df.select(nw.col("a").cast(nw.Time())) | ||
| assert result.collect_schema() == {"a": nw.Time()} |
There was a problem hiding this comment.
Great to see this is working for the Expr.cast case!
Could we also gets some tests to see how nw.Time interacts with Expr.dt methods?
I'm thinking we should aim to support anything that doesn't mention date, duration, time_zone components
There was a problem hiding this comment.
The polars Temporal docs usually specify which subset of TemporalType they work with
There was a problem hiding this comment.
I've only found polars.Expr.dt.to_string mention Time so far
There was a problem hiding this comment.
There's also a huge suite here to draw from for test ideas: https://github.com/pola-rs/polars/blob/52b93ef5909cd0a5790001894aec4b473c361631/py-polars/tests/unit/datatypes/test_temporal.py
There was a problem hiding this comment.
That might get more complex that expected: pandas does not access pyarrow time via .dt:
from datetime import time
import pandas as pd
import pyarrow as pa
data = {"a": [time(12, 0, 0), time(12, 0, 5)]}
(
pd.DataFrame(data)
.convert_dtypes(dtype_backend="pyarrow")
.astype(pd.ArrowDtype(pa.time64("ns")))
["a"].dt
)AttributeError: Can only use .dt accessor with datetimelike values
There was a problem hiding this comment.
@FBruzzesi we don't have to resolve all the issues in this PR - but I think it would still be worth identifying them for follow-ups.
Before reading through (#2113 (comment)) I wasn't aware of how little support there was for Time
Got it! Thanks for your help and the updates, @FBruzzesi. I'm not too familiar with Marimo and Shiny, but happy to assist however I can. Let me know if there's anything I can do! 👍 |
Hey @marvinl803 I opened one PR in each repo, and forgot to mention it. You should be able to see the link somewhere here (I am from mobile and it's tricky to find the links) 😊 |
No problem, @FBruzzesi! I found them—I'll check them out. |
|
@MarcoGorelli I have a PR open in marimo that provide a fix, yet we will need a release - either we can merge this PR and make a patch release, or merge it on Monday before the minor release (I would prefer this second option) 😁 |
Time datatype
FBruzzesi
left a comment
There was a problem hiding this comment.
Thanks a ton for the contribution (and your patience) @marvinl803
Let's ship this to include this feature in today's release

P.S. @dangotbanned I was not able to solve the mypy issues is CI 🥲
@FBruzzesi Let me take a quick look before merging 🤝 |
|
I've opened a PR upstream for another fix |
|
Thanks @dangotbanned , shall we merge? |
## 📝 Summary [Narwhals#2113](narwhals-dev/narwhals#2113) is going to introduce `Time` dtype, this PR prepares for that ## 📋 Checklist - [x] I have read the [contributor guidelines](https://github.com/marimo-team/marimo/blob/main/CONTRIBUTING.md). - [ ] For large changes, or changes that affect the public API: this change was discussed or approved through an issue, on [Discord](https://marimo.io/discord?ref=pr), or the community [discussions](https://github.com/marimo-team/marimo/discussions) (Please provide a link if applicable). - [ ] I have added tests for the changes made. - [x] I have run the code and verified that it works as expected. ## 📜 Reviewers @akshayka OR @mscolnick --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Myles Scolnick <myles@marimo.io>
Both `pa.date32()` and `pa.time64("ns")` are currently supported in `modin`, but weren't included in #2113
What type of PR is this? (check all applicable)
Related issues
Binary&Timedatatype #1989Checklist
If you have comments or can explain your changes, please do so below
I have added support for the Polars Time datatype. I wanted to implement this first to ensure there are no issues before proceeding with the Binary datatype, as mentioned in the issue.
Please let me know if any changes or fixes are needed. Thank you!