[英]How do I index a dataframe using a list of datetime indices in pandas?
If I want to locate one datetime index, I would use:如果我想找到一个日期时间索引,我会使用:
df.loc['2018-11-27 12:30:00']
But I have a list of datetime indexes但我有一个日期时间索引列表
DatetimeIndex(['2018-11-27 12:30:00', '2018-12-07 18:00:00',
'2018-12-15 16:30:00', '2019-01-13 20:30:00',
'2019-01-28 16:00:00', '2019-02-06 15:30:00',
'2019-03-04 11:00:00', '2019-03-14 15:00:00',
'2019-03-25 21:00:00', '2019-04-15 20:30:00',
...
'2021-04-14 07:00:00', '2021-05-08 20:30:00',
'2021-06-03 10:30:00', '2021-06-15 18:30:00',
'2021-06-29 17:30:00', '2021-08-23 12:00:00',
'2021-09-07 03:00:00', '2021-09-18 06:00:00',
'2021-10-20 16:00:00', '2021-11-10 18:00:00'],
dtype='datetime64[ns]', name='time', length=232, freq=None)
How do I pass them all into the dataframe?如何将它们全部传递到 dataframe?
I get this error if I use df.loc[indexes]:如果我使用 df.loc[indexes],我会收到此错误:
KeyError: "None of [DatetimeIndex(['2018-11-27 12:30:00', '2018-12-07 18:00:00',\n '2018-12-15 16:30:00', '2019-01-13 20:30:00',\n '2019-01-28 16:00:00', '2019-02-06 15:30:00',\n '2019-03-04 11:00:00', '2019-03-14 15:00:00',\n '2019-03-25 21:00:00', '2019-04-15 20:30:00',\n ...\n '2021-04-14 07:00:00', '2021-05-08 20:30:00',\n '2021-06-03 10:30:00', '2021-06-15 18:30:00',\n '2021-06-29 17:30:00', '2021-08-23 12:00:00',\n '2021-09-07 03:00:00', '2021-09-18 06:00:00',\n '2021-10-20 16:00:00', '2021-11-10 18:00:00'],\n dtype='datetime64[ns]', name='time', length=232, freq=None)] are in the [index]"
Test df.index
with list:用列表测试
df.index
:
df[df.index.isin(L)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.