to_datetime#
- skrub.to_datetime(data, format=None)[source]#
Convert a dataframe or series to Datetime dtype.
This function tries to convert the given dataframe or series from string to datetime, by either testing common datetime formats or using the
formatspecified by the user. Columns that cannot be parsed are returned unchanged.Note that this transformation is stateless, so it should not be used in a pipeline that is fitted on a training set and then applied to a test set. Use the
ToDatetimetransformer instead.Caution
For versions of Pandas <3.0, inferring the format may fail if it includes both date and time components, and the digits of the year are the same as the digits of the hour and minutes, like
"1959-07-01 19:59:16". In such cases, the format should be specified explicitly.- Parameters:
- datapandas or polars
{DataFrame, Series} The dataframe or series to convert to Datetime.
- format
strorNone, optional, default=None Format string to use to parse datetime strings. See the reference documentation for format codes: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes .
- datapandas or polars
- Returns:
- outputpandas or polars {DataFrame,
Series}. The input transformed to Datetime.
- outputpandas or polars {DataFrame,
See also
ToDatetimeParse datetimes represented as strings and return Datetime columns.
Examples
>>> import pandas as pd >>> from skrub import to_datetime >>> X = pd.DataFrame(dict(a=[1, 2], b=["01/02/2021", "21/02/2021"])) >>> X a b 0 1 01/02/2021 1 2 21/02/2021 >>> to_datetime(X) a b 0 1 2021-02-01 1 2 2021-02-21