Skip to content

narwhals.Series.dt

year()

Get the year in a datetime series.

Examples:

>>> import pandas as pd
>>> import polars as pl
>>> from datetime import datetime
>>> import narwhals as nw
>>> dates = [datetime(2012, 1, 7), datetime(2023, 3, 10)]
>>> s_pd = pd.Series(dates)
>>> s_pl = pl.Series(dates)

We define a library agnostic function:

>>> @nw.narwhalify
... def func(s_any):
...     return s_any.dt.year()

We can then pass either pandas or Polars to func:

>>> func(s_pd)
0    2012
1    2023
dtype: int...
>>> func(s_pl)
shape: (2,)
Series: '' [i32]
[
   2012
   2023
]

month()

Gets the month in a datetime series.

Examples:

>>> import pandas as pd
>>> import polars as pl
>>> from datetime import datetime
>>> import narwhals as nw
>>> dates = [datetime(2023, 2, 1), datetime(2023, 8, 3)]
>>> s_pd = pd.Series(dates)
>>> s_pl = pl.Series(dates)

We define a library agnostic function:

>>> @nw.narwhalify
... def func(s_any):
...     return s_any.dt.month()

We can then pass either pandas or Polars to func:

>>> func(s_pd)
0    2
1    8
dtype: int...
>>> func(s_pl)
shape: (2,)
Series: '' [i8]
[
   2
   8
]

day()

Extracts the day in a datetime series.

Examples:

>>> import pandas as pd
>>> import polars as pl
>>> from datetime import datetime
>>> import narwhals as nw
>>> dates = [datetime(2022, 1, 1), datetime(2022, 1, 5)]
>>> s_pd = pd.Series(dates)
>>> s_pl = pl.Series(dates)

We define a library agnostic function:

>>> @nw.narwhalify
... def func(s_any):
...     return s_any.dt.day()

We can then pass either pandas or Polars to func:

>>> func(s_pd)
0    1
1    5
dtype: int...
>>> func(s_pl)
shape: (2,)
Series: '' [i8]
[
   1
   5
]

ordinal_day()

Get ordinal day.

Examples:

>>> import pandas as pd
>>> import polars as pl
>>> from datetime import datetime
>>> import narwhals as nw
>>> data = [datetime(2020, 1, 1), datetime(2020, 8, 3)]
>>> s_pd = pd.Series(data)
>>> s_pl = pl.Series(data)

We define a library agnostic function:

>>> @nw.narwhalify
... def func(s_any):
...     return s_any.dt.ordinal_day()

We can then pass either pandas or Polars to func:

>>> func(s_pd)
0      1
1    216
dtype: int32
>>> func(s_pl)
shape: (2,)
Series: '' [i16]
[
   1
   216
]

hour()

Extracts the hour in a datetime series.

Examples:

>>> import pandas as pd
>>> import polars as pl
>>> from datetime import datetime
>>> import narwhals as nw
>>> dates = [datetime(2022, 1, 1, 5, 3), datetime(2022, 1, 5, 9, 12)]
>>> s_pd = pd.Series(dates)
>>> s_pl = pl.Series(dates)

We define a library agnostic function:

>>> @nw.narwhalify
... def func(s_any):
...     return s_any.dt.hour()

We can then pass either pandas or Polars to func:

>>> func(s_pd)
0    5
1    9
dtype: int...
>>> func(s_pl)
shape: (2,)
Series: '' [i8]
[
   5
   9
]

minute()

Extracts the minute in a datetime series.

Examples:

>>> import pandas as pd
>>> import polars as pl
>>> from datetime import datetime
>>> import narwhals as nw
>>> dates = [datetime(2022, 1, 1, 5, 3), datetime(2022, 1, 5, 9, 12)]
>>> s_pd = pd.Series(dates)
>>> s_pl = pl.Series(dates)

We define a library agnostic function:

>>> @nw.narwhalify
... def func(s_any):
...     return s_any.dt.minute()

We can then pass either pandas or Polars to func:

>>> func(s_pd)
0     3
1    12
dtype: int...
>>> func(s_pl)
shape: (2,)
Series: '' [i8]
[
   3
   12
]

