Skip to content

Creating DatetimeIndex from empty array with datetime64 dtype raises IndexError #2804

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

Conversation

lesteve
Copy link
Contributor

@lesteve lesteve commented Feb 5, 2013

using pandas from master:

import numpy as np
import pandas as pd
pd.DatetimeIndex(np.array([], dtype='datetime64'))

I get the following stack trace:

/data/python/pandas/pandas/tseries/index.pyc in __new__(cls, data, freq, start, end, periods, copy, name, tz, verify_integrity, normalize, **kwds)
    229             else:
    230                 if data.dtype != _NS_DTYPE:
--> 231                     subarr = tslib.cast_to_nanoseconds(data)
    232                 else:
    233                     subarr = data

/data/python/pandas/pandas/tslib.so in pandas.tslib.cast_to_nanoseconds (pandas/tslib.c:14151)()

IndexError: invalid index

This happens because pandas.tslib.cast_to_nanoseconds expects the array to be not empty to get the datetime64 unit of the first element.

One way to fix it would be to check whether data is empty before trying the conversion.

It would be nice to have a better error inside pd.tslib.cast_to_nanoseconds saying that it expects a non-empty array as well.

wesm added a commit that referenced this pull request Feb 10, 2013
@wesm
Copy link
Member

wesm commented Feb 10, 2013

thanks!

@wesm wesm closed this Feb 10, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants