-
Notifications
You must be signed in to change notification settings - Fork 120
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: Datetime(time_unit, time_zone)
and Duration(time_unit)
types
#960
Merged
Merged
Changes from 3 commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
121f6f8
WIP
FBruzzesi 4896df2
order matters?
FBruzzesi cd2ed40
datetime test and polars fix
FBruzzesi eb1468e
rm NoneType
FBruzzesi e71f9c3
pandas pre 1.5
FBruzzesi 32385d0
no cover backend version branch
FBruzzesi 3abeaf8
add pytz to dev requirements for testing
FBruzzesi c5b7635
merge main
FBruzzesi 4415e3c
xfail pyarrow table on windows
FBruzzesi 5309d4f
Duration(time_unit)
FBruzzesi 85fdd80
Merge branch 'main' into feat/time-zone-aware-datetime
FBruzzesi 91bfb7a
Merge branch 'main' into feat/time-zone-aware-datetime
FBruzzesi 20e36a1
add Datetime and Duration methods, as in polars
FBruzzesi ec1cb5e
downstream?
FBruzzesi 2147ec6
revert
FBruzzesi 0f69ec1
hash class only
FBruzzesi 22836a0
else case no cover
FBruzzesi a1f56bc
Merge branch 'main' into feat/time-zone-aware-datetime
FBruzzesi a84480d
merge main
FBruzzesi 80a574d
trigger ci
FBruzzesi 916eac5
try making stable dtypes
MarcoGorelli e94b517
Merge remote-tracking branch 'upstream/main' into feat/time-zone-awarβ¦
MarcoGorelli 180b86e
broken, but getting there?
MarcoGorelli da884e8
Merge remote-tracking branch 'upstream/main' into feat/time-zone-awarβ¦
MarcoGorelli 114be74
fixup
MarcoGorelli 587d917
reduce diff
MarcoGorelli dd050a8
stableify duration too
MarcoGorelli b4de1f7
test duration too
MarcoGorelli 458f2a2
try removing pytz
MarcoGorelli 34c27ef
try fix ci
MarcoGorelli 0de71a6
try fix ci
MarcoGorelli d105911
try fix ci
MarcoGorelli a773d85
try fix ci
MarcoGorelli 0149431
try fix ci
MarcoGorelli 2249af0
allow s time unit
MarcoGorelli 942a77b
test second resolution
MarcoGorelli ad38667
override duration time unit for pandas pre 2.0
MarcoGorelli 38898a8
:label:
MarcoGorelli 43da4c3
pre-2.0 pandas
MarcoGorelli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from __future__ import annotations | ||
|
||
from datetime import timezone | ||
from types import NoneType | ||
from typing import Literal | ||
|
||
import pytest | ||
|
||
import narwhals.stable.v1 as nw | ||
|
||
|
||
@pytest.mark.parametrize("time_unit", ["us", "ns", "ms"]) | ||
@pytest.mark.parametrize("time_zone", ["Europe/Rome", timezone.utc, None]) | ||
def test_datetime_valid( | ||
time_unit: Literal["us", "ns", "ms"], time_zone: str | timezone | None | ||
) -> None: | ||
dtype = nw.Datetime(time_unit=time_unit, time_zone=time_zone) | ||
|
||
assert dtype.time_unit == time_unit | ||
assert isinstance(dtype.time_zone, (str, NoneType)) | ||
|
||
|
||
@pytest.mark.parametrize("time_unit", ["abc", "s"]) | ||
def test_datetime_invalid(time_unit: str) -> None: | ||
with pytest.raises(ValueError, match="invalid `time_unit`"): | ||
nw.Datetime(time_unit=time_unit) # type: ignore[arg-type] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
from __future__ import annotations | ||
|
||
from datetime import datetime | ||
from datetime import timedelta | ||
from datetime import timezone | ||
from typing import Any | ||
|
||
import pandas as pd | ||
|
@@ -6,6 +11,7 @@ | |
|
||
import narwhals.stable.v1 as nw | ||
from narwhals.utils import parse_version | ||
from tests.utils import compare_dicts | ||
|
||
data = { | ||
"a": [1], | ||
|
@@ -179,3 +185,27 @@ class Banana: | |
|
||
with pytest.raises(AssertionError, match=r"Unknown dtype"): | ||
df.select(nw.col("a").cast(Banana)) | ||
|
||
|
||
def test_cast_datetime_tz_aware(constructor: Any, request: Any) -> None: | ||
if "dask" in str(constructor): | ||
request.applymarker(pytest.mark.xfail) | ||
|
||
data = { | ||
"date": [ | ||
datetime(2024, 1, 1, tzinfo=timezone.utc) + timedelta(days=i) | ||
for i in range(3) | ||
] | ||
} | ||
expected = { | ||
"date": ["2024-01-01 01:00:00", "2024-01-02 01:00:00", "2024-01-03 01:00:00"] | ||
} | ||
|
||
df = nw.from_native(constructor(data)) | ||
result = df.select( | ||
nw.col("date") | ||
.cast(nw.Datetime("ms", time_zone="Europe/Rome")) | ||
.cast(nw.String()) | ||
.str.slice(offset=0, length=19) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 19: number of characters of |
||
) | ||
compare_dicts(result, expected) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,13 +75,13 @@ def test_cast_date_datetime_pandas() -> None: | |
df = df.select(nw.col("a").cast(nw.Datetime)) | ||
result = nw.to_native(df) | ||
expected = pd.DataFrame({"a": [datetime(2020, 1, 1), datetime(2020, 1, 2)]}).astype( | ||
{"a": "timestamp[ns][pyarrow]"} | ||
{"a": "timestamp[us][pyarrow]"} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changes the default to the polars one |
||
) | ||
pd.testing.assert_frame_equal(result, expected) | ||
|
||
# pandas: pyarrow datetime to date | ||
# # pandas: pyarrow datetime to date | ||
dfpd = pd.DataFrame({"a": [datetime(2020, 1, 1), datetime(2020, 1, 2)]}).astype( | ||
{"a": "timestamp[ns][pyarrow]"} | ||
{"a": "timestamp[us][pyarrow]"} | ||
) | ||
df = nw.from_native(dfpd) | ||
df = df.select(nw.col("a").cast(nw.Date)) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please try to break these π