Pandas Datetimeindex Indexing Dtype: Datetime64 Vs Timestamp
Indexing a pandas DatetimeIndex (with dtype numpy datetime64[ns]) returns either: another DatetimeIndex for multiple indices a pandas Timestamp for single index The confusing par
Solution 1:
You are using numpy functions to manipulate pandas types. They are not always compatible.
The function np.in1d
first converts its both arguments to ndarrays. A DatetimeIndex
has a built-in conversion and an array of dtype np.datetime64
is returned (it's DatetimIndex.values
). But a Timestamp
doesn't have such a facility and it's not converted.
Instead, you can use for example a python keyword in
(the most natural way):
a_datetimeindex[0] in a_datetimeindex
or an Index.isin
method for a collection of elements
a_datetimeindex.isin(a_list_or_index)
If you want to use np.in1d
, explicitly convert both arguments to numpy types. Or call it on the underlying numpy arrays:
np.in1d(a_datetimeindex.values[0], a_datetimeindex.values)
Alternatively, it's probably safe to use np.in1d
with two collections of the same type:
np.in1d(a_datetimeindex, another_datetimeindex)
or even
np.in1d(a_datetimeindex[[0]], a_datetimeindex)
Post a Comment for "Pandas Datetimeindex Indexing Dtype: Datetime64 Vs Timestamp"