second()

Extracts the second(s) in a datetime series.

Examples:

>>> import pandas as pd
>>> import polars as pl
>>> from datetime import datetime
>>> import narwhals as nw
>>> dates = [datetime(2022, 1, 1, 5, 3, 10), datetime(2022, 1, 5, 9, 12, 4)]
>>> s_pd = pd.Series(dates)
>>> s_pl = pl.Series(dates)

We define a library agnostic function:

>>> @nw.narwhalify
... def func(s_any):
...     return s_any.dt.second()

We can then pass either pandas or Polars to func:

>>> func(s_pd)
0    10
1     4
dtype: int...
>>> func(s_pl)
shape: (2,)
Series: '' [i8]
[
   10
    4
]

millisecond()

Extracts the milliseconds in a datetime series.

Examples:

>>> import pandas as pd
>>> import polars as pl
>>> from datetime import datetime
>>> import narwhals as nw
>>> dates = [
...     datetime(2023, 5, 21, 12, 55, 10, 400000),
...     datetime(2023, 5, 21, 12, 55, 10, 600000),
...     datetime(2023, 5, 21, 12, 55, 10, 800000),
...     datetime(2023, 5, 21, 12, 55, 11, 0),
...     datetime(2023, 5, 21, 12, 55, 11, 200000),
... ]
>>> s_pd = pd.Series(dates)
>>> s_pl = pl.Series(dates)

We define a library agnostic function:

>>> @nw.narwhalify
... def func(s_any):
...     return s_any.dt.millisecond().alias("datetime")

We can then pass either pandas or Polars to func:

>>> func(s_pd)
0    400
1    600
2    800
3      0
4    200
Name: datetime, dtype: int...
>>> func(s_pl)
shape: (5,)
Series: 'datetime' [i32]
[
    400
    600
    800
    0
    200
]

microsecond()

Extracts the microseconds in a datetime series.

Examples:

>>> import pandas as pd
>>> import polars as pl
>>> from datetime import datetime
>>> import narwhals as nw
>>> dates = [
...     datetime(2023, 5, 21, 12, 55, 10, 400000),
...     datetime(2023, 5, 21, 12, 55, 10, 600000),
...     datetime(2023, 5, 21, 12, 55, 10, 800000),
...     datetime(2023, 5, 21, 12, 55, 11, 0),
...     datetime(2023, 5, 21, 12, 55, 11, 200000),
... ]
>>> s_pd = pd.Series(dates)
>>> s_pl = pl.Series(dates)

We define a library agnostic function:

>>> @nw.narwhalify
... def func(s_any):
...     return s_any.dt.microsecond().alias("datetime")

We can then pass either pandas or Polars to func:

>>> func(s_pd)
0    400000
1    600000
2    800000
3         0
4    200000
Name: datetime, dtype: int...
>>> func(s_pl)
shape: (5,)
Series: 'datetime' [i32]
[
   400000
   600000
   800000
   0
   200000
]

nanosecond()

Extracts the nanosecond(s) in a date series.

Examples:

>>> import pandas as pd
>>> import polars as pl
>>> from datetime import datetime
>>> import narwhals as nw
>>> dates = [
...     datetime(2022, 1, 1, 5, 3, 10, 500000),
...     datetime(2022, 1, 5, 9, 12, 4, 60000),
... ]
>>> s_pd = pd.Series(dates)
>>> s_pl = pl.Series(dates)

We define a library agnostic function:

>>> @nw.narwhalify
... def func(s_any):
...     return s_any.dt.nanosecond()

We can then pass either pandas or Polars to func:

>>> func(s_pd)
0    500000000
1     60000000
dtype: int...
>>> func(s_pl)
shape: (2,)
Series: '' [i32]
[
   500000000
   60000000
]

total_minutes()

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.

Examples:

>>> import pandas as pd
>>> import polars as pl
>>> from datetime import timedelta
>>> import narwhals as nw
>>> data = [timedelta(minutes=10), timedelta(minutes=20, seconds=40)]
>>> s_pd = pd.Series(data)
>>> s_pl = pl.Series(data)

