1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
|
from __future__ import annotations
from typing import TYPE_CHECKING, Generic
from narwhals.typing import SeriesT
if TYPE_CHECKING:
from narwhals.typing import TimeUnit
class SeriesDateTimeNamespace(Generic[SeriesT]):
def __init__(self, series: SeriesT) -> None:
self._narwhals_series = series
def date(self) -> SeriesT:
"""Get the date in a datetime series.
Returns:
A new Series with the date portion of the datetime values.
Raises:
NotImplementedError: If pandas default backend is being used.
Examples:
>>> from datetime import datetime
>>> import pandas as pd
>>> import narwhals as nw
>>> s_native = pd.Series(
... [datetime(2012, 1, 7, 10, 20), datetime(2023, 3, 10, 11, 32)]
... ).convert_dtypes(dtype_backend="pyarrow")
>>> s = nw.from_native(s_native, series_only=True)
>>> s.dt.date().to_native()
0 2012-01-07
1 2023-03-10
dtype: date32[day][pyarrow]
"""
return self._narwhals_series._with_compliant(
self._narwhals_series._compliant_series.dt.date()
)
def year(self) -> SeriesT:
"""Get the year in a datetime series.
Returns:
A new Series containing the year component of each datetime value.
Examples:
>>> from datetime import datetime
>>> import polars as pl
>>> import narwhals as nw
>>> s_native = pl.Series([datetime(2012, 1, 7), datetime(2023, 3, 10)])
>>> s = nw.from_native(s_native, series_only=True)
>>> s.dt.year().to_native() # doctest: +NORMALIZE_WHITESPACE
shape: (2,)
Series: '' [i32]
[
2012
2023
]
"""
return self._narwhals_series._with_compliant(
self._narwhals_series._compliant_series.dt.year()
)
def month(self) -> SeriesT:
"""Gets the month in a datetime series.
Returns:
A new Series containing the month component of each datetime value.
Examples:
>>> from datetime import datetime
>>> import polars as pl
>>> import narwhals as nw
>>> s_native = pl.Series([datetime(2012, 1, 7), datetime(2023, 3, 10)])
>>> s = nw.from_native(s_native, series_only=True)
>>> s.dt.month().to_native() # doctest: +NORMALIZE_WHITESPACE
shape: (2,)
Series: '' [i8]
[
1
3
]
"""
return self._narwhals_series._with_compliant(
self._narwhals_series._compliant_series.dt.month()
)
def day(self) -> SeriesT:
"""Extracts the day in a datetime series.
Returns:
A new Series containing the day component of each datetime value.
Examples:
>>> from datetime import datetime
>>> import pyarrow as pa
>>> import narwhals as nw
>>> s_native = pa.chunked_array(
... [[datetime(2022, 1, 1), datetime(2022, 1, 5)]]
... )
>>> s = nw.from_native(s_native, series_only=True)
>>> s.dt.day().to_native() # doctest: +ELLIPSIS
<pyarrow.lib.ChunkedArray object at ...>
[
[
1,
5
]
]
"""
return self._narwhals_series._with_compliant(
self._narwhals_series._compliant_series.dt.day()
)
def hour(self) -> SeriesT:
"""Extracts the hour in a datetime series.
Returns:
A new Series containing the hour component of each datetime value.
Examples:
>>> from datetime import datetime
>>> import pyarrow as pa
>>> import narwhals as nw
>>> s_native = pa.chunked_array(
... [[datetime(2022, 1, 1, 5, 3), datetime(2022, 1, 5, 9, 12)]]
... )
>>> s = nw.from_native(s_native, series_only=True)
>>> s.dt.hour().to_native() # doctest: +ELLIPSIS
<pyarrow.lib.ChunkedArray object at ...>
[
[
5,
9
]
]
"""
return self._narwhals_series._with_compliant(
self._narwhals_series._compliant_series.dt.hour()
)
def minute(self) -> SeriesT:
"""Extracts the minute in a datetime series.
Returns:
A new Series containing the minute component of each datetime value.
Examples:
>>> from datetime import datetime
>>> import pandas as pd
>>> import narwhals as nw
>>> s_native = pd.Series(
... [datetime(2022, 1, 1, 5, 3), datetime(2022, 1, 5, 9, 12)]
... )
>>> s = nw.from_native(s_native, series_only=True)
>>> s.dt.minute().to_native()
0 3
1 12
dtype: int32
"""
return self._narwhals_series._with_compliant(
self._narwhals_series._compliant_series.dt.minute()
)
def second(self) -> SeriesT:
"""Extracts the seconds in a datetime series.
Returns:
A new Series containing the second component of each datetime value.
Examples:
>>> from datetime import datetime
>>> import pandas as pd
>>> import narwhals as nw
>>> s_native = pd.Series(
... [datetime(2022, 1, 1, 5, 3, 10), datetime(2022, 1, 5, 9, 12, 4)]
... )
>>> s = nw.from_native(s_native, series_only=True)
>>> s.dt.second().to_native()
0 10
1 4
dtype: int32
"""
return self._narwhals_series._with_compliant(
self._narwhals_series._compliant_series.dt.second()
)
def millisecond(self) -> SeriesT:
"""Extracts the milliseconds in a datetime series.
Returns:
A new Series containing the millisecond component of each datetime value.
Examples:
>>> from datetime import datetime
>>> import pandas as pd
>>> import narwhals as nw
>>> s_native = pd.Series(
... [
... datetime(2022, 1, 1, 5, 3, 7, 400000),
... datetime(2022, 1, 1, 5, 3, 7, 0),
... ]
... )
>>> s = nw.from_native(s_native, series_only=True)
>>> s.dt.millisecond().alias("datetime").to_native()
0 400
1 0
Name: datetime, dtype: int32
"""
return self._narwhals_series._with_compliant(
self._narwhals_series._compliant_series.dt.millisecond()
)
def microsecond(self) -> SeriesT:
"""Extracts the microseconds in a datetime series.
Returns:
A new Series containing the microsecond component of each datetime value.
Examples:
>>> from datetime import datetime
>>> import pandas as pd
>>> import narwhals as nw
>>> s_native = pd.Series(
... [
... datetime(2022, 1, 1, 5, 3, 7, 400000),
... datetime(2022, 1, 1, 5, 3, 7, 0),
... ]
... )
>>> s = nw.from_native(s_native, series_only=True)
>>> s.dt.microsecond().alias("datetime").to_native()
0 400000
1 0
Name: datetime, dtype: int32
"""
return self._narwhals_series._with_compliant(
self._narwhals_series._compliant_series.dt.microsecond()
)
def nanosecond(self) -> SeriesT:
"""Extract the nanoseconds in a date series.
Returns:
A new Series containing the nanosecond component of each datetime value.
Examples:
>>> from datetime import datetime
>>> import pandas as pd
>>> import narwhals as nw
>>> s_native = pd.Series(
... [
... datetime(2022, 1, 1, 5, 3, 7, 400000),
... datetime(2022, 1, 1, 5, 3, 7, 0),
... ]
... )
>>> s = nw.from_native(s_native, series_only=True)
>>> s.dt.nanosecond().alias("datetime").to_native()
0 400000000
1 0
Name: datetime, dtype: int32
"""
return self._narwhals_series._with_compliant(
self._narwhals_series._compliant_series.dt.nanosecond()
)
def ordinal_day(self) -> SeriesT:
"""Get ordinal day.
Returns:
A new Series containing the ordinal day (day of year) for each datetime value.
Examples:
>>> from datetime import datetime
>>> import pyarrow as pa
>>> import narwhals as nw
>>> s_native = pa.chunked_array(
... [[datetime(2020, 1, 1), datetime(2020, 8, 3)]]
... )
>>> s = nw.from_native(s_native, series_only=True)
>>> s.dt.ordinal_day().to_native() # doctest: +ELLIPSIS
<pyarrow.lib.ChunkedArray object at ...>
[
[
1,
216
]
]
"""
return self._narwhals_series._with_compliant(
self._narwhals_series._compliant_series.dt.ordinal_day()
)
def weekday(self) -> SeriesT:
"""Extract the week day in a datetime series.
Returns:
A new Series containing the week day for each datetime value.
Returns the ISO weekday number where monday = 1 and sunday = 7
Examples:
>>> from datetime import datetime
>>> import pyarrow as pa
>>> import narwhals as nw
>>> s_native = pa.chunked_array(
... [[datetime(2020, 1, 1), datetime(2020, 8, 3)]]
... )
>>> s = nw.from_native(s_native, series_only=True)
>>> s.dt.weekday().to_native() # doctest: +ELLIPSIS
<pyarrow.lib.ChunkedArray object at ...>
[
[
3,
1
]
]
"""
return self._narwhals_series._with_compliant(
self._narwhals_series._compliant_series.dt.weekday()
)
def total_minutes(self) -> SeriesT:
"""Get total minutes.
Notes:
The function outputs the total minutes in the int dtype by default,
however, pandas may change the dtype to float when there are missing values,
consider using `fill_null()` in this case.
Returns:
A new Series containing the total number of minutes for each timedelta value.
Examples:
>>> from datetime import timedelta
>>> import polars as pl
>>> import narwhals as nw
>>> s_native = pl.Series(
... [timedelta(minutes=10), timedelta(minutes=20, seconds=40)]
... )
>>> s = nw.from_native(s_native, series_only=True)
>>> s.dt.total_minutes().to_native() # doctest: +NORMALIZE_WHITESPACE
shape: (2,)
Series: '' [i64]
[
10
20
]
"""
return self._narwhals_series._with_compliant(
self._narwhals_series._compliant_series.dt.total_minutes()
)
def total_seconds(self) -> SeriesT:
"""Get total seconds.
Notes:
The function outputs the total seconds in the int dtype by default,
however, pandas may change the dtype to float when there are missing values,
consider using `fill_null()` in this case.
Returns:
A new Series containing the total number of seconds for each timedelta value.
Examples:
>>> from datetime import timedelta
>>> import polars as pl
>>> import narwhals as nw
>>> s_native = pl.Series(
... [timedelta(minutes=10), timedelta(minutes=20, seconds=40)]
... )
>>> s = nw.from_native(s_native, series_only=True)
>>> s.dt.total_seconds().to_native() # doctest: +NORMALIZE_WHITESPACE
shape: (2,)
Series: '' [i64]
[
600
1240
]
"""
return self._narwhals_series._with_compliant(
self._narwhals_series._compliant_series.dt.total_seconds()
)
def total_milliseconds(self) -> SeriesT:
"""Get total milliseconds.
Notes:
The function outputs the total milliseconds in the int dtype by default,
however, pandas may change the dtype to float when there are missing values,
consider using `fill_null()` in this case.
Returns:
A new Series containing the total number of milliseconds for each timedelta value.
Examples:
>>> from datetime import timedelta
>>> import polars as pl
>>> import narwhals as nw
>>> s_native = pl.Series(
... [
... timedelta(milliseconds=10),
... timedelta(milliseconds=20, microseconds=40),
... ]
... )
>>> s = nw.from_native(s_native, series_only=True)
>>> s.dt.total_milliseconds().to_native() # doctest: +NORMALIZE_WHITESPACE
shape: (2,)
Series: '' [i64]
[
10
20
]
"""
return self._narwhals_series._with_compliant(
self._narwhals_series._compliant_series.dt.total_milliseconds()
)
def total_microseconds(self) -> SeriesT:
"""Get total microseconds.
Returns:
A new Series containing the total number of microseconds for each timedelta value.
Notes:
The function outputs the total microseconds in the int dtype by default,
however, pandas may change the dtype to float when there are missing values,
consider using `fill_null()` in this case.
Examples:
>>> from datetime import timedelta
>>> import polars as pl
>>> import narwhals as nw
>>> s_native = pl.Series(
... [
... timedelta(microseconds=10),
... timedelta(milliseconds=1, microseconds=200),
... ]
... )
>>> s = nw.from_native(s_native, series_only=True)
>>> s.dt.total_microseconds().to_native() # doctest: +NORMALIZE_WHITESPACE
shape: (2,)
Series: '' [i64]
[
10
1200
]
"""
return self._narwhals_series._with_compliant(
self._narwhals_series._compliant_series.dt.total_microseconds()
)
def total_nanoseconds(self) -> SeriesT:
"""Get total nanoseconds.
Notes:
The function outputs the total nanoseconds in the int dtype by default,
however, pandas may change the dtype to float when there are missing values,
consider using `fill_null()` in this case.
Returns:
A new Series containing the total number of nanoseconds for each timedelta value.
Examples:
>>> from datetime import datetime
>>> import polars as pl
>>> import narwhals as nw
>>> s_native = pl.Series(
... ["2024-01-01 00:00:00.000000001", "2024-01-01 00:00:00.000000002"]
... ).str.to_datetime(time_unit="ns")
>>> s = nw.from_native(s_native, series_only=True)
>>> s.diff().dt.total_nanoseconds().to_native() # doctest: +NORMALIZE_WHITESPACE
shape: (2,)
Series: '' [i64]
[
null
1
]
"""
return self._narwhals_series._with_compliant(
self._narwhals_series._compliant_series.dt.total_nanoseconds()
)
def to_string(self, format: str) -> SeriesT:
"""Convert a Date/Time/Datetime series into a String series with the given format.
Arguments:
format: Format string for converting the datetime to string.
Returns:
A new Series with the datetime values formatted as strings according to the specified format.
Notes:
Unfortunately, different libraries interpret format directives a bit
differently.
- Chrono, the library used by Polars, uses `"%.f"` for fractional seconds,
whereas pandas and Python stdlib use `".%f"`.
- PyArrow interprets `"%S"` as "seconds, including fractional seconds"
whereas most other tools interpret it as "just seconds, as 2 digits".
---
Therefore, we make the following adjustments.
- for pandas-like libraries, we replace `"%S.%f"` with `"%S%.f"`.
- for PyArrow, we replace `"%S.%f"` with `"%S"`.
---
Workarounds like these don't make us happy, and we try to avoid them as
much as possible, but here we feel like it's the best compromise.
If you just want to format a date/datetime Series as a local datetime
string, and have it work as consistently as possible across libraries,
we suggest using:
- `"%Y-%m-%dT%H:%M:%S%.f"` for datetimes
- `"%Y-%m-%d"` for dates
---
Though note that, even then, different tools may return a different number
of trailing zeros. Nonetheless, this is probably consistent enough for
most applications.
If you have an application where this is not enough, please open an issue
and let us know.
Examples:
>>> from datetime import datetime
>>> import pyarrow as pa
>>> import narwhals as nw
>>> s_native = pa.chunked_array(
... [[datetime(2020, 3, 1), datetime(2020, 4, 1)]]
... )
>>> s = nw.from_native(s_native, series_only=True)
>>> s.dt.to_string("%Y/%m/%d").to_native() # doctest: +ELLIPSIS
<pyarrow.lib.ChunkedArray object at ...>
[
[
"2020/03/01",
"2020/04/01"
]
]
"""
return self._narwhals_series._with_compliant(
self._narwhals_series._compliant_series.dt.to_string(format)
)
def replace_time_zone(self, time_zone: str | None) -> SeriesT:
"""Replace time zone.
Arguments:
time_zone: Target time zone.
Returns:
A new Series with the specified time zone.
Examples:
>>> from datetime import datetime, timezone
>>> import polars as pl
>>> import narwhals as nw
>>> s_native = pl.Series(
... [
... datetime(2024, 1, 1, tzinfo=timezone.utc),
... datetime(2024, 1, 2, tzinfo=timezone.utc),
... ]
... )
>>> s = nw.from_native(s_native, series_only=True)
>>> s.dt.replace_time_zone(
... "Asia/Kathmandu"
... ).to_native() # doctest: +NORMALIZE_WHITESPACE
shape: (2,)
Series: '' [datetime[μs, Asia/Kathmandu]]
[
2024-01-01 00:00:00 +0545
2024-01-02 00:00:00 +0545
]
"""
return self._narwhals_series._with_compliant(
self._narwhals_series._compliant_series.dt.replace_time_zone(time_zone)
)
def convert_time_zone(self, time_zone: str) -> SeriesT:
"""Convert time zone.
If converting from a time-zone-naive column, then conversion happens
as if converting from UTC.
Arguments:
time_zone: Target time zone.
Returns:
A new Series with the specified time zone.
Examples:
>>> from datetime import datetime, timezone
>>> import pandas as pd
>>> import narwhals as nw
>>> s_native = pd.Series(
... [
... datetime(2024, 1, 1, tzinfo=timezone.utc),
... datetime(2024, 1, 2, tzinfo=timezone.utc),
... ]
... )
>>> s = nw.from_native(s_native, series_only=True)
>>> s.dt.convert_time_zone("Asia/Kathmandu").to_native()
0 2024-01-01 05:45:00+05:45
1 2024-01-02 05:45:00+05:45
dtype: datetime64[ns, Asia/Kathmandu]
"""
if time_zone is None:
msg = "Target `time_zone` cannot be `None` in `convert_time_zone`. Please use `replace_time_zone(None)` if you want to remove the time zone."
raise TypeError(msg)
return self._narwhals_series._with_compliant(
self._narwhals_series._compliant_series.dt.convert_time_zone(time_zone)
)
def timestamp(self, time_unit: TimeUnit) -> SeriesT:
"""Return a timestamp in the given time unit.
Arguments:
time_unit: One of
- 'ns': nanosecond.
- 'us': microsecond.
- 'ms': millisecond.
Returns:
A new Series with timestamps in the specified time unit.
Examples:
>>> from datetime import date
>>> import pandas as pd
>>> import narwhals as nw
>>> s_native = pd.Series(
... [date(2001, 1, 1), None, date(2001, 1, 3)], dtype="datetime64[ns]"
... )
>>> s = nw.from_native(s_native, series_only=True)
>>> s.dt.timestamp("ms").to_native()
0 9.783072e+11
1 NaN
2 9.784800e+11
dtype: float64
"""
if time_unit not in {"ns", "us", "ms"}:
msg = (
"invalid `time_unit`"
f"\n\nExpected one of {{'ns', 'us', 'ms'}}, got {time_unit!r}."
)
raise ValueError(msg)
return self._narwhals_series._with_compliant(
self._narwhals_series._compliant_series.dt.timestamp(time_unit)
)
def truncate(self, every: str) -> SeriesT:
"""Divide the date/datetime range into buckets.
Arguments:
every: Length of bucket. Must be of form `<multiple><unit>`,
where `multiple` is a positive integer and `unit` is one of
- 'ns': nanosecond.
- 'us': microsecond.
- 'ms': millisecond.
- 's': second.
- 'm': minute.
- 'h': hour.
- 'd': day.
- 'mo': month.
- 'q': quarter.
- 'y': year.
Returns:
Series of data type `Date` or `Datetime`.
Examples:
>>> from datetime import datetime
>>> import pandas as pd
>>> import narwhals as nw
>>> s_native = pd.Series([datetime(2021, 3, 1, 12, 34)])
>>> s = nw.from_native(s_native, series_only=True)
>>> s.dt.truncate("1h").to_native()
0 2021-03-01 12:00:00
dtype: datetime64[ns]
"""
return self._narwhals_series._with_compliant(
self._narwhals_series._compliant_series.dt.truncate(every)
)
|