如何在 Pandas 中使用 DatetimeIndex 作为索引

How to use DatetimeIndex as an index in Pandas

我有一个名为 region 的数据框,数组 yDatetimeIndex 这样的类型:

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'])

但问题是,我不知道如何使用 y 作为 region 中的索引。我试图通过以下方式使其更简单:

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

不知道为什么,这个日期时间包含在区域的索引中。

我也在寻找这些结果的解释:

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

你可以试试.loc

region.loc[y]