We define a library agnostic function:

>>> @nw.narwhalify
... def func(s_any):
...     return s_any.dt.total_minutes()

We can then pass either pandas or Polars to func:

>>> func(s_pd)
0    10
1    20
dtype: int...
>>> func(s_pl)
shape: (2,)
Series: '' [i64]
[
        10
        20
]

total_seconds()

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.

Examples:

>>> import pandas as pd
>>> import polars as pl
>>> from datetime import timedelta
>>> import narwhals as nw
>>> data = [timedelta(seconds=10), timedelta(seconds=20, milliseconds=40)]
>>> s_pd = pd.Series(data)
>>> s_pl = pl.Series(data)

We define a library agnostic function:

>>> @nw.narwhalify
... def func(s_any):
...     return s_any.dt.total_seconds()

We can then pass either pandas or Polars to func:

>>> func(s_pd)
0    10
1    20
dtype: int...
>>> func(s_pl)
shape: (2,)
Series: '' [i64]
[
        10
        20
]

total_milliseconds()

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.

Examples:

>>> import pandas as pd
>>> import polars as pl
>>> from datetime import timedelta
>>> import narwhals as nw
>>> data = [
...     timedelta(milliseconds=10),
...     timedelta(milliseconds=20, microseconds=40),
... ]
>>> s_pd = pd.Series(data)
>>> s_pl = pl.Series(data)

We define a library agnostic function:

>>> @nw.narwhalify
... def func(s_any):
...     return s_any.dt.total_milliseconds()

We can then pass either pandas or Polars to func:

>>> func(s_pd)
0    10
1    20
dtype: int...
>>> func(s_pl)
shape: (2,)
Series: '' [i64]
[
        10
        20
]

total_microseconds()

Get total microseconds.

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:

>>> import pandas as pd
>>> import polars as pl
>>> from datetime import timedelta
>>> import narwhals as nw
>>> data = [
...     timedelta(microseconds=10),
...     timedelta(milliseconds=1, microseconds=200),
... ]
>>> s_pd = pd.Series(data)
>>> s_pl = pl.Series(data)

We define a library agnostic function:

>>> @nw.narwhalify
... def func(s_any):
...     return s_any.dt.total_microseconds()

We can then pass either pandas or Polars to func:

>>> func(s_pd)
0      10
1    1200
dtype: int...
>>> func(s_pl)
shape: (2,)
Series: '' [i64]
[
        10
        1200
]

total_nanoseconds()

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.

Examples:

>>> import pandas as pd
>>> import polars as pl
>>> from datetime import timedelta
>>> import narwhals as nw
>>> data = ["2024-01-01 00:00:00.000000001", "2024-01-01 00:00:00.000000002"]
>>> s_pd = pd.to_datetime(pd.Series(data))
>>> s_pl = pl.Series(data).str.to_datetime(time_unit="ns")

We define a library agnostic function:

>>> @nw.narwhalify
... def func(s_any):
...     return s_any.diff().dt.total_nanoseconds()

We can then pass either pandas or Polars to func:

>>> func(s_pd)
0    NaN
1    1.0
dtype: float64
>>> func(s_pl)
shape: (2,)
Series: '' [i64]
[
        null
        1
]

to_string(format)

Convert a Date/Time/Datetime series into a String series with the given 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 pandas as pd
>>> import polars as pl
>>> import narwhals as nw
>>> data = [
...     datetime(2020, 3, 1),
...     datetime(2020, 4, 1),
...     datetime(2020, 5, 1),
... ]
>>> s_pd = pd.Series(data)
>>> s_pl = pl.Series(data)

We define a dataframe-agnostic function:

>>> @nw.narwhalify
... def func(s_any):
...     return s_any.dt.to_string("%Y/%m/%d")

We can then pass either pandas or Polars to func:

>>> func(s_pd)
0    2020/03/01
1    2020/04/01
2    2020/05/01
dtype: object
>>> func(s_pl)
shape: (3,)
Series: '' [str]
[
   "2020/03/01"
   "2020/04/01"
   "2020/05/01"
]