I have 2 dataframes, taken from a larger frame (with df.head(x)
), both with the same index:
print df
val
DT
2017-03-06 00:00:00 1.06207
2017-03-06 00:02:00 1.06180
2017-03-06 00:04:00 1.06167
2017-03-06 00:06:00 1.06141
2017-03-06 00:08:00 1.06122
... ...
2017-03-10 21:50:00 1.06719
2017-03-10 21:52:00 1.06719
2017-03-10 21:54:00 1.06697
2017-03-10 21:56:00 1.06713
2017-03-10 21:58:00 1.06740
a and b are then taken from df
print a.index
print b.index
DatetimeIndex(['2017-03-06 00:32:00'], dtype='datetime64[ns]', name=u'DT', freq=None)
DatetimeIndex(['2017-03-06 00:18:00'], dtype='datetime64[ns]', name=u'DT', freq=None)
But, when I use a.index.item()
, I get it in the format 1488759480000000000. That means when I go to take a slice from df
based on a
and b
, I get an empty dataframe
>>> df[a.index.item() : b.index.item()]
Empty DataFrame
and further to that, when I try to convert them both:
df[a.index.to_pydatetime() : b.index.to_pydatetime()]
TypeError: Cannot convert input [[datetime.datetime(2017, 3, 6, 0, 18)]] of type <type 'numpy.ndarray'> to Timestamp
This is infuriating, surely there should be continuity of objects when using item()
. Can anyone give me some pointers?
You can use loc
with first value of a
and b
:
df.loc[a.index[0] : b.index[0]]
Your solution working if convert to Timestamp
:
print (df.loc[pd.Timestamp(a.index.item()): pd.Timestamp(b.index.item())])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.