简体   繁体   English

如何使用另一个日期时间索引获取带有日期时间索引的 Pandas 数据框中的行?

[英]How to get the rows in Pandas dataframe with datetime index using another datetime index?

I have a Pandas dataframe with the following datetime index:我有一个带有以下日期时间索引的 Pandas 数据框:

DatetimeIndex(['2020-01-02', '2020-01-03', '2020-01-06', '2020-01-07',
               '2020-01-08', '2020-01-09', '2020-01-10', '2020-01-13',
               '2020-01-14', '2020-01-15',
               ...
               '2020-01-17', '2020-01-21', '2020-01-22', '2020-01-23',
               '2020-01-24', '2020-01-27', '2020-01-28', '2020-01-29',
               '2020-01-30', '2020-01-31'],
              dtype='datetime64[ns]', name='Date', length=49098, freq=None)

I want to get the rows which intersect with the following datetime index:我想获取与以下日期时间索引相交的行:

DatetimeIndex(['2020-01-02', '2020-01-03', '2020-01-06', '2020-01-07',
               '2020-01-08', '2020-01-09', '2020-01-10'],
              dtype='datetime64[ns]', name='Date', freq=None)

What is the most natural (aka "Pythonic") way to do it?最自然(又名“Pythonic”)的方法是什么?

Use Index.intersection :使用Index.intersection

idx = idx1.intersection(idx2)

Or, if the indexes have not been previously defined:或者,如果之前未定义索引:

idx = df1.index.intersection(df2.index)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM