Skip to content

API: DatetimeIndex.asobject raises ValueError when contains NaT #7539

Closed
@sinhrks

Description

@sinhrks

Related to #7485.

Currently, DatetimeIndex.asobject raises ValueError when it contains NaT, otherwise PeriodIndex doesn't inconsistently.

I think these should work the same way, and would like to confirm PeriodIndex should raise ValueError, or DatetimeIndex.asobject allows NaT. There may be historical reason, but I'm not sure why DatetimeIndex raises ValueError even though we can create object-dtype Index containning NaT

didx = pd.DatetimeIndex([datetime.datetime(2014, 1, 1), pd.NaT])
didx.asobject
# ValueError: DatetimeIndex with NaT cannot be converted to object

pidx = pd.PeriodIndex(['2014-01', 'NaT'], freq='M')
pidx.asobject
# Index([2014-01, NaT], dtype='object')

Metadata

Metadata

Assignees

No one assigned

    Labels

    API DesignCompatpandas objects compatability with Numpy or Python functionsDatetimeDatetime data dtype

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions