简体   繁体   English

如何使用 pandas 中的日期时间索引列表索引 dataframe?

[英]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.

相关问题 如何使用日期时间索引在pandas中进行插值重建索引? - How can I do an interpolating reindex in pandas using datetime indices? 如何正确地将日期时间设置为 Pandas dataframe 的索引? - How do I properly set the Datetime as an index for a Pandas dataframe? 如何在熊猫数据框中索引DateTime - How to index DateTime in Pandas dataframe 如何按日期时间索引 pandas dataframe? - How to index a pandas dataframe by datetime? 如何使用索引列表从pandas数据帧中删除行 - How to Drop rows from pandas dataframe by using a list of indices 如何使用 boolean 索引索引 pandas dataframe? - How do I index an pandas dataframe using boolean indexing? 如何使用字符串访问pandas DataFrame datetime索引 - How to access pandas DataFrame datetime index using strings 使用列表为DataFrame编制索引,并为熊猫的越界索引返回NaN? - Index a DataFrame with a list and return NaN for out-of-bounds indices in Pandas? 如何使用另一个日期时间索引获取带有日期时间索引的 Pandas 数据框中的行? - How to get the rows in Pandas dataframe with datetime index using another datetime index? 如何将包含数据和datetime64 [ns]的列表与带有datetime64 [ns]索引的熊猫数据框合并 - How to merge a list containing data and datetime64[ns] with a pandas dataframe with datetime64[ns] index
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM