Home > Mobile >  How to use DatetimeIndex as an index in Pandas
How to use DatetimeIndex as an index in Pandas

Time:02-26

I have a dataframe called region and the an array y is DatetimeIndex type like that:

print(y)
DatetimeIndex(['2016-01-01 00:30:00', '2016-01-01 01:30:00',
               '2016-01-01 02:00:00', '2016-01-01 03:00:00'])

But the problem, I do not know how I should use y as index in region. I tried to make it simpler with:

region["2016-01-01 00:30:00"]  # this line gives me an error KeyError: '2016-01-01 00:30:00'

I do not know why, however, this datetime is included in the index of region.

I am also looking for explanation for these results:

region["2016-01-01 00"]    # this works with me and print the result correctly
region["2016-01-01 00:30"]    # this does not work and gives me an error
region["2016-01-01 00:30:00"]    # this does not work and gives me an error

CodePudding user response:

You can try with .loc

region.loc[y]
  • Related