Parse nanoseconds with polars

Is it possible to parse dates with nanoseconds in polars?

>>> pl.Series(['2020-01-01 00:00:00.123456789']).str.strptime(pl.Datetime)
shape: (1,)

datetime[μs]

2020-01-01 00:00:00.123456

Notice how the nanoseconds were stripped off


Equivalent in pandas:

>>> pd.to_datetime(['2020-01-01 00:00:00.123456789'])
DatetimeIndex(['2020-01-01 00:00:00.123456789'], dtype='datetime64[ns]', freq=None)

>Solution :

I would have expected specifying ns as the time_unit to work:

>>> pl.Series(['2020-01-01 00:00:00.123456789']).str.strptime(pl.Datetime(time_unit="ns"))
shape: (1,)
Series: '' [datetime[ns]]
[
    2020-01-01 00:00:00.123456
]

Perhaps this is a "bug"?

Explicitly passing a format seems to work:

>>> pl.Series(['2020-01-01 00:00:00.123456789']).str.strptime(pl.Datetime, "%Y-%m-%d %H:%M:%S%.f")
shape: (1,)
Series: '' [datetime[ns]]
[
    2020-01-01 00:00:00.123456789
]

Leave a Reply