Skip to content

Commit

Permalink
fix: dask expr dt.nanosecond implementation (#678)
Browse files Browse the repository at this point in the history
  • Loading branch information
aidoskanapyanov authored Jul 30, 2024
1 parent 3783fd2 commit 9b1eecc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion narwhals/_dask/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ def microsecond(self) -> DaskExpr:

def nanosecond(self) -> DaskExpr:
return self._expr._from_call(
lambda _input: _input.dt.microsecond * 1000,
lambda _input: _input.dt.microsecond * 1000 + _input.dt.nanosecond,
"nanosecond",
)

Expand Down
17 changes: 13 additions & 4 deletions tests/dask_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,13 +408,22 @@ def test_dt_microsecond() -> None:
def test_dt_nanosecond() -> None:
import dask.dataframe as dd

data = {
"a": [datetime(2020, 1, 1, 1, 1, 1, 1000), datetime(2021, 1, 1, 2, 2, 2, 2000)]
}
val1 = (
pd.Timestamp("2014-07-04 15:00")
+ pd.tseries.offsets.Micro(654)
+ pd.tseries.offsets.Nano(321)
)
val2 = (
pd.Timestamp("2014-07-04 15:00")
+ pd.tseries.offsets.Micro(123)
+ pd.tseries.offsets.Nano(456)
)

data = {"a": [val1, val2]}
dfdd = dd.from_pandas(pd.DataFrame(data))
df = nw.from_native(dfdd)
result = df.with_columns(nanosecond=nw.col("a").dt.nanosecond())
expected = {"a": data["a"], "nanosecond": [1000000, 2000000]}
expected = {"a": data["a"], "nanosecond": [654321, 123456]}
compare_dicts(result, expected)


Expand Down

0 comments on commit 9b1eecc

Please sign in to comment.