Nushell
Get Nu!
Getting Started
  • The Nushell Book
  • Command Reference
  • Cookbook
  • Language Reference Guide
  • Contributing Guide
Blog
  • English
  • 中文
  • Deutsch
  • Français
  • Español
  • 日本語
  • Português do Brasil
  • Русский язык
GitHub
Get Nu!
Getting Started
  • The Nushell Book
  • Command Reference
  • Cookbook
  • Language Reference Guide
  • Contributing Guide
Blog
  • English
  • 中文
  • Deutsch
  • Français
  • Español
  • 日本語
  • Português do Brasil
  • Русский язык
GitHub
  • Categories

    • Bits
    • Bytes
    • Chart
    • Conversions
    • Core
    • Database
    • Dataframe
    • Dataframe Or Lazyframe
    • Date
    • Debug
    • Default
    • Env
    • Experimental
    • Expression
    • Filesystem
    • Filters
    • Formats
    • Generators
    • Hash
    • History
    • Lazyframe
    • Math
    • Misc
    • Network
    • Path
    • Platform
    • Plugin
    • Prompt
    • Random
    • Removed
    • Shells
    • Strings
    • System
    • Viewers

polars as-datetime for dataframe

Converts string to datetime.

This command requires a plugin

The polars as-datetime command resides in the polars plugin. To use this command, you must install and register nu_plugin_polars. See the Plugins chapter in the book for more information.

Signature

> polars as-datetime {flags} (format)

Flags

  • --not-exact, -n: the format string may be contained in the date (e.g. foo-2021-01-01-bar could match 2021-01-01)
  • --naive: the input datetimes should be parsed as naive (i.e., not timezone-aware). Ignored if input is an expression.
  • --ambiguous, -a {oneof<string, nothing>}: Determine how to deal with ambiguous datetimes: raise (default): raise error earliest: use the earliest datetime latest: use the latest datetime null: set to null Used only when input is a lazyframe or expression and ignored otherwise

Parameters

  • format: formatting date time string

Input/output types:

inputoutput
dataframedataframe
expressionexpression

Examples

Converts string to datetime

> ["2021-12-30 00:00:00 -0400" "2021-12-31 00:00:00 -0400"] | polars into-df | polars as-datetime "%Y-%m-%d %H:%M:%S %z"
╭───┬─────────────╮
│ # │  datetime   │
├───┼─────────────┤
│ 0 │ 3 years ago │
│ 1 │ 3 years ago │
╰───┴─────────────╯

Converts string to datetime with high resolutions

> ["2021-12-30 00:00:00.123456789" "2021-12-31 00:00:00.123456789"] | polars into-df | polars as-datetime "%Y-%m-%d %H:%M:%S.%9f" --naive
╭───┬─────────────╮
│ # │  datetime   │
├───┼─────────────┤
│ 0 │ 3 years ago │
│ 1 │ 3 years ago │
╰───┴─────────────╯

Converts string to datetime using the --not-exact flag even with excessive symbols

> ["2021-12-30 00:00:00 GMT+4"] | polars into-df | polars as-datetime "%Y-%m-%d %H:%M:%S" --not-exact --naive
╭───┬─────────────╮
│ # │  datetime   │
├───┼─────────────┤
│ 0 │ 3 years ago │
╰───┴─────────────╯

Converts string to datetime using the --not-exact flag even with excessive symbols in an expression

> ["2025-11-02 00:00:00", "2025-11-02 01:00:00", "2025-11-02 02:00:00", "2025-11-02 03:00:00"] | polars into-df | polars select (polars col 0 | polars as-datetime "%Y-%m-%d %H:%M:%S")
╭───┬─────────────╮
│ # │  datetime   │
├───┼─────────────┤
│ 0 │ in 4 months │
│ 1 │ in 4 months │
│ 2 │ in 4 months │
│ 3 │ in 4 months │
╰───┴─────────────╯

Notes

Format example: "%y/%m/%d %H:%M:%S" => 21/12/31 12:54:98 "%y-%m-%d %H:%M:%S" => 2021-12-31 24:58:01 "%y/%m/%d %H:%M:%S" => 21/12/31 24:58:01 "%y%m%d %H:%M:%S" => 210319 23:58:50 "%Y/%m/%d %H:%M:%S" => 2021/12/31 12:54:98 "%Y-%m-%d %H:%M:%S" => 2021-12-31 24:58:01 "%Y/%m/%d %H:%M:%S" => 2021/12/31 24:58:01 "%Y%m%d %H:%M:%S" => 20210319 23:58:50 "%FT%H:%M:%S" => 2019-04-18T02:45:55 "%FT%H:%M:%S.%6f" => microseconds "%FT%H:%M:%S.%9f" => nanoseconds