Skip to content

BUG: Do not fail when parsing pydatetime objects in pd.to_datetime #49298

Closed
@sv1990

Description

@sv1990

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

When reading excel files I often get date columns looking like

import pandas as pd
from datetime import datetime

s = pd.Series(["01/02/01", datetime(2001, 2, 2)])

When trying to parse those columns using

pd.to_datetime(s, format="%d/%m/%y")

the Exception ValueError("time data '2001-02-02 00:00:00' does not match format '%d/%m/%y' (match)") is raised.

The origin of this issue is that the already parsed datetime is converted to a string in isoformat and then an attempt to parse it in the given format is made.

Using pd.to_datetime without a format leads to wrong results since the format is ambiguous.

Feature Description

pd.to_datetime should either get an option to handle datetime.datetime objects differently or do so by default.

Alternative Solutions

The only alternative solution I could think off currently is a raw loop and checking the type of each element individually.

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions