Skip to content

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

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
sinhrks opened this issue Jun 21, 2014 · 1 comment · Fixed by #7544
Closed

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

sinhrks opened this issue Jun 21, 2014 · 1 comment · Fixed by #7544
Labels
API Design Compat pandas objects compatability with Numpy or Python functions Datetime Datetime data dtype
Milestone

Comments

@sinhrks
Copy link
Member

sinhrks commented Jun 21, 2014

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

jreback commented Jun 21, 2014

I think here's the reason. Long time ago datetimes were object dtype when embeded in a frame (prior 0.10.0). Now only a dateitme64[ns] with a tz and PeriodIndex need object dtype.

So I think ok to fix DatetimeIndex to allow an object dtype output (though issue is that it may try to coerce it back to datetime64[ns]! in the constructor)

See here for the embedding code: https://github.com/pydata/pandas/blob/master/pandas/core/frame.py#L2047 (should prob change PeriodIndex to use the same type of _to_embed method as well).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design Compat pandas objects compatability with Numpy or Python functions Datetime Datetime data dtype
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants