Skip to content

API: Int64Index cannot be converted into DatetimeIndex? #20997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mroeschke opened this issue May 10, 2018 · 1 comment · Fixed by #21216
Closed

API: Int64Index cannot be converted into DatetimeIndex? #20997

mroeschke opened this issue May 10, 2018 · 1 comment · Fixed by #21216
Labels
API Design Dtype Conversions Unexpected or buggy dtype conversions
Milestone

Comments

@mroeschke
Copy link
Member

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)
@jreback
Copy link
Contributor

jreback commented May 10, 2018

because you need a unit

This allows you to pass a unit (which defaults to ns)

In [7]: pd.to_datetime(pd.Index(val))
Out[7]: DatetimeIndex(['2018-01-01'], dtype='datetime64[ns]', freq=None)

We do allow an int64 array to be directly converted, it is assumed to be in ns

In [11]: pd.DatetimeIndex(pd.Index(val).values)
Out[11]: DatetimeIndex(['2018-01-01'], dtype='datetime64[ns]', freq=None)

So no real objection to allowing an Int64Index in the DTI constructor. I suppose even in .astype it is ok. Though user facing conversions should always be in to_datetime.

@jreback jreback added Dtype Conversions Unexpected or buggy dtype conversions API Design labels May 10, 2018
@jreback jreback added this to the 0.24.0 milestone May 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design Dtype Conversions Unexpected or buggy dtype conversions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants