-
Notifications
You must be signed in to change notification settings - Fork 87
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
enh: add dt.timestamp #1220
enh: add dt.timestamp #1220
Changes from 27 commits
487a525
b1e568c
5ecbed9
7ed770d
d3f76cb
c9a8ec0
839891b
485c1e1
bf909ad
5301aa4
a37e5cd
fee2422
ee760e6
dc86689
70d6462
b1e97c9
124d588
6277094
d158f22
49d235c
674b912
263f093
532b07f
186f926
a5ed5f6
a022148
3a572e6
a01d9ad
32bb502
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ | |
from typing import Sequence | ||
from typing import overload | ||
|
||
from narwhals._pandas_like.utils import calculate_timestamp_date | ||
from narwhals._pandas_like.utils import calculate_timestamp_datetime | ||
from narwhals._pandas_like.utils import int_dtype_mapper | ||
from narwhals._pandas_like.utils import narwhals_to_native_dtype | ||
from narwhals._pandas_like.utils import native_series_from_iterable | ||
|
@@ -944,3 +946,29 @@ def convert_time_zone(self, time_zone: str) -> PandasLikeSeries: | |
else: | ||
result = self._pandas_series._native_series.dt.tz_convert(time_zone) | ||
return self._pandas_series._from_native_series(result) | ||
|
||
def timestamp(self, time_unit: Literal["ns", "us", "ms"] = "us") -> PandasLikeSeries: | ||
s = self._pandas_series._native_series | ||
dtype = self._pandas_series.dtype | ||
is_pyarrow_dtype = "pyarrow" in str(self._pandas_series._native_series.dtype) | ||
mask_na = s.isna() | ||
if dtype == self._pandas_series._dtypes.Date: | ||
s_cast = s.astype("Int32[pyarrow]") | ||
result = calculate_timestamp_date(s_cast, time_unit) | ||
elif dtype == self._pandas_series._dtypes.Datetime: | ||
original_time_unit = dtype.time_unit # type: ignore[attr-defined] | ||
if ( | ||
self._pandas_series._implementation is Implementation.PANDAS | ||
and self._pandas_series._backend_version < (2,) | ||
): # pragma: no cover | ||
s_cast = s.view("Int64[pyarrow]") if is_pyarrow_dtype else s.view("int64") | ||
else: | ||
s_cast = ( | ||
s.astype("Int64[pyarrow]") if is_pyarrow_dtype else s.astype("int64") | ||
) | ||
Comment on lines
+965
to
+969
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. I assume 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. it's the most ridiculous thing - pandas 1.5 emits a warning telling you to use |
||
result = calculate_timestamp_datetime(s_cast, original_time_unit, time_unit) | ||
else: | ||
msg = "Input should be either of Date or Datetime type" | ||
raise TypeError(msg) | ||
result[mask_na] = None | ||
return self._pandas_series._from_native_series(result) |
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.
That's because date can only be pyarrow backed, is that right?
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.
yup! will add a comment