Pandas Dataframe Mask based on index

前端 未结 2 697
长情又很酷
长情又很酷 2020-12-19 08:05

I have the following dataframe:

import pandas as pd
index = pd.date_range(\'2013-1-1\',periods=10,freq=\'15Min\')
data = pd.DataFrame(data=[1,2,3,4,5,6,7,8,9         


        
相关标签:
2条回答
  • 2020-12-19 08:15

    'start' and 'stop' keywords are deprecated.With pandas >17.1; I had to use the following syntax instead:

    data.iloc[data.index.indexer_between_time('1:15', '02:00')]
    Out[90]: 
                         value
    2013-01-01 01:15:00      6
    2013-01-01 01:30:00      7
    2013-01-01 01:45:00      8
    2013-01-01 02:00:00      9
    
    0 讨论(0)
  • 2020-12-19 08:18

    You can mask using indexer_between_time:

    In [11]: data.index.indexer_between_time(start='01:15', end='02:00')
    Out[11]: array([5, 6, 7, 8])
    
    In [12]: data.iloc[data.index.indexer_between_time(start='1:15', end='02:00')]
    Out[12]:
                         value
    2013-01-01 01:15:00      6
    2013-01-01 01:30:00      7
    2013-01-01 01:45:00      8
    2013-01-01 02:00:00      9
    

    As you can see, you access the index by the attribute .index.

    Note: indexer_between_time by default both include_start and include_end are True, it also offers a tz argument to compare the time to a different timezone.

    0 讨论(0)
提交回复
热议问题