Skip to content

API: Int64Index cannot be converted into DatetimeIndex? #20997

Closed
@mroeschke

Description

@mroeschke

Is there a historical reason why an Int64Index (defaulting to represent epoch timestamps) cannot be converted to a DatetimeIndex?

In [7]: pd.__version__
Out[7]: '0.23.0rc2+27.geff1faf.dirty'

In [8]: val = [pd.Timestamp('2018-01-01').value]

In [9]: pd.DatetimeIndex(pd.Index(val))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-9-77aedfdf1582> in <module>()
----> 1 pd.DatetimeIndex(pd.Index(val))

/mnt/c/Users/Matt Roeschke/Projects/pandas-mroeschke/pandas/core/indexes/datetimes.py in __new__(cls, data, freq, start, end, periods, tz, normalize, closed, ambiguous, dayfirst, yearfirst, dtype, copy, name, verify_integrity)
    439             # must be integer dtype otherwise
    440             if isinstance(data, Int64Index):
--> 441                 raise TypeError('cannot convert Int64Index->DatetimeIndex')
    442             if data.dtype != _INT64_DTYPE:
    443                 data = data.astype(np.int64)

TypeError: cannot convert Int64Index->DatetimeIndex

# Expected behavior of In[9]
In [10]: pd.DatetimeIndex(pd.Index(val).values)
Out[10]: DatetimeIndex(['2018-01-01'], dtype='datetime64[ns]', freq=None)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions